首页 > 解决方案 > Elasticsearch 以更复杂的方式组合分数

问题描述

假设我们发出以下搜索请求:

GET my-index-000001/_search
{
  query: {
    multi_match: {
      type: "most_fields",
      query: "word1 word2",
      fields: [
        "name",
        "name.prefix",
        "name.suffix"
      ]
    }
  }                  
}

这样,Elasticsearch 索引中每个对象的返回分数等于以下每个字段的搜索分数之和:“name”、“name.prefix”、“name.suffix”。我怎么能得到以下分数,而不是得到所有这些字段的总和(searchResult(field) 表示从字段获得的分数,maximum() 返回最高分数): score = searchResult("name") + maximum(searchResult ("name.prefix"), searchResult("name.suffix")) ? 或者换句话说,我怎样才能得到一个字段的分数和其他两个字段的最大值的总和?

标签: httpelasticsearchrequest

解决方案


您可以使用dis_max查询来获得最高分

如果返回的文档与多个查询子句匹配,则 dis_max 查询为该文档分配来自任何匹配子句的最高相关性分数,并为任何其他匹配的子查询加上一个平局增量。

{
  "query": {
    "bool": {
      "should": [
        {
          "dis_max": {
            "queries": [
              {
                "match": {
                  "name.prefix": "word1 word2"
                }
              },
              {
                "match": {
                  "name.suffix": "word1 word2"
                }
              }
            ],
            "tie_breaker": 0.7
          }
        },
        {
          "match": {
            "name.full_name": "word1 word2"
          }
        }
      ]
    }
  }
}

should 子句将给出 name.full_name 和 dis_max 查询的总和

如果您将使用 _search?explain=true 运行,您将获得有关如何计算分数的说明

"_explanation" : {
          "value" : 1.3521059,
          "description" : "sum of:",
          "details" : [
            {
              "value" : 0.7767416,
              "description" : "max plus 0.7 times others of:",
              "details" : [
                {
                  "value" : 0.2876821,
                  "description" : "sum of:",
                  "details" :[....]
            },
                {......}        
          ]
            },
        {
              "value" : 0.5753642,
              "description" : "sum of:",
              "details" : [
        .........
              ]
            }
         ]
  }

推荐阅读