There
are different types of settings that can be done in an Elasticsearch index. One
among them is setting up our analyzers for a particular index.
Let
us discuss on how to add the list of analyzers to be used by fields in a type.
Add analyzer to
index using RESTFul service
PUT http://localhost:9200/simplyjava/_settings {
"analysis": {
"filter": {
"my_snowball_filter": {
"type"
: "snowball",
"language" : "English"
},
"my_synonym_filter": {
"type":
"synonym",
"synonyms_path":
"my_synonym_file.txt"
}
},
"tokenizer" : {
"comma" : {
"type" :
"pattern",
"pattern" : ","
}
},
"analyzer": {
"a1" : {
"type":"custom",
"tokenizer": "comma",
"filter": ["lowercase",
"my_synonym_filter"," my_snowball_filter "]
}
}
}
}
Add settings
using Java API
The name-value
pairs that are used in above rest call can be built as a json object using json
builder. This json object can be passed in the java API call.
XContentBuilder
settingsBuilder;
try
{
settingsBuilder =
XContentFactory.jsonBuilder()
.startObject()
.startObject("analysis")
.startObject("filter")
.startObject("my_synonym_filter")
.field("type","synonym")
.field("synonyms_path","my_synonym_file.txt")
.endObject()
.endObject()
.startObject("tokenizer")
.startObject("comma")
.field("type","pattern")
.field("pattern",",")
.endObject()
.endObject()
.startObject("analyzer")
.startObject("a1")
.field("type","custom")
.field("tokenizer","comma")
.array("filter","lowercase","my_snowball_filter","my_synonym_filter")
.endObject()
.endObject()
.endObject()
.endObject();
client.admin().indices().prepareCreate("simplyjava1").setSettings(settingsBuilder).get();
} catch
(IOException e) {
e.printStackTrace();
}
The
above piece of code will create index as well as map the settings to the
created index.
There
is a variety of analyzers available in Elasticsearch. We will be discussing that
in the upcoming topics.
Use
the following script to view the settings that have been created from executing
the snippet.
GET
http://localhost:9200/simplyjava/_settings
Note:
- Settings can be updated only when the index is closed, so make sure you close the index before updating it using REST service. Reopen the index once the settings are updated.
- Make sure all the files defined in the analyzers are present in config folder of elastic search server, before executing the settings.
Thanks !! Meet you soon with next
post !!
No comments:
Post a Comment