# How can I handle typos in synonyms?

**URL:** <https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141>\
**Category:** Elasticsearch\
**Created:** [May 23, 2023, 4:38pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141 "2023-05-23T16:38:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 23, 2023, 4:38pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/1 "2023-05-23T16:38:22Z")

</div>

I have synonyms in synonyms.txt - "auto, vehicle =\> car".

In index I have a document with string "car" and an analyzer to handle synonyms.

When you use "auto", for example, it will also return you results for "car". But when I have a typo in the synonym something like "vhicle" or "apto" it doesn't recognize the synonym and as a result original document value "car".

I tried to apply fuzziness, but it only applies to the original value that I have in the index and handles typos in "car" but not in synonyms. So either exact match for the synonym or a fuzzy query for the original string "car" works.

Here is my analyzer and the filter:

```auto
"analyzer": {
      "c_analyzer": {
        "tokenizer": "standard",
        "filter": ["lowercase", "synonym"]
      }
    },
    "filter": {
      "synonym": {
        "type": "synonym",
        "synonyms_path": "synonyms.txt"
      }
}

```

and then the query:

```auto
match: {
  word: {
    query: 'apto',
    operator: 'OR',
    fuzziness: 'auto',
    boost: 9,
    analyzer: 'c_analyzer'
  },
}

```

Can't find any info so really sad about it 😅 So any help would be useful.

---

<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:** [May 23, 2023, 7:21pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/2 "2023-05-23T19:21:23Z")

</div>

Hi @gennadii

One solution is to index all synonym terms like "auto" and "vehicle". That way you can use Fuzzy.

```auto
PUT idx_synonyms
{
  "mappings": {
    "properties": {
      "store": {
        "type": "text",
        "analyzer": "synonym"
      }
    }
  }, 
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "standard",
            "filter": ["lowercase", "synonym"]
          }
        },
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms": ["auto, vehicle, car"]
          }
        }
      }
    }
  }
}

POST idx_synonyms/_doc
{
  "store": "car"
}

```

Query

```auto
GET idx_synonyms/_search
{
  "query": {
    "match": {
      "store": {
        "query": "apto",
        "fuzziness": "AUTO"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 23, 2023, 8:29pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/3 "2023-05-23T20:29:42Z")

</div>

Thank you for the answer. Works perfect. But for some reasons it doesn't work when trying to apply synonyms from file:

```auto
"filter": {
  "synonym": {
    "type": "synonym",
    "synonyms_path": "synonyms.txt"
  }
}

```

Synonyms itself work when loading them from file, but the query part doesn't apply fuzziness on a synonym.

---

<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:** [May 23, 2023, 8:49pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/4 "2023-05-23T20:49:14Z")

</div>

Did you change the data in the synonym file?

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 23, 2023, 8:51pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/5 "2023-05-23T20:51:34Z")

</div>

I updated the file to have a format:

```auto
car, auto => vehicle

```

and uploaded it as an update of bundle to elastic

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 23, 2023, 9:50pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/6 "2023-05-23T21:50:03Z")

</div>

UPD: with this solution there is also another issue. If in the index I have "cars" and use "auto" in query it can't find "cars". How can it be solved? So I want to find something with the synonym even if there is a typo/another form of the word in the synonym or in the original document.

---

<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:** [May 23, 2023, 10:34pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/7 "2023-05-23T22:34:46Z")

</div>

> [@gennadii](#):
>
> `car, auto => vehicle`

I believe this way will not work.

> [@gennadii](#):
>
> UPD: with this solution there is also another issue. If in the index I have "cars" and use "auto" in query it can't find "cars". How can it be solved? So I want to find something with the synonym even if there is a typo/another form of the word in the synonym or in the original document.

Add a [stemmer filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-stemmer-tokenfilter.html) in the language you want, in this example English. That way you get car and cars.

```auto
analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "standard",
            "filter": [
              "stemmer",
              "lowercase",
              "synonym"
            ]
          }
        },
        "filter": {
          "my_stemmer": {
            "type": "stemmer",
            "language": "english"
          },
          "synonym": {
            "type": "synonym",
            "synonyms": [
              "auto, vehicle, car"
            ]
          }
        }
      }

```

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 23, 2023, 10:41pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/8 "2023-05-23T22:41:44Z")

</div>

> [@RabBit\_BR](#):
>
> ```auto
> "my_stemmer": {
> "type": "stemmer",
> "language": "english"
> },
> 
> ```

Great, thank you so much. The only thing is I am struggling with is typos. If you have let's say in index "vhicles" and in query "cars", it will not find "vhicles".

---

<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:** [May 23, 2023, 11:34pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/9 "2023-05-23T23:34:51Z")

</div>

> [@gennadii](#):
>
> "vhicles"

do you index a wrong word?

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 24, 2023, 5:53am UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/10 "2023-05-24T05:53:04Z")

</div>

Yes, typo should be handled on both sides. So if I have "vhicles" in index and a typo "aptos" in query, it should first find the synony, "auto", then convert it to "vehicle" and then use something like fuzzy search to find "vhicles". At least that is how I built it in my head 😄 Is it even possible?

---

<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:** [May 24, 2023, 1:00pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/11 "2023-05-24T13:00:09Z")

</div>

For me that doesn't make sense. Index misspelled words? As I showed earlier, you can index the synonyms and use fuzzy when the search term is wrong.

---

<div class="post-metadata">

**Author:** ![gennadii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gennadii/32/119721_2.png) [@gennadii](https://discuss.elastic.co/u/gennadii)\
**Post date:** [May 24, 2023, 1:38pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/12 "2023-05-24T13:38:02Z")

</div>

A user can create a record and this record goes to the index. That is why it can contain a typo. So "vhicle" in index should be found with a query "car", the same way as "car" in index with query "vhicle"

---

<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:** [May 24, 2023, 2:54pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/13 "2023-05-24T14:54:38Z")

</div>

> [@gennadii](#):
>
> A user can create a record and this record goes to the index. That is why it can contain a typo. So "vhicle" in index should be found with a query "car", the same way as "car" in index with query "vhicle"

this sounds very strange to me. I would not index spelling errors, what you want from my point of view is not correct (you index the wrong term and want to use fuzzy to match the right term with the wrong term that is indexed).  
I would review this requirement.

---

<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:** [June 21, 2023, 2:54pm UTC](https://discuss.elastic.co/t/how-can-i-handle-typos-in-synonyms/334141/14 "2023-06-21T14:54:55Z")

</div>

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