# Different response of match and term query using synonyms

**URL:** <https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827>\
**Category:** Elasticsearch\
**Created:** [March 3, 2020, 7:56am UTC](https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827 "2020-03-03T07:56:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syed\_Saeed\_Ul\_Hassan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_saeed_ul_hassan/32/46276_2.png) [@Syed\_Saeed\_Ul\_Hassan](https://discuss.elastic.co/u/Syed_Saeed_Ul_Hassan)\
**Post date:** [March 3, 2020, 7:56am UTC](https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827/1 "2020-03-03T07:56:38Z")

</div>

Hi all,  
I have recently started working with elastic search. I read about synonyms and how **expansion** and **replacement** works in synonyms but when I perform a `match` query, resultant response seems like synonyms worked as expansions, and when I perform `term` query, resultant response matches the expectations of doing replacement.  
Below is my setting

```
    {
    "settings": {
        "index" : {
            "analysis" : {
                "filter" : {
                    "synonym_filter" : {
                        "type" : "synonym_graph",
                        "synonyms" : [
                            "cat => pet",
                            "dog => pet"
                        ],
                        "tokenizer": "keyword"
                    }
                },
                "analyzer" : {
                    "synonym_analyzer" : {
                        "tokenizer" : "standard",
                        "filter" : ["lowercase", "synonym_filter"] 
                    }
                }
            }
        }
    },
    "mappings": {
            "properties": {
              "title": { 
                "type": "text",
                "analyzer": "synonym_analyzer"
              }
        }
    }
}

```

I have indexed three documents with titles **cat** , **dog** , and **pet** respectively.

- When I search " **cat**" with `match` query, it returns **cat** along with **dog** and **pet**. And same response for all three documents when I search either of it.
- When I search " **cat**" with `term` query, it returns **cat** as expected
- When I search " **pet**" with `term` query, it returns all three i.e. **cat** , **dog** , and **pet** as expected.

**Is there any way I can perform match query where synonyms don't expand but replace like happening in term query?**

**What is preferred way of using synonyms with match query?**

**Why there is difference in both queries for same synonyms?**

**Term Query**

```
        {
            "explain": true,
            "query": {
                "bool": {
                    "filter": {
                        "term": {
                            "title": "cat"
                        }
                    }
                }
            }
        }

```

**Match Query**

```
        {
            "explain": true,
            "query": {
                "bool": {
                    "must": {
                        "match": {
                            "title": "cat"
                        }
                    }
                }
            }
        }

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Daniel\_Penning](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_penning/32/48792_2.png) [@Daniel\_Penning](https://discuss.elastic.co/u/Daniel_Penning)\
**Post date:** [March 3, 2020, 4:12pm UTC](https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827/2 "2020-03-03T16:12:44Z")

</div>

`match` query runs the analyzer while `term` query does not. So searching with a `match`query for `cat` is turned into `cat pet` by the analyzer. Configuring separate analyzer for indexing and search, where only one contains the synonym\_filter, should fix your problem.

---

<div class="post-metadata">

**Author:** ![Syed\_Saeed\_Ul\_Hassan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_saeed_ul_hassan/32/46276_2.png) [@Syed\_Saeed\_Ul\_Hassan](https://discuss.elastic.co/u/Syed_Saeed_Ul_Hassan)\
**Post date:** [March 3, 2020, 8:28pm UTC](https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827/3 "2020-03-03T20:28:14Z")

</div>

Thanks for the reply Daneil! Can you please explain it more through mapping?

---

<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:** [March 31, 2020, 8:28pm UTC](https://discuss.elastic.co/t/different-response-of-match-and-term-query-using-synonyms/221827/4 "2020-03-31T20:28:22Z")

</div>

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