Index API

Index Request

An IndexRequest requires the following arguments:

IndexRequest request = new IndexRequest(
        "posts", 
        "doc",  
        "1");   
String jsonString = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
        "}";
request.source(jsonString, XContentType.JSON); 

Index

Type

Document id

Document source provided as a String

Providing the document source

The document source can be provided in different ways:

Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
        .source(jsonMap); 

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

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
    builder.field("user", "kimchy");
    builder.field("postDate", new Date());
    builder.field("message", "trying out Elasticsearch");
}
builder.endObject();
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
        .source(builder);  

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

IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
        .source("user", "kimchy",
                "postDate", new Date(),
                "message", "trying out Elasticsearch"); 

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

Optional arguments

The following arguments can optionally be provided:

request.routing("routing"); 

Routing value

request.parent("parent"); 

Parent value

request.timeout(TimeValue.timeValueSeconds(1)); 
request.timeout("1s"); 

Timeout to wait for primary shard to become available as a TimeValue

Timeout to wait for primary shard to become available as a String

request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); 
request.setRefreshPolicy("wait_for");                            

Refresh policy as a WriteRequest.RefreshPolicy instance

Refresh policy as a String

request.version(2); 

Version

request.versionType(VersionType.EXTERNAL); 

Version type

request.opType(DocWriteRequest.OpType.CREATE); 
request.opType("create"); 

Operation type provided as an DocWriteRequest.OpType value

Operation type provided as a String: can be create or update (default)

request.setPipeline("pipeline"); 

The name of the ingest pipeline to be executed before indexing the document

Synchronous Execution

IndexResponse indexResponse = client.index(request);

Asynchronous Execution

client.indexAsync(request, new ActionListener<IndexResponse>() {
    @Override
    public void onResponse(IndexResponse indexResponse) {
        
    }

    @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

Index Response

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

String index = indexResponse.getIndex();
String type = indexResponse.getType();
String id = indexResponse.getId();
long version = indexResponse.getVersion();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
    
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
    
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
    
}
if (shardInfo.getFailed() > 0) {
    for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
        String reason = failure.reason(); 
    }
}

Handle (if needed) the case where the document was created for the first time

Handle (if needed) the case where the document was rewritten as it was already existing

Handle the situation where number of successful shards is less than total shards

Handle the potential failures

If there is a version conflict, an ElasticsearchException will be thrown:

IndexRequest request = new IndexRequest("posts", "doc", "1")
        .source("field", "value")
        .version(1);
try {
    IndexResponse response = client.index(request);
} catch(ElasticsearchException e) {
    if (e.status() == RestStatus.CONFLICT) {
        
    }
}

The raised exception indicates that a version conflict error was returned

Same will happen in case opType was set to create and a document with same index, type and id already existed:

IndexRequest request = new IndexRequest("posts", "doc", "1")
        .source("field", "value")
        .opType(DocWriteRequest.OpType.CREATE);
try {
    IndexResponse response = client.index(request);
} catch(ElasticsearchException e) {
    if (e.status() == RestStatus.CONFLICT) {
        
    }
}

The raised exception indicates that a version conflict error was returned