Create Index API

edit

Create Index Request

edit

A CreateIndexRequest requires an index argument:

CreateIndexRequest request = new CreateIndexRequest("twitter"); 

The index to create

Index settings

edit

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 mappings

edit

An index may be created with mappings for its document types

request.mapping("tweet", 
    "  {\n" +
    "    \"tweet\": {\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

Index aliases

edit

Aliases can be set at index creation time

request.alias(
    new Alias("twitter_alias")  
);

The alias to define

Optional arguments

edit

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 Execution

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

Asynchronous Execution

edit

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 Response

edit

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