# How does synonym filter work in search phase

**URL:** <https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401>\
**Category:** Elasticsearch\
**Created:** [September 13, 2018, 3:25am UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401 "2018-09-13T03:25:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![merlinv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merlinv/32/32021_2.png) [@merlinv](https://discuss.elastic.co/u/merlinv)\
**Post date:** [September 13, 2018, 3:25am UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/1 "2018-09-13T03:25:36Z")

</div>

Hi there,  
I create a sample synonym filter like below

```
PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "standard",
                        "filter" : ["synonym"]
                    }
                },
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "synonyms" : ["girl, woman"]
                    }
                }
            }
        }
    },
    "mappings": {
        "items": {
            "properties": {
               "tag": {
                  "type":"text",
                  "analyzer": "synonym"
               }
            }
        }
    }
}

```

So when I search for `girl`, I can get result of items which contain both `girl` and `woman` tag.  
Also when using `_analyze` API to test:

```
GET test-index/_analyze
{
  "analyzer": "synonym",
  "text": ["girl"]
}

```

I get 2 tokens: one is `girl` as type and `woman` as SYNONYM type.

Hence I wonder that when I search for `girl`, it will equal to search for `(tag: girl AND tag:woman)` or `(tag:girl OR tag:woman)`

And during index phase, is `woman` added to inverted index when indexing document contains `girl` tag ?

The last question is when I update the synonym list by adding `man, boy` synonyms and without reindex all of my documents, I still can get result of `boy` when searching for `man`.  
Is that only applied for search phase with query like search `(tag:man OR tag:boy)` ?

Thanks,

---

<div class="post-metadata">

**Author:** ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)\
**Post date:** [September 13, 2018, 3:52pm UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/2 "2018-09-13T15:52:42Z")

</div>

Hi Merlin,

1. For this part:

> The last question is when I update the synonym list by adding `man, boy` synonyms and without reindex all of my documents, I still can get result of `boy` when searching for `man` .  
> Is that only applied for search phase with query like search `(tag:man OR tag:boy)` ?

Without a re-indexation, you cant search for that data since it is not added to the index.

1. Question:

> Hence I wonder that when I search for `girl` , it will equal to search for `(tag: girl AND tag:woman)` or `(tag:girl OR tag:woman)`

It will be as equal to an OR statement. If you need some customizations or more in depth scoring, you can use search synonyms instead of during indexing a document.

Greetings,  
Peter

---

<div class="post-metadata">

**Author:** ![merlinv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merlinv/32/32021_2.png) [@merlinv](https://discuss.elastic.co/u/merlinv)\
**Post date:** [September 14, 2018, 1:42am UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/3 "2018-09-14T01:42:34Z")

</div>

Hi Peter,

> Without a re-indexation, you cant search for that data since it is not added to the index.

I want to confirm:  
Let say I updated synonym list by adding `man, boy` without reindexing,  
then when searching `man`, in search phase it will equal to `(tag:man OR tag:boy) ` (1).  
It didn't add `boy` to inverted index as synonym since there is no re-indexation (2)

But because of (1) OR statement, so the search result of `man` still contain `boy` data. (3)  
Is it true ? Cuz in fact, I did test the above flow on ElasticCloud and I got result (3) for real.

Thanks,

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [September 26, 2018, 1:51pm UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/4 "2018-09-26T13:51:07Z")

</div>

Hi @merlinv,

I think your understanding is right, let me just try to clarify with a short example. Lets assume we set up the test index like this:

```auto
DELETE /test_index

PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "standard",
                        "filter" : ["synonym"]
                    }
                },
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "synonyms" : ["girl, woman"]
                    }
                }
            }
        }
    },
    "mappings": {
        "_doc": {
            "properties": {
               "tag": {
                  "type":"text",
                  "analyzer": "synonym"
               }
            }
        }
    }
}

PUT /test_index/_doc/1
{
  "tag" : "a girl"
}

PUT /test_index/_doc/2
{
  "tag" : "a woman"
}

PUT /test_index/_doc/3
{
  "tag" : "a man"
}

PUT /test_index/_doc/4
{
  "tag" : "a boy"
}

```

At this point, document 1 and 2 should have been indexed with synonyms, so a match query on either "woman" or "girl" should return both documents. If you try the same with "man" and "boy" you should only get one of the documents 3 and 4 back:

```auto
POST /test_index/_doc/_search
{
  "query" : {
    "match" : {
        "tag" : "girl"
    }
  }
}

```

In addition to that, if we use a `term` query, which isn't analysed (so the synonyms are not applied to the query at search time) we also get both documents 1 and 2, because in fact both tokens were indexed with the document

```auto
GET /test_index/_search
{
  "query": {
    "term": {
      "tag": {
        "value": "girl"
      }
    }
  }
}

```

If we now change the filter and add the second synonym rule:

```auto
POST /test_index/_close

PUT /test_index/_settings
{
    
        "index" : {
            "analysis" : {
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "synonyms" : ["girl, woman", 
                            "boy, man"]
                    }
                }
            }
        }
}

POST /test_index/_open

```

Now a `match` query will return both documents 3 and 4, even without reindexing. This is because the synonyms filter gets applied to the query at search time and expands it to both "boy" and "man" and searches for both:

```auto
POST /test_index/_doc/_search
{
  "query" : {
    "match" : {
        "tag" : "boy"
    }
  }
}

```

In contrast, the `term`query (again, no synonym expansion here) will still only return one of the two documents, because in fact only one term got indexed (the additional rule wasn't in effect yet):

```auto
GET /test_index/_search
{
  "query": {
    "term": {
      "tag": {
        "value": "man"
      }
    }
  }
}

```

If we now index another 5th document:

```auto
PUT /test_index/_doc/5
{
  "tag" : "another boy"
}

```

this one now gets indexed using the newly updated synonym rule, so now also the `term` query will return it, regardless of you search for the term "man" or "boy".

```auto
GET /test_index/_search
{
  "query": {
    "term": {
      "tag": {
        "value": "man"
      }
    }
  }
}

```

Internally documents 3 and 4 will only have either "man" or "boy" indexed, wheras the newly indexed document contains both "boy" and "man" in the inverted index structure.

I hope this confirms and clarifies what was explained above.

---

<div class="post-metadata">

**Author:** ![merlinv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merlinv/32/32021_2.png) [@merlinv](https://discuss.elastic.co/u/merlinv)\
**Post date:** [September 27, 2018, 2:28am UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/5 "2018-09-27T02:28:07Z")

</div>

Hi @cbuescher,

I understood.  
Thank you for the detailed explanation.

---

<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:** [October 25, 2018, 2:28am UTC](https://discuss.elastic.co/t/how-does-synonym-filter-work-in-search-phase/148401/6 "2018-10-25T02:28:10Z")

</div>

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