Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change synonyms for ElasticSearch

My synonyms are stored in a database and, when the synonyms are changed in the database, I want to update any values in the index which may be changed as a result of the synonym change.

There are two parts to this that I can think of. One, figuring out which documents to re-index. Two, figuring out how to tell ElasticSearch that the synonyms have changed. I am struggling with the 2nd one - telling ElasticSearch that the synonyms have changed.

A similar question has been asked - see Change dynamically elasticsearch synonyms - but from reading the answers in that issue, I have not been able to figure out what I need.

Currently, my configuration file looks something like the following:

index :
  analysis :
    analyzer :
      myanalyzer :
        filter: [standard, mysynonymfilter]
filter :
  mysynonymfilter :      
    type : synonym
    synonyms : synonyms.txt
    ignore_case : false
    expand : true
    format : solr

My idea was to do something like the following:

curl -XPUT 'http://127.0.0.1:9200/foo/_settings'  -d '
{
    "filter" : {
        "synonym" : {
            "type" : "mysynonymfilter",
            "synonyms" : [
                "cosmos, universe"
            ] 
        }
    }
}
'

but that doesn't seem to do what I want. That is, the index settings do not get updated as far as I can tell.

Is what I am trying to do possible? And if so, any idea what I am doing wrong?

Also, I am fairly sure I could get this to work by updating the synonym file (if I have to use a file), but that's a bit more complicated and something I'd like to avoid.

Thanks for your help, Eric

like image 567
Eric Avatar asked Aug 27 '13 22:08

Eric


People also ask

How do I change Elasticsearch synonyms?

Updating synonyms is easier if you specify them in a file with the synonyms_path parameter. You can just update the file (on every node in the cluster) and then force the analyzers to be re-created by either of these actions: Closing and reopening the index (see open/close index), or.

How do you implement synonyms in Elasticsearch?

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. In the above settings i defined two analyzer for two different fields.


1 Answers

I know this is an old thread but as of ES 7.5 they have added a new feature to update synonyms. Have a look at their documentation.

You need to issue a POST api like this POST /twitter/_reload_search_analyzers

This would reload all the search analyzers, also ensure that the synonym token filter have the updateable flag set to true like this "updatedable": true.

PS: This feature is part of X-Pack and comes under the basic license which is free.

like image 169
Abhilash Bolla Avatar answered Sep 19 '22 05:09

Abhilash Bolla