1. Overview

In this article, we will discuss create, update index settings using Elastic Search Transport Client Java API. We can create Index settings and set some parameters like number of shards, number of replicas and some other parameters based on our requirements.

2. Development Environment

Elastic Search: 6.6.0

Java: 1.8.0_65

IDE: IntelliJ Idea

Build Tool: Maven

3. Create Index Settings

3.1 Create Index 

CreateIndexRequest class is used to create Index. It takes index name as an argument as below code snippets.

CreateIndexRequest request = new CreateIndexRequest("indexsettingexample");

3.2 Create Index with settings

Elastic Search Transport Client Java API provide  Settings class to create Index with some custom settings like max_inner_result_window, number_of_shards, number_of_replicas etc…

3.2.1 Example

public void createSettings() throws ExecutionException, InterruptedException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(Settings.builder()
            .put("index.max_inner_result_window", 250)
            .put("index.write.wait_for_active_shards", 1)
            .put("index.query.default_field", "paragraph")
            .put("index.number_of_shards", 3)
            .put("index.number_of_replicas", 2)
    );
    CreateIndexResponse createIndexResponse = client.admin().indices().create(request).get();
    System.out.println("Index : " + createIndexResponse.index() + " Created");
    getSettings(); //call get settings method to print current index settings
}

3.2.2 Output

Index : indexsettingexampleone1550596328824 Created
***************Get Settings *********************
index.max_inner_result_window : 250
index.write.wait_for_active_shards : 1
index.query.default_field : paragraph
index.number_of_shards : 3
index.number_of_replicas : 2

3.3 Create Index with Analyzers

Elastic Search provides facility to create custom analyzers and filters to tokenize our text based on our requirements. We can provide a list of analyzers, filters under analysis using XContentBuilder or via a JSON source. Here is the example of creating Analyzers using XContentBuilder.  Strings class is used to convert XContentBuilder to JSON string.

3.3.1 Example

public void createSettingsWithAnalyzer() throws ExecutionException, InterruptedException, IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(Settings.builder()
            .put("index.max_inner_result_window", 250)
            .put("index.write.wait_for_active_shards", 1)
            .put("index.query.default_field", "paragraph")
            .put("index.number_of_shards", 3)
            .put("index.number_of_replicas", 2)
            .loadFromSource(Strings.toString(jsonBuilder()
                    .startObject()
                        .startObject("analysis")
                            .startObject("analyzer")
                                .startObject("englishAnalyzer")
                                    .field("tokenizer", "standard")
                                    .field("char_filter", "html_strip")
                                    .field("filter", new String[]{"snowball", "standard", "lowercase"})
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject()), XContentType.JSON)
            );
    CreateIndexResponse createIndexResponse = client.admin().indices().create(request).get();
    System.out.println("Index : "+createIndexResponse.index()+" Created");
    getSettingsWithAnalyzer();
}

3.3.2 Output

Index : indexsettingexampleone1550597968916 Created
***************Get Settings with Analyzers *********************
index.analysis.analyzer.englishAnalyzer.char_filter : html_strip
index.analysis.analyzer.englishAnalyzer.filter : [snowball, standard, lowercase]
index.analysis.analyzer.englishAnalyzer.tokenizer : standard
index.creation_date : 1550597971180
index.max_inner_result_window : 250
index.number_of_replicas : 2
index.number_of_shards : 3
index.provided_name : indexsettingexampleone1550597968916
index.query.default_field : paragraph
index.uuid : yrkatoioSs-gIliGIQgQyA
index.version.created : 6060099
index.write.wait_for_active_shards : 1

3.4 Create Index Settings using JSON Source

Another option to create Analyzer using JSON source. Refer below code snippets for more details.

3.4.1 Example

public void createSettingsWithAnalyzerJSONSource() throws ExecutionException, InterruptedException, IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.source("{\n" +
            "        \"settings\":{\n" +
            "            \"index\": {\n" +
            "                \"analysis\": {\n" +
            "                    \"normalizer\": {\n" +
            "                        \"lowercaseNormalizer\": {\n" +
            "                            \"type\": \"custom\",\n" +
            "                            \"char_filter\": [],\n" +
            "                            \"filter\": [\"lowercase\"]\n" +
            "                        }\n" +
            "                    },\n" +
            "                    \"analyzer\": {\n" +
            "                        \"englishAnalyzer\": {\n" +
            "                            \"tokenizer\": \"standard\",\n" +
            "                            \"char_filter\": [\n" +
            "                                \"html_strip\"\n" +
            "                            ],\n" +
            "                            \"filter\" : [\n" +
            "                                \"standard\",\n" +
            "                                \"lowercase\",\n" +
            "                                \"trim\"\n" +
            "                            ]\n" +
            "                        }\n" +
            "                    },\n" +
            "                    \"filter\" : {\n" +
            "                        \"snowballStemmer\": {\n" +
            "                            \"type\": \"snowball\",\n" +
            "                            \"language\": \"english\"\n" +
            "                        }\n" +
            "                    }\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "    }",XContentType.JSON);
    CreateIndexResponse createIndexResponse = client.admin().indices().create(request).get();
    System.out.println("Index : "+createIndexResponse.index() + " Created.");
    getSettingsWithAnalyzer();
}

3.4.2 Output

Index : indexsettingexampleone1550598724119 Created.
***************Get Settings with Analyzers *********************
index.analysis.analyzer.englishAnalyzer.char_filter : [html_strip]
index.analysis.analyzer.englishAnalyzer.filter : [standard, lowercase, trim]
index.analysis.analyzer.englishAnalyzer.tokenizer : standard
index.analysis.filter.snowballStemmer.language : english
index.analysis.filter.snowballStemmer.type : snowball
index.analysis.normalizer.lowercaseNormalizer.char_filter : []
index.analysis.normalizer.lowercaseNormalizer.filter : [lowercase]
index.analysis.normalizer.lowercaseNormalizer.type : custom
index.creation_date : 1550598726356
index.number_of_replicas : 1
index.number_of_shards : 5
index.provided_name : indexsettingexampleone1550598724119
index.uuid : u2ZhfrjkTPevIt1DqMT0WA
index.version.created : 6060099

4. Get Index Settings

4.1 Get Index with settings

GetSettingsRequest and GetSettingsResponse classes are used to get all the index settings. Refer below example of api usage.

4.1.1 Example

public void getSettings() throws ExecutionException, InterruptedException {
        GetSettingsRequest getSettingsRequest = new GetSettingsRequest();
        GetSettingsResponse indexResponse = client.admin().indices().getSettings(getSettingsRequest).get();
        Settings settings = indexResponse.getIndexToSettings().get(indexName);
        System.out.println("index.max_inner_result_window : "+settings.get("index.max_inner_result_window"));
        System.out.println("index.write.wait_for_active_shards : "+settings.get("index.write.wait_for_active_shards"));
        System.out.println("index.query.default_field : "+settings.get("index.query.default_field"));
        System.out.println("index.number_of_shards : "+settings.get("index.number_of_shards"));
        System.out.println("index.number_of_replicas : "+settings.get("index.number_of_replicas"));
    }

4.1.2 Output

***************Get Settings *********************
index.max_inner_result_window : 250
index.write.wait_for_active_shards : 1
index.query.default_field : paragraph
index.number_of_shards : 3
index.number_of_replicas : 2

4.2 Get Index Analyzers

Here is another example of retrieving all the index settings including analyzers, filters, char_filters etc..

4.2.1 Example

public void getSettingsWithAnalyzer() throws ExecutionException, InterruptedException {
        GetSettingsRequest getSettingsRequest = new GetSettingsRequest();
        GetSettingsResponse indexResponse = client.admin().indices().getSettings(getSettingsRequest).get();
        Settings settings = indexResponse.getIndexToSettings().get(indexName);

        for(String key : settings.keySet()){
            System.out.println(key+" : "+settings.get(key));
        }
}

4.2.1 Output

***************Get Settings with Analyzers *********************
index.analysis.analyzer.englishAnalyzer.char_filter : [html_strip]
index.analysis.analyzer.englishAnalyzer.filter : [standard, lowercase, trim]
index.analysis.analyzer.englishAnalyzer.tokenizer : standard
index.analysis.filter.snowballStemmer.language : english
index.analysis.filter.snowballStemmer.type : snowball
index.analysis.normalizer.lowercaseNormalizer.char_filter : []
index.analysis.normalizer.lowercaseNormalizer.filter : [lowercase]
index.analysis.normalizer.lowercaseNormalizer.type : custom
index.creation_date : 1550598726356
index.number_of_replicas : 1
index.number_of_shards : 5
index.provided_name : indexsettingexampleone1550598724119
index.uuid : u2ZhfrjkTPevIt1DqMT0WA
index.version.created : 6060099

5. Update Index Settings

UpdateSettingsRequest class along with Settings class is used to update any existing settings. In below example, we have update index.max_inner_result_window setting to 100.

5.1 Example

public void updateSettings() throws ExecutionException, InterruptedException {

    UpdateSettingsRequest request = new UpdateSettingsRequest(indexName);
    String settingKey = "index.max_inner_result_window";
    int settingValue = 100;
    Settings settings =
            Settings.builder()
                    .put(settingKey, settingValue)
                    .build();
    request.settings(settings);
    AcknowledgedResponse updateSettingsResponse =
            client.admin().indices().updateSettings(request).get();

    System.out.println("IsAcknowledged : "+updateSettingsResponse.isAcknowledged());
    getUpdatedSettings();
}

5.1 Output

IsAcknowledged : true
***************Get Updated Settings *********************
index.max_inner_result_window : 100

6. Conclusion

Elastic Search Transport Client Java API provides all the various ways to create Index, add/update settings etc.. In this article, we have discussed all the Index and Index Settings related operation with an example.

7. References

8. Source Code

Create Index Settings Example

You can also check our Git repository for Elastic Search Create Index Settings Example and other useful examples.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *