Indices Administrationedit

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 Indexedit

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

Index Settingsedit

Each index created can have specific settings associated with it.

                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)

Settings for this index

Execute the action and wait for the result

Put Mappingedit

You can add mappings at index creation time:

        .addMapping("_doc", "message", "type=text") 

Creates an index called twitter

Add a _doc type with a field called message that has the data type text.

There are several variants of the above addMapping method, some taking an XContentBuilder or a Map with the mapping definition as arguments. Make sure to check the javadocs to pick the simplest one for your use case.

The PUT mapping API also allows for updating the mapping after index creation. In this case you can provide the mapping as a String similar to the REST API syntax:

.setSource("{\n" +
        "  \"properties\": {\n" +
        "    \"name\": {\n" +                           
        "      \"type\": \"text\"\n" +
        "    }\n" +
        "  }\n" +
        "}", XContentType.JSON)

// You can also provide the type in the source document
.setSource("{\n" +
        "    \"_doc\":{\n" +                            
        "        \"properties\": {\n" +
        "            \"name\": {\n" +
        "                \"type\": \"text\"\n" +
        "            }\n" +
        "        }\n" +
        "    }\n" +
        "}", XContentType.JSON)

Puts a mapping on existing index called twitter

Adds a new field name to the mapping

The type can be also provided within the source


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

        .prepareRefresh("twitter", "company")   

Refresh all indices

Refresh one index

Refresh many indices

Get Settingsedit

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 Settingsedit

You can change index settings by calling:

                .put("index.number_of_replicas", 0)

Index to update