How to add Synonyms list to the current index settings

Consider the scenario when you want to extend an existing instance's synonyms list. Let's say you wish to provide a list of English language synonyms. You may get a list of synonyms for multiple languages using this Git repository.

Use the steps below to add this synonyms list to the index setting.

  • Close read/write operations on the index by closing the index using the API listed below.
POST /my-index/_close

Utilize the following API to update the index settings.

PUT /my-index/_settings
{
    "index": {
        "analysis": {
            "filter": {
                "synonym_file": {
                    "type": "synonym_graph",
                    "synonyms_path": "english_synonyms.txt"
                }
            },
            "analyzer": {
                "generic_analyzer": {
                    "filter": [
                        "english_possessive_stemmer",
                        "lowercase",
                        "english_stop",
                        "english_keywords"
                    ],
                    "tokenizer": "standard"
                },
                "english_analyzer": {
                    "filter": [
                        "synonym_file",
                        "english_possessive_stemmer",
                        "lowercase",
                        "english_stop",
                        "english_keywords",
                        "english_stemmer"
                    ],
                    "tokenizer": "standard"
                }
            }
        }
    }
}

In order to use the newly created, english_analyzer update the index mapping.

PUT /my-index/_mappings
{
    "properties":
    {
        "sentences":
        {
            "properties":
            {
                "description":
                {
                    "type": "text",
                    "analyzer": "english_analyzer"
                }
            }
        }
    }
}

You can now open the index after updating the index mapping.

POST /my-index/_open

Using the Analyze API listed below, you may determine whether or not the modifications were correctly implemented.

GET my-index/_analyzer
{
  "text": "news",
  "analyzer": "english_analyzer"
}

You will receive the news word's synonyms in response. The synonyms that are retrieved are depending on the synonym list that was added to the index setting.