Is it possible to store the synonyms for elasticsearch in the index? Or is it possible to get the synonym list from a database like couchdb? I'd like to add synonyms dynamically to elasticsearch via the REST-API.
There are two approaches when working with synonyms :
- expanding them at indexing time,
- expanding them at query time.
Expanding synonyms at query time is not recommended since it raises issues with :
- scoring, since synonyms have different document frequencies,
- multi-token synonyms, since the query parser splits on whitespaces.
More details on this at http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.SynonymFilterFactory (on Solr wiki, but relevant for ElasticSearch too).
So the recommended approach is to expand synonyms at indexing time. In your case, if the synonym list is managed dynamically, it means that you should re-index every document which contains a term whose synonym list has been updated so that scoring remains consistent between documents analyzed pre and post update. I'm not saying that it is not possible but it requires some work and will probably raise performance issues with synonyms which have a high frequency in your index.
There are few new solutions now to those proposed in other answers few years ago. The two main approaches implemented as plugins:
- The file-watcher-synonym filter is a plugin that can periodically reload synonyms every given numbers of seconds, as defined by user.
- The refresh-token-plugin allows a real-time update of the index. However, this plugin aparrently has some problems, which stem from the fact that elasticsearch is unable to distinguish between analyzers at search time only from those used at index time.
Good discussion on this subject can be found on the elastisearch github ticket system: https://github.com/brusic/refresh-token-filters
It isn't too painful in elasticsearch to update the synonym list. It can be done by opening and closing You could have it driven from anywhere, but need some of your own infrastructure. It'd work like this:
- You want an alias pointing at your current index
- Sync down a new index file to your servers
- Create a new index with a custom analyzer that uses the new index
- Rebuild the content from current index to new index
- Repoint index alias from current to new index
In 2021, just expand synonyms at query time using a specific search analyzer and use the Reload analyzer API:
POST /my-index/_reload_search_analyzers
The synonym graph token filter must have set updatable
to true
:
"my-synonyms": {
"type": "synonym_graph",
"synonyms_path": "my-synonyms.txt",
"updateable": true
}
Besides, you should probably expand synonyms at query time anyway. Why?
- Chances are that you have too much data to reindex every night or so.
- Elasticsearch does not allow the Synonym Graph Filter for an index analyzer, only the deprecated Synonym Filter which does not handle multi word synonyms correctly.
Update 2024:
Now you have two more options:
- The newest Elasticsearch versions have a Synonyms API.
- Querqy can use a "Common Rules rewriter" to configure synonyms and has an API to update the rewriters.
© 2022 - 2024 — McMap. All rights reserved.