# How to generate shingles before synonyms filter?

**URL:** <https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235>\
**Category:** Elasticsearch\
**Created:** [August 17, 2022, 2:32am UTC](https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235 "2022-08-17T02:32:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattkallo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattkallo/32/109746_2.png) [@mattkallo](https://discuss.elastic.co/u/mattkallo)\
**Post date:** [August 17, 2022, 2:32am UTC](https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235/1 "2022-08-17T02:32:17Z")

</div>

Hi,

I understand we can't have token filters that produce multiple tokens before a synonyms filter. ([Ensure TokenFilters only produce single tokens when parsing synonyms by romseygeek · Pull Request #34331 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/34331)).

I was trying to use a shingles filter to generate combination of words before using the synonyms filter. But thats not working. Can you suggest an alternate solution for the below scenario?

(I already have a phrase list filter which takes a predefined list of phrases from a file but that doesn't cover all the phrases. Wanted a more dynamic solution)

Synonyms

> "heart attack", "cardiac arrest"

Input text

> "Symptoms of heart attack"

"heart", "attack", "cardiac" and "arrest" should not get mapped their respective synonyms individually, instead only the combination of "heart attack" should get mapped to "cardiac arrest  
"

Pls let me know the best way to achieve this.

---

<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:** [August 17, 2022, 6:40pm UTC](https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235/2 "2022-08-17T18:40:40Z")

</div>

Hi @mattkallo.

Look this example, maybe help you to find the solution

```auto
PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "synonyms_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "synonyms_filter",
            "shingle_filter"
          ]
        }
      },
      "filter": {
        "synonyms_filter": {
          "type": "synonym",
          "synonyms": [
            "heart attack => cardiac arrest"
          ]
        },
        "shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 3,
          "output_unigrams": false
        }
      }
    }
  }
}

GET test/_analyze
{
  "analyzer": "synonyms_analyzer",
  "text": ["heart attack"]
}

```

Tokens:

```auto
{
  "tokens" : [
    {
      "token" : "cardiac arrest",
      "start_offset" : 0,
      "end_offset" : 12,
      "type" : "shingle",
      "position" : 0
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![mattkallo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattkallo/32/109746_2.png) [@mattkallo](https://discuss.elastic.co/u/mattkallo)\
**Post date:** [August 17, 2022, 10:56pm UTC](https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235/3 "2022-08-17T22:56:32Z")

</div>

Thanks @RabBit_BR for the response. I tried this approach, won't this create invalid (contextually) combinations? For eg. In the above case, if "heart" is mapped to its synonyms and "attack" mapped to its own set of synonyms, and using synonyms filter before shingle will output contextually invalid combinations. Am I wrong in this observation?

---

<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:** [September 14, 2022, 10:57pm UTC](https://discuss.elastic.co/t/how-to-generate-shingles-before-synonyms-filter/312235/4 "2022-09-14T22:57:26Z")

</div>

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