'How synonyms work internally in Elasticsearch?

I came across with Elasticsearch some time ago and started exploring it. I got to know about synonyms feature which is amazing! Can someone explain how internally this whole synonyms process work? How index time synonyms analyzing and search time synonyms analyzing are different in terms of internal structure?

Thanks :)



Solution 1:[1]

Elastic Doc:

Typically, the same parser should be applied at both index time and lookup time to ensure that the query terms are in the same format as the inverted index terms.

When you use the search_analyzer synonyms, you are generating the synonym tokens for the search term just in search time.

When you use synonyms at indexing time, you are expanding the term to the other terms of the synonyms, that is, everything is there in the inverted index. This can decrease your storage as you are indexing more term.

IndexTime example:

PUT synonym_index_time
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "synonyms_filter"
            ]
          }
        },
        "filter": {
          "synonyms_filter": {
            "type": "synonym",
            "lenient": true,
            "synonyms": [
              "laptop, notebook"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "synonym_analyzer"
      }
    }
  }
}

Test:

GET synonym_index_time/_analyze
{
  "field": "name",
  "text": ["laptop"]
}

Results:

{
  "tokens" : [
    {
      "token" : "laptop",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "notebook",
      "start_offset" : 0,
      "end_offset" : 6,
      "type" : "SYNONYM",
      "position" : 0
    }
  ]
}

Look, the terms laptop and notebook have been indexed, but notebook is a synonym.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andrecoelho.rabbitbr