ElasticSearch如何为同一个字段设置不同的分值?

whhtz7ly  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(204)

我在一个ES索引中有不同的type_id,并且希望给不同的值type_id不同的分数,以使某些类型的搜索结果排名更高。

{
    "query":{
        "bool":{
            "must":[
                {"terms":{"type_id":[9,10]}}
            ],
            "should":[
                {"match":{ "display_name":{"query":"keyword","boost":10}}},
                {"match":{ "description":{"query":"keyword","boost":2}}}
            ]
        }
    }
}

display_namedescription相同时,我想使type_id 9的匹配分数高于type_id 10。
请在这个问题上指导我。谢谢。

wz1wpwve

wz1wpwve1#

你可以像下面这样对你的查询进行分组,并使用boost来给某些id更多的权重。

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "term": {
                  "type_id": {
                    "value": 9,
                    "boost": 2
                  }
                }
              },
              {
                "term": {
                  "type_id": {
                    "value": 10,
                    "boost": 1
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1, 
            "should": [
              {
                "match": {
                  "display_name": {
                    "query": "keyword",
                    "boost": 10
                  }
                }
              },
              {
                "match": {
                  "description": {
                    "query": "keyword",
                    "boost": 2
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

编辑:注解中的查询,可以使用function_score

{
  "query": {
    "bool": {
      "minimum_should_match": 1, 
      "should": [
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type_id": {
                        "value": 9
                      }
                    }
                  }
                ],
                "minimum_should_match": 1,
                "should": [
                  {
                    "match": {
                      "display_name": {
                        "query": "keyword"
                      }
                    }
                  },
                  {
                    "match": {
                      "description": {
                        "query": "keyword"
                      }
                    }
                  }
                ]
              }
            },
            "boost": "5"
          }
        },
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type_id": {
                        "value": 10
                      }
                    }
                  }
                ],
                "minimum_should_match": 1,
                "should": [
                  {
                    "match": {
                      "display_name": {
                        "query": "keyword"
                      }
                    }
                  },
                  {
                    "match": {
                      "description": {
                        "query": "keyword"
                      }
                    }
                  }
                ]
              }
            },
            "boost": "4"
          }
        }
      ]
    }
  }
}

相关问题