Which SHOULD query gave the match?

I have a should clause inside a filter with some queries in it.
A document will match if at least one of the queries match.
Is there a way i can understand from the returned documents which query in the SHOULD gave the match for each doc?

You could use named queries for this, by giving a name to each of your queries. In the results, each hit will feature a matched_queries array containing the names of the queries that matched (e.g. query_1 and query_2 below).

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "content": {
              "query": "foo",
              "_name": "foo_query"
            }
          }
        },
        {
          "match": {
            "content": {
              "query": "baz",
              "_name": "baz_query"
            }
          }
        }
      ]
    }
  }
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.