# Confusion in using synonym token filter in elasticsearch 6.x

**URL:** <https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454>\
**Category:** Elasticsearch\
**Created:** [April 18, 2018, 4:38am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454 "2018-04-18T04:38:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishant.saini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nishant.saini/32/41489_2.png) [@nishant.saini](https://discuss.elastic.co/u/nishant.saini)\
**Post date:** [April 18, 2018, 4:38am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/1 "2018-04-18T04:38:30Z")

</div>

Synonym token in elasticsearch v2.4 supports tokenizer parameter. Hence token filter could have its own tokenizer (here "keyword") different from that is being used in custom analyser (here "whitespace") as in below setting:

```
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "my_synonym_filter": {
            "type": "synonym",
            "synonyms_path": "synonym.txt",
            "tokenizer": "keyword"
          }
        },
        "analyzer": {
          "my_synonyms": {
            "filter": [
              "lowercase",
              "my_synonym_filter"
            ],
            "tokenizer": "whitespace"
          }
        }
      }
    }
  }
}

```

How can I achieve the same elasticsearch v6.x?

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [April 18, 2018, 8:46am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/2 "2018-04-18T08:46:41Z")

</div>

Hi,

Can you explain more your use case?  
I'm not sure why "whitespace" is not good...

---

<div class="post-metadata">

**Author:** ![nishant.saini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nishant.saini/32/41489_2.png) [@nishant.saini](https://discuss.elastic.co/u/nishant.saini)\
**Post date:** [April 18, 2018, 9:35am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/3 "2018-04-18T09:35:26Z")

</div>

Hi,

The use case is when a term has multi-word synonym.  
For e.g. the following is synonym list:  
abc,xyz,lmn pqr

Now if the input string for the analyzer is _ **xyz** _ then the expected output after analysis should be the following terms:  
**abc**  
**xyz**  
**lmn pqr**  
But since we cannot specify tokenizer (_keyword_) for synonym filter anymore in elasticsearch 6.x the synonym **lmn pqr** get tokenised into two terms **lmn** and **pqr** which was not intended.

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [April 18, 2018, 10:29am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/4 "2018-04-18T10:29:41Z")

</div>

I can understand indexing phase, so still I'm not sure how you use "lmn pqr" token.  
Could you explain whole your use case? how do you search or what/how do you want to use "lmn pqr" term?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [April 18, 2018, 10:29am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/5 "2018-04-18T10:29:56Z")

</div>

For multi-words synonym you should not use the `synonym` filter but the `synonym_graph` which handles multi-words correctly:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-graph-tokenfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-graph-tokenfilter.html)  
This filter is designed to be used only at query time, query parsers are now able to detect multi-words synonym and they build a phrase query for them "lmn pqr" in your example:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-synonyms](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-synonyms)

---

<div class="post-metadata">

**Author:** ![nishant.saini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nishant.saini/32/41489_2.png) [@nishant.saini](https://discuss.elastic.co/u/nishant.saini)\
**Post date:** [April 19, 2018, 5:50am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/6 "2018-04-19T05:50:11Z")

</div>

What about terms aggregation? If I want doc count against the term: **lmn pqr** then I guess it won't work

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [April 19, 2018, 6:46am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/7 "2018-04-19T06:46:10Z")

</div>

For `terms` aggregation it won't work but if you want to make these multi terms a single keyword you can also change your synonym rule and apply it at index **and** query time:  
`abc,xyz,lmn pqr => abc,xyz,lmn_pqr`  
In this example the synonym terms will be `abc`, `xyz` and `lmn_pqr` so the `terms` aggregation would correctly return the count for the term `lmn_pqr`?

---

<div class="post-metadata">

**Author:** ![nishant.saini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nishant.saini/32/41489_2.png) [@nishant.saini](https://discuss.elastic.co/u/nishant.saini)\
**Post date:** [April 20, 2018, 3:46am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/8 "2018-04-20T03:46:34Z")

</div>

Thanks  
Let me try if this solution cater to my needs.

---

<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:** [May 18, 2018, 3:46am UTC](https://discuss.elastic.co/t/confusion-in-using-synonym-token-filter-in-elasticsearch-6-x/128454/9 "2018-05-18T03:46:57Z")

</div>

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