The synonyms feature of Elasticsearch is very powerful and can significantly enhance your search engine’s efficiency when properly used. A common issue when using the synonyms feature is to update the synonyms set.
The synonyms defined inline in the settings of an index cannot be updated directly, and we need to close the index, update the settings, and re-open the index to make the changes effective. Another way is to use a synonyms file which can be updated by reloading the index. However, using an index file is difficult to manage when the Elasticsearch server is distributed or hosted in the cloud. This is because we need to put the file on all cluster nodes.
The good news is that there is a third way to do it now, which is much more convenient than the previous two. We can now use the synonyms APIs to manage synonyms. Even though it’s still a beta functionality of Elasticsearch at the time of writing, I think it will be adopted soon because this functionality is highly demanded by developers and can solve the tricky problem of updating synonyms sets very conveniently. We will explore the common usage of the synonyms APIs in this post.
Preparation
We will use the following docker-compose.yaml
file to start Elasticsearch and Kinana locally for demonstration.
version: "3.9"
services:
elasticsearch:
image: elasticsearch:8.11.1
environment:
- discovery.type=single-node
- ES_JAVA_OPTS=-Xms1g -Xmx1g
- xpack.security.enabled=false
ports:
- target: 9200
published: 9200
networks:
- elastickibana:
image: kibana:8.11.1
ports:
- target: 5601
published: 5601
depends_on:
- elasticsearch
networks:
- elastic
networks:
elastic:
name: elastic
driver: bridge
Note that you need to have at least version 8.10.0 of Elasticsearch to use the synonyms APIs. The latest version available would be the best as the feature should have become more mature then.