# Using synonym\_graph means non-synonyms are not found

**URL:** <https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022>\
**Category:** Elasticsearch\
**Created:** [February 21, 2023, 4:44am UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022 "2023-02-21T04:44:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 21, 2023, 4:44am UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/1 "2023-02-21T04:44:18Z")

</div>

Hi, I have this analyzer

```auto
settings = {
    "analysis": {
        "analyzer": {
            "gale_analyzer": {
                "tokenizer": "standard",
                "filter": [
                    "lowercase",
                    "x_synonyms"
                ]
            }
        },
        "filter": {
            "x_synonyms": {
                "type": "synonym_graph",
                "synonyms_path": "dev_elastic_syn.csv",
                "updateable": True
            }
        }
    }
}

```

and it works great for a word that has a synonym in `dev_elastic_syn.csv` but for a word that is in a document but not in that file it isn't found. If I do the same search without the analyzer, it is found. Any ideas?

Here's the query

```auto
query={
                "match_phrase": {
                    "text": {
                        "query": ngram,
                        #"analyzer": "x_analyzer"
                    }
                }
            }

```

I have to comment out the analyzer to get it to find words not in `dev_elastic_syn.csv`.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 21, 2023, 8:22am UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/2 "2023-02-21T08:22:24Z")

</div>

What is the mapping for the indexed field that is queried?

---

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 21, 2023, 5:55pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/3 "2023-02-21T17:55:56Z")

</div>

I didn't set up an explicit mapping. There is only one other field that is like a serial number. I'll try setting up the mapping to see if that fixes it. Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 21, 2023, 6:04pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/4 "2023-02-21T18:04:48Z")

</div>

The mapping in the index will determine how your field is analysed and which tokens are indexed. This is by default the analyser used when querying so the query string is tokenised the same way as the indexed fields. If you specify a search time analyser you need to ensure the way this tokenises data matches with what you have indexed, or you will not get any matches.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 21, 2023, 6:12pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/5 "2023-02-21T18:12:53Z")

</div>

Thanks. I'm a little confused because I thought the synonyms were not supposed to be applied at index time, because that takes up too much space or something.

If I map a synonym analyzer then it will apply the synonyms at index time, right?

I remember reading that you could do synonyms at index or at query and it was better to do them at query. I guess I'm worried that if I map the an analyzer with synonyms it will apply it at index.

I'm having trouble keeping track of all of the moving parts. If you don't map, at query time it should apply the synonyms to both the query and each document, right?

This is separate from the "analyzer" vs "query\_analyzer" at the mapping. I don't see any reason to specify a different analyzer for querying for my use case. Thanks so much for your help.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 21, 2023, 6:20pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/6 "2023-02-21T18:20:40Z")

</div>

I may be wrong. To be sure I would recommend using the [analyze API](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/indices-analyze.html) to see how the indexed field is tokenised (check the mappings) and compare this when running the query string through the analyser that includes the synonym.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 21, 2023, 6:23pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/7 "2023-02-21T18:23:38Z")

</div>

Thanks. Yeah, I tried the analyzer API and everything seemed to be working fine.

Maybe I will try the mapping with the synonyms at index time (if that's how it works) and see if that helps. I've got plenty of space. If I need to update the synonym list I can just re-index. Thanks!

---

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 21, 2023, 6:24pm UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/8 "2023-02-21T18:24:30Z")

</div>

I'll report back and let you know if the matching fixed my problems. Thanks again!

---

<div class="post-metadata">

**Author:** ![Jonathan\_Mugan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_mugan/32/117501_2.png) [@Jonathan\_Mugan](https://discuss.elastic.co/u/Jonathan_Mugan)\
**Post date:** [February 22, 2023, 4:20am UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/9 "2023-02-22T04:20:43Z")

</div>

Yep, that worked, thanks!

---

<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 22, 2023, 4:21am UTC](https://discuss.elastic.co/t/using-synonym-graph-means-non-synonyms-are-not-found/326022/10 "2023-03-22T04:21:39Z")

</div>

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