# How do I get find which parts of an Elasticsearch query match

**URL:** <https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761>\
**Category:** Elasticsearch\
**Created:** [January 9, 2023, 6:58pm UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761 "2023-01-09T18:58:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirs5](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@amirs5](https://discuss.elastic.co/u/amirs5)\
**Post date:** [January 9, 2023, 6:58pm UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/1 "2023-01-09T18:58:40Z")

</div>

I have a list of Key-word-sets, for example:

```auto
set_1: ["movie", "cinema", "theater"],
set_2: ["beach", "ocean", "dock"],
set_3: ["office", "downtown", "center"]

```

and I want to build up a single query that tells us which one of these sets of keywords have any match in the text.

For example, if the text is the following

```auto
I went to the movie theater, which was down by the beach

```

The query should return `[set_1, set_2]`

and the following text

```auto
The office for my workplace is downtown

```

Should return `[set_3]`

Is there any way of doing this simply? We ideally want to fit in one query. The idea was rolling something by hand with `highlight` in the query but that seems like it would be difficult.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 9, 2023, 7:32pm UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/2 "2023-01-09T19:32:31Z")

</div>

Hi @amirs5

set\_1, set\_2 and set\_3 are different documents? If yes, I believe that the should clausule can help you.

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "set_1": {
              "query": "I went to the movie theater, which was down by the beach"
            }
          }
        },
        {
          "match": {
            "set_2": {
              "query": "I went to the movie theater, which was down by the beach"
            }
          }
        },
        {
          "match": {
            "set_3": {
              "query": "I went to the movie theater, which was down by the beach"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "set_1": {},
      "set_2": {},
      "set_3": {}
    }
    }
  }

```

Hits:

```auto
"hits": [
      {
        "_index": "bar",
        "_id": "MjP_l4UBQB-6H-4ZoKZr",
        "_score": 0.5753642,
        "_source": {
          "set_1": [
            "movie",
            "cinema",
            "theater"
          ]
        },
        "highlight": {
          "set_1": [
            "<em>movie</em>",
            "<em>theater</em>"
          ]
        }
      },
      {
        "_index": "bar",
        "_id": "MzP_l4UBQB-6H-4ZoKZr",
        "_score": 0.2876821,
        "_source": {
          "set_2": [
            "beach",
            "ocean",
            "dock"
          ]
        },
        "highlight": {
          "set_2": [
            "<em>beach</em>"
          ]
        }
      }
    ]

```

---

<div class="post-metadata">

**Author:** ![amirs5](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@amirs5](https://discuss.elastic.co/u/amirs5)\
**Post date:** [January 9, 2023, 9:28pm UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/3 "2023-01-09T21:28:33Z")

</div>

Hi, Thank you for the response,

I'm looking for something slightly different, set\_1 set\_2 and set\_3 are all part of the query.

"I went to the movie theater, which was down by the beach" is an example of a potential document.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 9, 2023, 11:21pm UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/4 "2023-01-09T23:21:43Z")

</div>

What elasticsearch returns are the documents that match the list of terms in the input.

> [@amirs5](#):
>
> The query should return `[set_1, set_2]`

Why do you want to do this?

---

<div class="post-metadata">

**Author:** ![amirs5](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@amirs5](https://discuss.elastic.co/u/amirs5)\
**Post date:** [January 10, 2023, 12:57am UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/5 "2023-01-10T00:57:16Z")

</div>

The idea is we have a series of large bodies of text that we index in our Elastic search clusters, for example one would be a news article.

We then want to use the Elasticsearch query to "tag" the article of text.

We build up all the sets of keywords, for example:

set\_1 is the "cinema keywords set" which I describe above  
set\_2 is the "ocean keywords set"  
set\_3 is the office keywords set.

We may have hundreds of these keywords set.

Then we want to use Elasticsearch to tag a specific article with the keyword sets. For example, a document that mentions cinema and ocean keywords can return the correct tags from the query.

Of course there are ways of doing this on our own, however if we had something like this it would be much easier as we don't want to load in the full body of text, and we also want to avoid rolling our own lemmatization logic, etc.

I hope this makes more sense. Thank you!

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 10, 2023, 1:31am UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/6 "2023-01-10T01:31:01Z")

</div>

I think I understand. You want to use passing a list to check if the terms exist in the text, if so, these terms become tags that can later be indexed in the document to facilitate the search. Right?

In this case, you pass the list using the Terms Query, where a match happens, you use the highlight to mark the text. With the terms marked, you create the list of tags in the document.

The problem I see is that it is not possible to send the list of sets and make the correspondences you want. The option is to make requests for each set separately.

Wouldn't it be better for you to have a service that infers the tags of the texts instead of trying to do it through Elasticsearch?

---

<div class="post-metadata">

**Author:** ![amirs5](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@amirs5](https://discuss.elastic.co/u/amirs5)\
**Post date:** [January 10, 2023, 2:28am UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/7 "2023-01-10T02:28:45Z")

</div>

Thank you! That is the idea we had as well. Going from Highlights -\> back to search terms makes sense.

Yes I agree that having a service such as that would be useful however we wanted to keep our documents indexed only in one place (Elasticsearch) if possible.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 10, 2023, 2:37am UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/8 "2023-01-10T02:37:33Z")

</div>

Following the logic of the Terms Query, the closest to the result you want would be this for set\_1. You would have to manipulate the highlight result.

```auto
{
  "query": {
    "terms": {
      "text": [
        "movie",
        "cinema",
        "theater"
      ]
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}

```

Hits

```auto
"hits": [
      {
        "_index": "bar",
        "_id": "SzOHmYUBQB-6H-4ZDqaL",
        "_score": 1,
        "_source": {
          "text": "I went to the movie theater, which was down by the beach"
        },
        "highlight": {
          "text": [
            "I went to the <em>movie</em> <em>theater</em>, which was down by the beach"
          ]
        }
      }
    ]

```

---

<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 7, 2023, 2:38am UTC](https://discuss.elastic.co/t/how-do-i-get-find-which-parts-of-an-elasticsearch-query-match/322761/9 "2023-02-07T02:38:22Z")

</div>

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