Elasticsearch v8 Java API client adding index settings

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

}

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.

@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

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

Thank you so much @RabBit_BR , its a great help, Much appreciated

However the code is missing "normalizer", here i add to it

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.