# Problem with Search-time Synonyms

**URL:** <https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654>\
**Category:** Elasticsearch\
**Created:** [November 21, 2023, 4:32pm UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654 "2023-11-21T16:32:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elleWajexi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellewajexi/32/127875_2.png) [@elleWajexi](https://discuss.elastic.co/u/elleWajexi)\
**Post date:** [November 21, 2023, 4:32pm UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/1 "2023-11-21T16:32:03Z")

</div>

I have an index with synonyms :

```auto
"index": {
  "analysis": {
    "analyzer": {
      "index_analyzer": {
        "tokenizer": "standard",
        "filter": [
          "lowercase",
          "my_stemmer"
        ]
      },
      "search_analyzer": {
        "tokenizer": "standard",
        "filter": [
          "lowercase",
          "synonym_filter",
          "my_stemmer"
        ]
      }
    },
    "filter": {
      "synonym_filter": {
        "type": "synonym_graph",
        "synonyms_path": "/app/config/synonyms.txt",
        "updateable": True
      },
      "my_stemmer": {
        "type": "stemmer",
        "language": "light_english"
      }
    }
  }
}

```

with this mapping

```auto
"mappings": {
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "index_analyzer",
      "search_analyzer": "search_analyzer"
    }
 }

```

as you can see, I only use the synonyms filter in search-time.  
now let's say I have a synonym like this:  
`nana, grammy => grandma `  
now when I search for `nana` the search\_analyzer replaces `nana` with `grandma ` and only returns documents that contain the word `grandma` and it completely ignores `nana`.  
Do you have any suggestions on what I should do? Should I add synonyms to index-time?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 21, 2023, 5:21pm UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/2 "2023-11-21T17:21:12Z")

</div>

Hi @elleWajexi,

Welcome! Your specified synonym maps all of the terms on the left (in this case `nana` and `grammy` to `grandma`, which is why only `grandma` is searched for.

Have you tried using an equivalent synonym listing, `nana, grammy, grandma` which will expand out instead of map to `grandma`?

---

<div class="post-metadata">

**Author:** ![elleWajexi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellewajexi/32/127875_2.png) [@elleWajexi](https://discuss.elastic.co/u/elleWajexi)\
**Post date:** [November 21, 2023, 6:37pm UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/3 "2023-11-21T18:37:55Z")

</div>

I did try this, one problem I have with using equivalent synonyms is, using it with multi-word synonyms.  
for example in `nana, grammy, grandma, grand mother`  
when I search for `nana` returns these tokens:

```auto
{
  "tokens": [
    {
      "token": "nana",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "grammy",
      "start_offset": 0,
      "end_offset": 4,
      "type": "SYNONYM",
      "position": 0
    },
    {
      "token": "grandma",
      "start_offset": 0,
      "end_offset": 4,
      "type": "SYNONYM",
      "position": 0
    },
    {
      "token": "grand",
      "start_offset": 0,
      "end_offset": 4,
      "type": "SYNONYM",
      "position": 0
    },
    {
      "token": "mother",
      "start_offset": 0,
      "end_offset": 4,
      "type": "SYNONYM",
      "position": 1
    }
  ]
}

```

this way if there's a document that has multiple keywords of `grand` or `mother` could get a higher score and return irrelevant results.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 22, 2023, 10:33am UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/4 "2023-11-22T10:33:35Z")

</div>

Can you explain what result you want in the `grand` `mother` case? Should the additional synonym not be `grandmother` all one word?

---

<div class="post-metadata">

**Author:** ![elleWajexi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellewajexi/32/127875_2.png) [@elleWajexi](https://discuss.elastic.co/u/elleWajexi)\
**Post date:** [November 22, 2023, 3:21pm UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/5 "2023-11-22T15:21:52Z")

</div>

I want if someone searches for `grandmother` it also includes misspellings like `grand mother `

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 23, 2023, 10:04am UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/6 "2023-11-23T10:04:19Z")

</div>

Could you try a mapping synonym alongisde your expansion rule? Perhaps something like the below:

```auto
GET /_analyze
{
  "tokenizer": "standard",
  "filter" : [
    "lowercase",
    {
      "type": "synonym_graph",
      "synonyms": ["grand mother => nana, grammy, grandma, grandmother", "nana, grammy, grandma, grandmother"]
    }
  ],
  "text" : "Looking for my grand mother"
}

```

Resulting tokens:

```auto
{
  "tokens": [
    {
      "token": "looking",
      "start_offset": 0,
      "end_offset": 7,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "for",
      "start_offset": 8,
      "end_offset": 11,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "my",
      "start_offset": 12,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "nana",
      "start_offset": 15,
      "end_offset": 27,
      "type": "SYNONYM",
      "position": 3
    },
    {
      "token": "grammy",
      "start_offset": 15,
      "end_offset": 27,
      "type": "SYNONYM",
      "position": 3
    },
    {
      "token": "grandma",
      "start_offset": 15,
      "end_offset": 27,
      "type": "SYNONYM",
      "position": 3
    },
    {
      "token": "grandmother",
      "start_offset": 15,
      "end_offset": 27,
      "type": "SYNONYM",
      "position": 3
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![elleWajexi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellewajexi/32/127875_2.png) [@elleWajexi](https://discuss.elastic.co/u/elleWajexi)\
**Post date:** [November 24, 2023, 10:26am UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/7 "2023-11-24T10:26:46Z")

</div>

hi, thanks for the reply.  
I ended up doing the same thing, adding  
`"grand mother => nana, grammy, grandma, grandmother"` mapping.

the only issue with this is if someone searches for `grand mother` it's gonna ignore documents with `grand mother` in them to fix this I had to add  
`grand mother => nana, grammy, grandma, grandmother` to the index time too.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [November 24, 2023, 10:38am UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/8 "2023-11-24T10:38:07Z")

</div>

Glad you got something working in the end. Thanks for sharing you final approach @elleWajexi!

---

<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:** [December 22, 2023, 10:38am UTC](https://discuss.elastic.co/t/problem-with-search-time-synonyms/347654/9 "2023-12-22T10:38:53Z")

</div>

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