ijaxahmed
(Ijaz Ahmed)
July 28, 2022, 2:55pm
1
How to set index setting while creation using Elasticsearch v8 Java API client. I can able to set numberOfShards and numberOfReplicas using below syntax but could not able to set analysis due to confusing builder syntax.
elasticsearch.Client().indices()
.create(createIndexBuilder -> createIndexBuilder
.settings(builder->builder
.numberOfReplicas("3")
.numberOfShards("3")
.analysis(aBuilder->aBuilder.filter(TokenFilter)
.filter(Map.of("my_filter",
Map.of("type", "shingle",
"max_shingle_size", 3,
"min_shingle_size", 2,
"output_unigrams", false
))).analyzer())
.index("product")
I want to achieve these settings of the index
{
"number_of_replicas": 3,
"analysis": {
"normalizer": {
"my_lowercase": {
"filter": [
"lowercase"
],
"type": "custom"
}
},
"filter": {
"my_filter": {
"type": "shingle",
"output_unigrams": false,
"max_shingle_size": 3,
"min_shingle_size": 2
}
},
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"type": "custom",
"filter": [
"lowercase",
"my_filter"
]
}
}
},
"number_of_shards": 3
}
RabBit_BR
(andre.coelho)
July 28, 2022, 3:06pm
2
Hi @ijaxahmed
Maybe it's simpler for you to have the file with the setting and create the index from it.
But I don't know if this is an impediment for you.
ijaxahmed
(Ijaz Ahmed)
July 28, 2022, 3:23pm
3
@RabBit_BR , yes i created index through file, but file only has mapping information
elasticsearch.Client().indices()
.create(createIndexBuilder -> createIndexBuilder
.settings(builder->builder
.numberOfReplicas("3")
.numberOfShards("3")
.analysis(aBuilder->aBuilder.filter(TokenFilter)
.filter(Map.of("my_filter",
Map.of("type", "shingle",
"max_shingle_size", 3,
"min_shingle_size", 2,
"output_unigrams", false
))).analyzer())
.index("product")
.mappings(t-> t.withJson(this.getClass()
.getResourceAsStream(getJsonFromFile(pathToMapping))))
but i wanted to add setting through builder or json
RabBit_BR
(andre.coelho)
July 28, 2022, 3:28pm
4
The example below creates the index according to the setting you mentioned.
TokenFilterDefinition tokenFilterDefinition = new TokenFilterDefinition.Builder()
.shingle(ShingleTokenFilter.of(s ->
s.maxShingleSize("3").minShingleSize("2")
))
.build();
TokenFilter tokenFilter = new TokenFilter.Builder()
.definition(tokenFilterDefinition)
.build();
CustomAnalyzer customAnalyzer = new CustomAnalyzer.Builder()
.tokenizer("standard")
.filter("lowercase", "my_filter")
.build();
Analyzer analyzer = new Analyzer.Builder()
.custom(customAnalyzer)
.build();
IndexSettingsAnalysis indexAnalyzers = new IndexSettingsAnalysis.Builder()
.filter("my_filter", tokenFilter)
.analyzer(Map.of("my_analyzer", analyzer))
.build();
client.indices()
.create(createIndexBuilder -> createIndexBuilder
.settings(builder -> builder
.numberOfReplicas("3")
.numberOfShards("3")
.analysis(indexAnalyzers)
).index("test_analyzer"));
1 Like
ijaxahmed
(Ijaz Ahmed)
July 29, 2022, 3:42pm
5
Thank you so much @RabBit_BR , its a great help, Much appreciated
However the code is missing "normalizer", here i add to it
RabBit_BR:
TokenFilterDefinition tokenFilterDefinition = new TokenFilterDefinition.Builder()
.shingle(ShingleTokenFilter.of(s ->
s.maxShingleSize("3").minShingleSize("2")
))
.build();
TokenFilter tokenFilter = new TokenFilter.Builder()
.definition(tokenFilterDefinition)
.build();
CustomAnalyzer customAnalyzer = new CustomAnalyzer.Builder()
.tokenizer("standard")
.filter("lowercase", "my_filter")
.build();
Analyzer analyzer = new Analyzer.Builder()
.custom(customAnalyzer)
.build();
CustomNormalizer customNormalizer = new CustomNormalizer.Builder()
.filter("lowercase")
.build();
Normalizer normalizer = new Normalizer.Builder()
.custom(customNormalizer)
.build();
IndexSettingsAnalysis indexAnalyzers = new IndexSettingsAnalysis.Builder()
.filter("my_filter", tokenFilter)
.analyzer(Map.of("my_analyzer", analyzer))
.normalizer(Map.of("my_lowercase",normalizer))
.build();
client.indices()
.create(createIndexBuilder -> createIndexBuilder
.settings(builder -> builder
.numberOfReplicas("3")
.numberOfShards("3")
.analysis(indexAnalyzers)
).index("test_analyzer"));
1 Like
system
(system)
Closed
August 26, 2022, 3:43pm
6
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.