Reputation: 465
I am using ElasticSearch 1.5.2 and I wish to have the following settings :
"settings": {
"analysis": {
"filter": {
"filter_shingle": {
"type": "shingle",
"max_shingle_size": 2,
"min_shingle_size": 2,
"output_unigrams": false
},
"filter_stemmer": {
"type": "porter_stem",
"language": "English"
}
},
"tokenizer": {
"my_ngram_tokenizer": {
"type": "nGram",
"min_gram": 1,
"max_gram": 1
}
},
"analyzer": {
"ShingleAnalyzer": {
"tokenizer": "my_ngram_tokenizer",
"filter": [
"standard",
"lowercase",
"filter_stemmer",
"filter_shingle"
]
}
}
}
}
Where should I add them? I mean before index creation or after?
By searching online I found some method like
client.admin().indices().prepareCreate("temp_index").setSettings(ImmutableSettings.settingsBuilder().loadFromSource((jsonBuilder()
.startObject()
.startObject("analysis")
.startObject("analyzer")......and so on)
But I am having 2 issues,
I get a compile error : The method loadFromSource in the type ImmutableSettings.builder is not applicable for the arguments XContentBuilder
Also, I don't know how to convert my settings to this format. And where is the documentation to get to know about all these methods? I tried reading the official ElasticSearch JAVA API https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/search.html but I was unable to find anything about setting up analyzers. All the related content I find is only in the form of REST APIs and not in Java API.
Upvotes: 3
Views: 5726
Reputation: 4489
XContentBuilder
have specific syntax , can be used to create a json object.
You can follow https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index_.html#helpers to convert your string to XContentBuilder
format.
If you have same string loaded in application. You simply can do.
client.admin().indices()
.prepareCreate("index_name").setSettings("{setting _ json _ string }").get()
Will simply work.
If you want to know how the json will look like in XContentBuilder, then here it is,
XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("analysis")
.startObject("filter")
.startObject("filter_shingle")
.field("type","shingle")
.field("max_shingle_size",2)
.field("min_shingle_size",2)
.field("output_unigrams",false)
.endObject()
.startObject("filter_stemmer")
.field("type","porter_stem")
.field("language","English")
.endObject()
.endObject()
.startObject("tokenizer")
.startObject("my_ngram_tokenizer")
.field("type","nGram")
.field("min_gram",1)
.field("max_gram",1)
.endObject()
.endObject()
.startObject("analyzer")
.startObject("ShingleAnalyzer")
.field("tokenizer","my_ngram_tokenizer")
.array("filter","standard","lowercase","filter_stemmer","filter_shingle")
.endObject()
.endObject()
.endObject()
.endObject()
client.admin().indices()
.prepareCreate("index_name").setSettings(settingsBuilder).get()
Upvotes: 4