ElasticSearch: How to Programmatically Update Settings of an Existing Index
Are you unsure about programmatically updating the settings of an existing ElasticSearch index? Never fear, we've got you covered.
Join the DZone community and get the full member experience.
Join For FreeThe goal of this article is to show how to programmatically update the settings of an ElasticSeach index. Let's use ElaticSeachsynonyms as an example.
Imagine that for a specific index, you have the following synonyms settings:
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"ignore_case": true,
"synonyms": [
"Romania, RO",
"Belgium, BE"
]
}
},
....
Now, imagine (also) that you want to add a new entry into the synonyms lists (“France, FR”).
You could do this by using the ElasticSearch REST interface (please go here if you want to know more: Elasticsearch: updating the mappings and settings of an existing index) or you can use the Java API offered by ElasticSearch to do the same task programmatically:
//close the index before the update
client.admin().indices().close(new CloseIndexRequest(indexName));
//update the synonyms
client.admin().indices().prepareUpdateSettings(indexName).setSettings(
Settings.builder()
.put(
//setting prefix
"index.analysis.filter.synonym_filter",
//group name
"synonyms",
//settings
new String[] {"0", "1", "2"},
//values
new String []{
"Romania,RO",
"Belgium,BE",
"France,FR"
}
)
).get();
//open the index
client.admin().indices().open(new OpenIndexRequest(indexName));
Useful Links:
ElasticSearch Doc – Update Indices Settings
(Unofficial) ElasticSearch Java API for the IndicesAdminClieant interface
(Unofficial) ElasticSearch Java API for the ImmutableSettings.Builder.put method
Published at DZone with permission of Adrian CITU, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments