Elasticsearch 以更复杂的方式组合分数

Elasticsearch combining scores in more complex ways

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

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() returns 最大分数): score = searchResult("name") + 最大值(搜索结果(“name.prefix”),搜索结果(“name.suffix”))?或者换句话说,如何得到一个字段的分数与其他两个字段的分数的最大值之和?

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

If a returned document matches multiple query clauses, the dis_max query assigns the document the highest relevance score from any matching clause, plus a tie breaking increment for any additional matching subqueries.

{
  "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 query

的总和

如果您 运行 使用 _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" : [
        .........
              ]
            }
         ]
  }