Dev Sush
Dev Sush

Reputation: 23

How I can implement synonyms in elastic search?

I want to implement synonyms in my mapping. I have created parent-child mapping. Here is my mapping.

{  
   "mapping":{  
      "mappings":{  
         "question_data":{  
            "properties":{  
               "question_id":{  
                  "type":"integer"
               },
               "question":{  
                  "type":"string"
               }
            }
         },
         "answer_data":{  
            "_parent":{  
               "type":"question_data"
            },
            "_routing":{  
               "required":true
            },
            "properties":{  
               "answer_id":{  
                  "type":"integer"
               },
               "answer":{  
                  "type":"string",

               }
            }
         }
      }
   }
}

Thanks in advance.

Upvotes: 0

Views: 884

Answers (1)

user3775217
user3775217

Reputation: 4803

To use synonyms in elasticsearch you have to first create a synonym analyzer in settings to add synonym support for a particular field. Also in the settings you can define synonyms also.

PUT testindex_510
{
    "settings": {
        "analysis": {
            "analyzer": {
                "synonymanalyzer": {
                    "tokenizer": "standard",
                    "filter": ["lowercase", "locationsynfilter"]
                },
                "synonymanalyzer1": {
                    "tokenizer": "standard",
                    "filter": ["lowercase", "titlesynfilter"]
                }
            },
            "filter": {
                "locationsynfilter": {
                    "type": "synonym",
                    "synonyms": [
                        "lokhandwala,andheri west",
                        "versova,andheri west",
                        "mazgaon,byculla"
                    ]
                },
                "titlesynfilter": {
                    "type": "synonym",
                    "synonyms": [
                        "golds , gold",
                        "talwalkars, talwalkar"
                    ]
                }
            }

        }

    },
    "mappings": {
        "testtype": {
            "properties": {
                "title": {
                    "type": "string",
                    "analyzer": "synonymanalyzer1"
                },
                "location": {
                    "type": "string",
                    "analyzer": "synonymanalyzer"
                }
            }
        }
    }
}

In the above settings i defined two analyzer for two different fields. These analyzers support synonms and also define the synonyms in the filter for each analyzer.

You also define synonyms in a seperate txt file instead of defining in the mappings like the following one.

{
    "settings": {
        "analysis": {
            "analyzer": {
                "synonymanalyzer": {
                    "tokenizer": "standard",
                    "filter": ["lowercase", "locationsynfilter"]
                },
                "synonymanalyzer1": {
                    "tokenizer": "standard",
                    "filter": ["lowercase", "titlesynfilter"]
                }
            },
            "filter": {
                "titlesynfilter": {
                    "type": "synonym",
                    "synonyms_path": "analysis/titlesynonym.txt"
                },
                "locationsynfilter": {
                    "type": "synonym",
                    "synonyms_path": "analysis/locationsynonym.txt"
                }
            }

        }

    },
    "mappings": {
        "testtype": {
            "properties": {
                "title": {
                    "type": "string",
                    "analyzer": "synonymanalyzer1"
                },
                "location": {
                    "type": "string",
                    "analyzer": "synonymanalyzer"
                }
            }
        }
    }
}

where your txt file should look like. Please refer documentation for more configuration.

ipod, i-pod, i pod
foozball , foosball
universe , cosmos

Hope this helps

Upvotes: 2

Related Questions