如何在 Elasticsearch 查询中确定文档的优先级

How can I prioritize documents in Elasticsearch query

我的索引中有产品。文档的基本结构如下:

{
    "_id": "product",
    "_source": {
      ...
      "type": "product",
      "id": 1,
      "mainTaxon": {
        "name": "T-SHIRT",
      },
      "attributes": [
        {
          "code": "name",
          "name": "Name",
          "value": [
            "BANANA T-SHIRT"
          ],
          "score": 50
        },
      ]
    }
  },
  {
    "_id": "product",
    "_source": {
      ...
      "type": "product",
      "id": 2,
      "mainTaxon": {
        "name": "JEANS",
      },
      "attributes": [
        {
          "code": "name",
          "name": "Name",
          "value": [
            "BANANA JEANS"
          ],
          "score": 50
        },
      ]
    }
  }
}

当我搜索“BANANA”时,我会优先考虑 mainTaxonJEANS 不同的产品。因此,每个名称为 mainTaxon T_SHIRT 或其他名称的产品都会列在名称为 mainTaxon JEANS.

的产品之前

您可以使用 boosting query 来确定文档的优先级

 {
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "attributes.value": "banana"
        }
      },
      "negative": {
        "match": {
          "mainTaxon.name": "JEANS"
        }
      },
      "negative_boost": 0.5
    }
  }
}

搜索结果将是

"hits": [
      {
        "_index": "67164768",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5364054,
        "_source": {
          "type": "product",
          "id": 1,
          "mainTaxon": {
            "name": "T-SHIRT"
          },
          "attributes": [
            {
              "code": "name",
              "name": "Name",
              "value": [
                "BANANA T-SHIRT"
              ],
              "score": 50
            }
          ]
        }
      },
      {
        "_index": "67164768",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.32743764,
        "_source": {
          "type": "product",
          "id": 2,
          "mainTaxon": {
            "name": "JEANS"
          },
          "attributes": [
            {
              "code": "name",
              "name": "Name",
              "value": [
                "BANANA JEANS"
              ],
              "score": 50
            }
          ]
        }
      }
    ]