0

我的索引中有产品。文档的结构基本上是这样的:

{
    "_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”时,我会优先考虑与mainTaxon不同的产品JEANS。因此,带有mainTaxon名称T_SHIRT或其他名称的每个产品都将列在带有mainTaxon JEANS.

4

1 回答 1

1

您可以使用提升查询来确定文档的优先级

 {
  "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
            }
          ]
        }
      }
    ]
于 2021-04-19T15:36:51.037 回答