Put Mapping APIedit

Put Mapping Requestedit

A PutMappingRequest requires an index argument, and a type:

PutMappingRequest request = new PutMappingRequest("twitter"); 

The index to add the mapping to

The type to create (or update)

Mapping sourceedit

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

    "{\n" +
    "  \"properties\": {\n" +
    "    \"message\": {\n" +
    "      \"type\": \"text\"\n" +
    "    }\n" +
    "  }\n" +

Mapping source provided as a String

Providing the mapping sourceedit

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);

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

XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.field("type", "text");

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 argumentsedit

The following arguments can optionally be provided:


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


Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

Synchronous Executionedit

PutMappingResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);

Asynchronous Executionedit

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, RequestOptions.DEFAULT, 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>() {
        public void onResponse(PutMappingResponse putMappingResponse) {

        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 Responseedit

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