Elasticsearch dis_max查询,return精确匹配查询

Elasticsearch dis_max query, return exact matching query

假设我想执行这个查询

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

}

根据 elasticsearch 的文档,此查询 return 是 any 匹配子句中相关性得分最高的文档列表。

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

您可以使用named queries

查询:

{
  "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"
        ]
      }
    ]

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

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

explain here.

的官方 ES 文档