# How to know which documents in the search results include all of the tokens in the search query (for one particular field)

**URL:** https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639
**Category:** Elasticsearch
**Created:** [June 18, 2020, 1:06pm UTC](https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639 "2020-06-18T13:06:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![swafa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swafa/32/77405_2.png) [@swafa](https://discuss.elastic.co/u/swafa)
#### Post date: [June 18, 2020, 1:06pm UTC](https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639/1 "2020-06-18T13:06:06Z")

</div>

We are trying to find out which documents in our search results include all the tokens in our search query. For example when running the following query:

```auto
    GET /test/_search
    {
        "query" : {
          "match" : { "message" : { "query": "the brown fox" } }
        }
    }

```

We would need a response like this (only the hits array is included with minimal fields for brevity):

```auto
    "hits" : [
      {
        **"all_tokens_match": true** ,
        "_source" : {
          "message" : "The Quick Brown Fox"
        }
      },
      {
        **"all_tokens_match": false** ,
        "_source" : {
          "message" : "The Brown Bear"
        }
      }
    ]

```

How would you recommend we approach this kind of problem on Elasticsearch? We were looking into scripting but our field is usually indexed as a "text" and not as a "keyword" field and there seems to be some limitations with scripting in that case. In case you recommend scripting, we would be grateful for a snippet to guide us in the right direction. Thank you in advance for your support.

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [June 18, 2020, 9:07pm UTC](https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639/2 "2020-06-18T21:07:59Z")

</div>

Check if [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-queries-and-filters) will help your problem.  
If you use named queries, you would need to break your tokens into distinct queries, something like this:

```auto
GET /_search
{
    "query": {
        "bool" : {
            "should" : [
                {"match" : { "message" : {"query" : "the", "_name" : "my_query1"} }},
                {"match" : { "message" : {"query" : "brown", "_name" : "my_query2"} }},
                {"match" : { "message" : {"query" : "fox", "_name" : "my_query3"} }}
            ]
        }
    }
}

```

as a result you will get the following:

```auto
"hits" : [
      {
        "_index" : "my_index",
       ....
        "_source" : {
          "message" : "The Quick Brown Fox"
        },
        "matched_queries" : [
          "my_quer1",
          "my_query2",
          "my_query3"
        ]
      },
      {
        "_index" : "my_index",
          ...
        "_source" : {
          "message" : "The Brown Bear"
        },
        "matched_queries" : [
          "my_query1",
          "my_query2"
        ]
      },

```

---

<div class="post-metadata">

### Author: ![swafa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swafa/32/77405_2.png) [@swafa](https://discuss.elastic.co/u/swafa)
#### Post date: [June 23, 2020, 12:37pm UTC](https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639/3 "2020-06-23T12:37:46Z")

</div>

Thank you @mayya for guiding us in the right direction of named queries. It helped us tackle our problem after we combined named queries with the "minimum\_should\_match" parameter and set "boost": 0 to avoid it affecting the score of our main query.

We did it like this:

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "message": {
                            "query": "the brown fox",
                            "_name": "main_query"
                        }
                    }
                },
                {
                    "match": {
                        "message": {
                            "query": "the brown fox",
                            "_name": "all_tokens_match",
                            "minimum_should_match": "100%",
                            "boost": 0
                        }
                    }
                }
            ]
        }
    }
}

```

So we receive a response like this:

```auto
{
    "hits": [
        {
            "_score": 0.99938476,
            "_source": {
                "message": "The Quick Brown Fox"
            },
            "matched_queries": [
                "main_query",
                "all_tokens_match"
            ]
        },
        {
            "_score": 0.38727614,
            "_source": {
                "message": "The Brown Bear"
            },
            "matched_queries": [
                "main_query"
            ]
        }
    ]
}

```

Documents that all tokens in our query match now have **all\_tokens\_match** included in the **matched\_queries** part of the response.

---

<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: [July 21, 2020, 12:38pm UTC](https://discuss.elastic.co/t/how-to-know-which-documents-in-the-search-results-include-all-of-the-tokens-in-the-search-query-for-one-particular-field/237639/4 "2020-07-21T12:38:04Z")

</div>

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