Put Mapping API

edit

Put Mapping Request

edit

A PutMappingRequest requires an index argument, and a type:

PutMappingRequest request = new PutMappingRequest("twitter"); 
request.type("_doc"); 

The index to add the mapping to

The type to create (or update)

Mapping source

edit

A description of the fields to create on the mapping; if not defined, the mapping will default to empty.

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

Mapping source provided as a String

Providing the mapping source

edit

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);
jsonMap.put("properties", properties);
request.source(jsonMap); 

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

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

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

request.source("message", "type=text"); 

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

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

Synchronous Execution

edit
PutMappingResponse putMappingResponse = client.indices().putMapping(request);

Asynchronous Execution

edit

The asynchronous execution of a put mappings request requires both the PutMappingRequest instance and an ActionListener instance to be passed to the asynchronous method:

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

The PutMappingRequest 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 PutMappingResponse looks like:

ActionListener<PutMappingResponse> listener =
        new ActionListener<PutMappingResponse>() {
    @Override
    public void onResponse(PutMappingResponse putMappingResponse) {
        
    }

    @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

Put Mapping Response

edit

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

boolean acknowledged = putMappingResponse.isAcknowledged(); 

Indicates whether all of the nodes have acknowledged the request