# Same synonyms in different synonym files

**URL:** <https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358>\
**Category:** Elasticsearch\
**Created:** [April 4, 2023, 10:37pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358 "2023-04-04T22:37:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![antoinelefloch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antoinelefloch/32/24577_2.png) [@antoinelefloch](https://discuss.elastic.co/u/antoinelefloch)\
**Post date:** [April 4, 2023, 10:37pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/1 "2023-04-04T22:37:38Z")

</div>

Hello,  
it seems synonyms in 2nd file are not taken into account if already used in 1st file.  
In 1st file, I have:  
aaa,bbb

In second file, I have:  
aaa,synaaa  
bbb,synbbb  
ccc,synccc

When I do the \_analyze

```auto
{
  "explain": false, 
  "analyzer": "synonym_full_indexing_analyzer",
  "text" : "ccc"
}

```

I get for:

- aaa --\> aaa, bbb (I am not getting synaaa)
- bbb --\> aaa, bbb (I am not getting synbbb)
- ccc --\> ccc, synccc

I wonder if it is related to that:

> <https://stackoverflow.com/questions/53536461/invalid-synonym-rule-when-using-two-files>

I didn't check the Elastic code.  
Here is the analyzer:

```auto
                "synonym_full_indexing_analyzer": {
                    "type": "custom",
                    "tokenizer": "fr_full_text_nospace_tokenizer",
                    "filter": [
                        "conditional_elision",
                        "lowercase",
                        "asciifolding",
                        "full_indexing_synonyms_syn1",
                        "full_indexing_synonyms_syn2",
                        "flatten_graph"
                    ]
                },

```

Elastic 7.17.6

---

<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:** [April 6, 2023, 12:07pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/2 "2023-04-06T12:07:28Z")

</div>

Hi @antoinelefloch

I did this example based to [post](https://www.elastic.co/blog/boosting-the-power-of-elasticsearch-with-synonyms/). Chaining synonym filters seems to be possible. I'm not sure what your filters are other than synonyms and maybe they are the real problem.

```auto
PUT /synonym_chaining
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "first_synonyms": {
            "type": "synonym",
            "synonyms": ["aaa,bbb"]
          },
          "second_synonyms": {
            "type": "synonym",
            "synonyms": ["aaa,synaaa","bbb,synbbb","ccc,synccc"]
          }
        },
        "analyzer": {
          "synonym_analyzer": {
            "filter": [
              "first_synonyms",
              "second_synonyms"
            ],
            "tokenizer": "whitespace"
          }
        }
      }
    }
  }
}
GET /synonym_chaining/_analyze
{
  "analyzer": "synonym_analyzer",
  "text": "bbb"
}

```

Tokens:

```auto
{
  "tokens": [
    {
      "token": "bbb",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 0
    },
    {
      "token": "synbbb",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 0
    },
    {
      "token": "aaa",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 0
    },
    {
      "token": "synaaa",
      "start_offset": 0,
      "end_offset": 3,
      "type": "SYNONYM",
      "position": 0
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![antoinelefloch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antoinelefloch/32/24577_2.png) [@antoinelefloch](https://discuss.elastic.co/u/antoinelefloch)\
**Post date:** [April 12, 2023, 5:40pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/4 "2023-04-12T17:40:49Z")

</div>

when I try to use simple settings, the second synonym file is not taken into account anymore.  
This is Elastic 7.7.1

```auto
PUT /synonym_chaining_files
{
    "settings": {
        "index": {
            "analysis": {
                "filter": {
                    "first_synonyms": {
                        "type": "synonym",
                        "synonyms_path": "resources/v1_02/my_synonyms_1.txt"
                    },
                    "second_synonyms": {
                        "type": "synonym",
                        "synonyms": "resources/v1_02/my_synonyms_2.txt"
                    }
                },
                "analyzer": {
                    "synonym_analyzer": {
                        "filter": [
                            "first_synonyms",
                            "second_synonyms"
                        ],
                        "tokenizer": "whitespace"
                    }
                }
            }
        }
    }
}

```

---

<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:** [April 12, 2023, 6:01pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/5 "2023-04-12T18:01:39Z")

</div>

> [@antoinelefloch](#):
>
> ```auto
> "second_synonyms": {
> "type": "synonym",
> "synonyms": "resources/v1_02/my_synonyms_2.txt"
> }
> 
> ```

try replace **synonyms** to **synonyms\_path**

---

<div class="post-metadata">

**Author:** ![antoinelefloch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antoinelefloch/32/24577_2.png) [@antoinelefloch](https://discuss.elastic.co/u/antoinelefloch)\
**Post date:** [April 13, 2023, 6:26pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/6 "2023-04-13T18:26:19Z")

</div>

It works much better, thank you 🙂

I found the reason of my problem. It is related to my analyzer which has filters typed "synonym\_graph" and not simply "synonym". Here are the filters to reproduce the problem:

```auto
  "filter": {
      "first_synonyms": {
          "type": "synonym_graph",
          "synonyms": ["aaa,bbb"],
          "lenient": "true"
      },
      "second_synonyms": {
          "type": "synonym_graph",
          "synonyms": ["aaa,synaaa","bbb,synbbb","ccc,synccc"],
          "lenient": "true"
      }
  }

```

If I remove "lenient", I get the error:  
"term: aaa analyzed to a token (aaa) with position increment != 1 (got: 0)"

So I suppose synonym\_graph typed filters do not support having the same synonym token in 2 different synonym filters.

---

<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:** [May 11, 2023, 6:27pm UTC](https://discuss.elastic.co/t/same-synonyms-in-different-synonym-files/329358/7 "2023-05-11T18:27:11Z")

</div>

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