# Which SHOULD query gave the match?

**URL:** <https://discuss.elastic.co/t/which-should-query-gave-the-match/116327>\
**Category:** Elasticsearch\
**Created:** [January 20, 2018, 6:13am UTC](https://discuss.elastic.co/t/which-should-query-gave-the-match/116327 "2018-01-20T06:13:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yonigo](https://avatars.discourse-cdn.com/v4/letter/y/7c8e57/32.png) [@yonigo](https://discuss.elastic.co/u/yonigo)\
**Post date:** [January 20, 2018, 6:13am UTC](https://discuss.elastic.co/t/which-should-query-gave-the-match/116327/1 "2018-01-20T06:13:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 20, 2018, 6:18am UTC](https://discuss.elastic.co/t/which-should-query-gave-the-match/116327/2 "2018-01-20T06:18:04Z")

</div>

You could use [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html) 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"
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 17, 2018, 6:18am UTC](https://discuss.elastic.co/t/which-should-query-gave-the-match/116327/3 "2018-02-17T06:18:07Z")

</div>

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