首页 > 解决方案 > Elasticsearch 应该有不同的分数

问题描述

我通过过滤和使用布尔查询来应用分数来检索文档。例如:

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "color": "Yellow"
          }
        },
        {
          "term": {
            "color": "Red"
          }
        },

        {
          "term": {
            "color": "Blue"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

如果数据只有“黄色”,它会给我“1.5”的分数,但如果数据只有“红色”,它会给我“1.4”的分数。我希望分数是一样的。每个数据只有 1 个匹配项,为什么分数不同?应该查询中有什么可以忽略术语的顺序吗?当我只有 1 场比赛时,“黄色”的比赛总是会获得高分......

更新:问题不是应该数组中的术语顺序,而是“包含该术语的文档数量”

标签: elasticsearchelasticsearch-queryterm-query

解决方案


如果评分对您不重要,您可以将filter子句与子句一起使用bool/should

过滤上下文避免了评分部分,是一个正常的是/否查询。因此,匹配文档的分数将始终为 0.0

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "color.keyword": "Yellow"
              }
            },
            {
              "term": {
                "color.keyword": "Black"
              }
            },
            {
              "term": {
                "color.keyword": "Purple"
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
  }
} 

匹配文档的分数取决于几个因素,例如字段长度、术语频率、文档总数等。

您可以通过解释 API了解更多关于如何计算分数的信息

GET /_search?explain=true

推荐阅读