# Elasticsearch v8 Java API client adding index settings

**URL:** <https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888>\
**Category:** Elasticsearch\
**Tags:** reindex\
**Created:** [July 28, 2022, 2:55pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888 "2022-07-28T14:55:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)\
**Post date:** [July 28, 2022, 2:55pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/1 "2022-07-28T14:55:49Z")

</div>

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.

```auto
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

```auto
{
"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

}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 28, 2022, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/2 "2022-07-28T15:06:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)\
**Post date:** [July 28, 2022, 3:23pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/3 "2022-07-28T15:23:47Z")

</div>

@RabBit_BR , yes i created index through file, but file only has mapping information

```auto
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

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 28, 2022, 3:28pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/4 "2022-07-28T15:28:02Z")

</div>

The example below creates the index according to the setting you mentioned.

```auto

    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"));

```

---

<div class="post-metadata">

**Author:** ![ijaxahmed](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@ijaxahmed](https://discuss.elastic.co/u/ijaxahmed)\
**Post date:** [July 29, 2022, 3:42pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/5 "2022-07-29T15:42:32Z")

</div>

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](#):
>
> ```auto
> 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"));
> 
> ```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 26, 2022, 3:43pm UTC](https://discuss.elastic.co/t/elasticsearch-v8-java-api-client-adding-index-settings/310888/6 "2022-08-26T15:43:07Z")

</div>

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