fhaase
fhaase

Reputation: 38

Elasticsearch dis_max query, return exact matching query

Lets assume i want to perform this query

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

}

According to the documentation of elasticsearch, this query returns a list of documents with the highest relevance score from any matching clause.

But how can i determine which underlying query caused the document to appear in the result list? How can i determine if a result appears due to query 1 or query 2 in the list of queries? Can i somehow return this for each result document?

Upvotes: 1

Views: 878

Answers (2)

Andy186
Andy186

Reputation: 33

To get a breakdown of how the score was computed you can add the "explain": true option in the request body.

That will give you the full explanation of which clause accounted for which score. Don't forget that dis_max retuns a score equal to the highest scoring clause plus tie_braker times the rest of the scores.

Official ES documentation for explain here.

Upvotes: 0

jaspreet chahal
jaspreet chahal

Reputation: 9099

You can use named queries

Query:

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

Result:

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

Upvotes: 2

Related Questions