Indices Administration

To access indices Java API, you need to call indices() method from an AdminClient:

IndicesAdminClient indicesAdminClient = client.admin().indices();

In the rest of this guide, we will use client.admin().indices().

Create Index

Using an IndicesAdminClient, you can create an index with all default settings and no mapping:

client.admin().indices().prepareCreate("twitter").get();
Index Settings

Each index created can have specific settings associated with it.

client.admin().indices().prepareCreate("twitter")
        .setSettings(Settings.builder()             
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        )
        .get();                                     

Settings for this index

Execute the action and wait for the result

Put Mapping

The PUT mapping API allows you to add a new type while creating an index:

client.admin().indices().prepareCreate("twitter")   
        .addMapping("tweet", "{\n" +                
                "    \"tweet\": {\n" +
                "      \"properties\": {\n" +
                "        \"message\": {\n" +
                "          \"type\": \"string\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }")
        .get();

Creates an index called twitter

It also adds a tweet mapping type.

The PUT mapping API also allows to add a new type to an existing index:

client.admin().indices().preparePutMapping("twitter")   
        .setType("user")                                
        .setSource("{\n" +                              
                "  \"properties\": {\n" +
                "    \"name\": {\n" +
                "      \"type\": \"string\"\n" +
                "    }\n" +
                "  }\n" +
                "}")
        .get();

// You can also provide the type in the source document
client.admin().indices().preparePutMapping("twitter")
        .setType("user")
        .setSource("{\n" +
                "    \"user\":{\n" +                        
                "        \"properties\": {\n" +
                "            \"name\": {\n" +
                "                \"type\": \"string\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}")
        .get();

Puts a mapping on existing index called twitter

Adds a user mapping type.

This user has a predefined type

type can be also provided within the source

You can use the same API to update an existing mapping:

client.admin().indices().preparePutMapping("twitter")   
        .setType("tweet")                               
        .setSource("{\n" +                              
                "  \"properties\": {\n" +
                "    \"user_name\": {\n" +
                "      \"type\": \"string\"\n" +
                "    }\n" +
                "  }\n" +
                "}")
        .get();

Puts a mapping on existing index called twitter

Updates the user mapping type.

This user has now a new field user_name

Refresh

The refresh API allows to explicitly refresh one or more index:

client.admin().indices().prepareRefresh().get(); 
client.admin().indices()
        .prepareRefresh("twitter")               
        .get();
client.admin().indices()
        .prepareRefresh("twitter", "company")   
        .get();

Refresh all indices

Refresh one index

Refresh many indices

Get Settings

The get settings API allows to retrieve settings of index/indices:

GetSettingsResponse response = client.admin().indices()
        .prepareGetSettings("company", "employee").get();                           
for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) { 
    String index = cursor.key;                                                      
    Settings settings = cursor.value;                                               
    Integer shards = settings.getAsInt("index.number_of_shards", null);             
    Integer replicas = settings.getAsInt("index.number_of_replicas", null);         
}

Get settings for indices company and employee

Iterate over results

Index name

Settings for the given index

Number of shards for this index

Number of replicas for this index

Update Indices Settings

You can change index settings by calling:

client.admin().indices().prepareUpdateSettings("twitter")   
        .setSettings(Settings.builder()                     
                .put("index.number_of_replicas", 0)
        )
        .get();

Index to update

Settings