1

让我们假设我想执行这个查询

GET /_search
{
"query": {
    "dis_max" : {
        "queries" : [
            { "term" : { "title" : "Quick pets" }},
            { "term" : { "body" : "Quick pets" }}
        ],
        "tie_breaker" : 0.7
    }
}

}

根据 elasticsearch 的文档,此查询返回任何匹配子句中相关性得分最高的文档列表。

但是我如何确定是哪个基础查询导致文档出现在结果列表中?如何确定结果是否由于查询列表中的查询 1 或查询 2 而出现?我可以以某种方式为每个结果文档返回这个吗?

4

2 回答 2

2

您可以使用命名查询

询问:

{
  "query": {
    "dis_max": {
      "queries": [
        {
          "term": {
            "title.keyword": {
              "value": "Quick pets",
              "_name": "title" --> give name for each query
            }
          }
        },
        {
          "term": {
            "body.keyword": {
              "value": "Quick pets",
              "_name": "body"
            }
          }
        }
      ],
      "tie_breaker": 0.7
    }
  }
}

结果:

"hits" : [
      {
        "_index" : "index55",
        "_type" : "_doc",
        "_id" : "mAGWNXIBrjSHR7JVvY4C",
        "_score" : 0.6931471,
        "_source" : {
          "title" : "Quick pets"
        },
        "matched_queries" : [
          "title"
        ]
      },
      {
        "_index" : "index55",
        "_type" : "_doc",
        "_id" : "mQGXNXIBrjSHR7JVGI4E",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "ddd",
          "body" : "Quick pets"
        },
        "matched_queries" : [
          "body"
        ]
      }
    ]
于 2020-05-21T05:04:25.660 回答
0

要详细了解如何计算分数,您可以"explain": true在请求正文中添加选项。

这将为您提供有关哪个条款占哪个分数的完整解释。不要忘记dis_max返回等于最高得分子句加上tie_braker乘以其余分数的分数。

官方 ES 文档在这里解释

于 2021-07-23T10:21:18.310 回答