Create Index APIedit

Create Index Requestedit

A CreateIndexRequest requires an index argument:

CreateIndexRequest request = new CreateIndexRequest("twitter"); 

The index to create

Index settingsedit

Each index created can have specific settings associated with it.

request.settings(Settings.builder() 
    .put("index.number_of_shards", 3)
    .put("index.number_of_replicas", 2)
);

Settings for this index

Index mappingsedit

An index may be created with mappings for its document types

request.mapping("_doc", 
        "{\n" +
        "  \"_doc\": {\n" +
        "    \"properties\": {\n" +
        "      \"message\": {\n" +
        "        \"type\": \"text\"\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
        "}", 
        XContentType.JSON);

The type to define

The mapping for this type, provided as a JSON string

The mapping source can be provided in different ways in addition to the String example shown above:

Map<String, Object> jsonMap = new HashMap<>();
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
jsonMap.put("_doc", mapping);
request.mapping("_doc", jsonMap); 

Mapping source provided as a Map which gets automatically converted to JSON format

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
    builder.startObject("_doc");
    {
        builder.startObject("properties");
        {
            builder.startObject("message");
            {
                builder.field("type", "text");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
}
builder.endObject();
request.mapping("_doc", builder); 

Mapping source provided as an XContentBuilder object, the Elasticsearch built-in helpers to generate JSON content

request.mapping("_doc", "message", "type=text"); 

Mapping source provided as Object key-pairs, which gets converted to JSON format

Index aliasesedit

Aliases can be set at index creation time

request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "kimchy")));  

The alias to define

Providing the whole sourceedit

The whole source including all of its sections (mappings, settings and aliases) can also be provided:

request.source("{\n" +
        "    \"settings\" : {\n" +
        "        \"number_of_shards\" : 1,\n" +
        "        \"number_of_replicas\" : 0\n" +
        "    },\n" +
        "    \"mappings\" : {\n" +
        "        \"_doc\" : {\n" +
        "            \"properties\" : {\n" +
        "                \"message\" : { \"type\" : \"text\" }\n" +
        "            }\n" +
        "        }\n" +
        "    },\n" +
        "    \"aliases\" : {\n" +
        "        \"twitter_alias\" : {}\n" +
        "    }\n" +
        "}", XContentType.JSON); 

The source provided as a JSON string. It can also be provided as a Map or an XContentBuilder.

Optional argumentsedit

The following arguments can optionally be provided:

request.timeout(TimeValue.timeValueMinutes(2)); 
request.timeout("2m"); 

Timeout to wait for the all the nodes to acknowledge the index creation as a TimeValue

Timeout to wait for the all the nodes to acknowledge the index creation as a String

request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); 
request.masterNodeTimeout("1m"); 

Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

request.waitForActiveShards(2); 
request.waitForActiveShards(ActiveShardCount.DEFAULT); 

The number of active shard copies to wait for before the create index API returns a response, as an int

The number of active shard copies to wait for before the create index API returns a response, as an ActiveShardCount

Synchronous Executionedit

CreateIndexResponse createIndexResponse = client.indices().create(request);

Asynchronous Executionedit

The asynchronous execution of a create index request requires both the CreateIndexRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.indices().createAsync(request, listener); 

The CreateIndexRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for CreateIndexResponse looks like:

ActionListener<CreateIndexResponse> listener =
        new ActionListener<CreateIndexResponse>() {

    @Override
    public void onResponse(CreateIndexResponse createIndexResponse) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument

Create Index Responseedit

The returned CreateIndexResponse allows to retrieve information about the executed operation as follows:

boolean acknowledged = createIndexResponse.isAcknowledged(); 
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); 

Indicates whether all of the nodes have acknowledged the request

Indicates whether the requisite number of shard copies were started for each shard in the index before timing out