ElasticSearch词和词之间的不同得分

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

我的要求是这样的:

{
    "from":0,
    "query":{
        "bool":{
            "must":[
                {"term":{
                    "type_id":{
                        "value" : 34
                    }
                }}
            ]
        }
    },
    "size":20
}

术语

{
    "from":0,
    "query":{
        "bool":{
            "must":[
                {"terms":{"type_id":[34]}}
            ]
        }
    },
    "size":20
}

此索引中有67个文档
term查询返回得分为0.0149每个文档
但是terms查询返回分数是1.0
term评分似乎为1/67
我在索引中添加了另一个type_id 35,2016文档后
term得分为3.4299169
terms评分仍为1.0
我希望条件和条件执行相同。
想知道它是如何工作的吗?
谢谢

6kkfgxo0

6kkfgxo01#

link包含更多讨论
术语查询不会根据匹配术语的IDF(稀有性)对匹配项进行评分,而术语查询会。
如果您将使用?explain=true运行这两个查询。您将看到terms查询基于计算得分,而terms应用constantScore

术语查询

GET index62/_search?explain=true
{
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "type_id.keyword": {
              "value": "35"
            }
          }
        }
      ]
    }
  },
  "size": 20
}

结果

"_explanation" : {
          "value" : 0.4700036,
          "description" : "weight(type_id.keyword:35 in 0) [PerFieldSimilarity], result of:",
          "details" : [
            {
         "value" : 0.4700036,
              "description" : "score(freq=1.0), computed as boost * idf * tf from:",
              "details" : [
                {
                  "value" : 2.2,
                  "description" : "boost",
                  "details" : [ ]
                },
                {
                  "value" : 0.47000363,
                  "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                  "details" : [
                    {
                      "value" : 2,
                      "description" : "n, number of documents containing term",
                      "details" : [ ]
                    },
                    {
                      "value" : 3,
                      "description" : "N, total number of documents with field",
                      "details" : [ ]
                    }
                  ]
                },
                {
                  "value" : 0.45454544,
                  "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                     "value" : 1.2,
                      "description" : "k1, term saturation parameter",
                      "details" : [ ]
                    },
                    {
                      "value" : 0.75,
                      "description" : "b, length normalization parameter",
                      "details" : [ ]
                    },
                    {
                      "value" : 1.0,
                      "description" : "dl, length of field",
                      "details" : [ ]
                    },
                    {
                      "value" : 1.0,
                      "description" : "avgdl, average length of field",
                      "details" : [ ]
                    }
                  ]
                }
              ]
            }
          ]
        }

条款查询

{
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "type_id.keyword": [
              "35"
            ]
          }
        }
      ]
    }
  },
  "size": 20
}

结果

"_explanation" : 
{
      "value" : 1.0,
      "description" : "ConstantScore(type_id.keyword:35)",
      "details" : [ ]
}

相关问题