Put Template API

edit

Put Index Template Request

edit

A PutIndexTemplateRequest specifies the name of a template and patterns which controls whether the template should be applied to the new index.

PutIndexTemplateRequest request = new PutIndexTemplateRequest("my-template"); 
request.patterns(Arrays.asList("pattern-1", "log-*")); 

The name of the template

The patterns of the template

Settings

edit

The settings of the template will be applied to the new index whose name matches the template’s patterns.

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

Settings for this template

Mappings

edit

The mapping of the template will be applied to the new index whose name matches the template’s patterns.

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

Aliases

edit

The aliases of the template will define aliasing to the index whose name matches the template’s patterns. A placeholder {index} can be used in an alias of a template.

request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "kimchy")));  
request.alias(new Alias("{index}_alias").searchRouting("xyz"));  

The alias to define

The alias to define with placeholder

Order

edit

In case multiple templates match an index, the orders of matching templates determine the sequence that settings, mappings, and alias of each matching template is applied. Templates with lower orders are applied first, and higher orders override them.

request.order(20);  

The order of the template

Version

edit

A template can optionally specify a version number which can be used to simplify template management by external systems.

request.version(4);  

The version number of the template

Providing the whole source

edit

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

request.source("{\n" +
    "  \"index_patterns\": [\n" +
    "    \"log-*\",\n" +
    "    \"pattern-1\"\n" +
    "  ],\n" +
    "  \"order\": 1,\n" +
    "  \"settings\": {\n" +
    "    \"number_of_shards\": 1\n" +
    "  },\n" +
    "  \"mappings\": {\n" +
    "    \"_doc\": {\n" +
    "      \"properties\": {\n" +
    "        \"message\": {\n" +
    "          \"type\": \"text\"\n" +
    "        }\n" +
    "      }\n" +
    "    }\n" +
    "  },\n" +
    "  \"aliases\": {\n" +
    "    \"alias-1\": {},\n" +
    "    \"{index}-alias\": {}\n" +
    "  }\n" +
    "}", XContentType.JSON); 

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

Optional arguments

edit

The following arguments can optionally be provided:

request.create(true);  

To force to only create a new template; do not overwrite the existing template

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
PutIndexTemplateResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);

Asynchronous Execution

edit

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

client.indices().putTemplateAsync(request, RequestOptions.DEFAULT, listener); 

The PutIndexTemplateRequest 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 PutIndexTemplateResponse looks like:

ActionListener<PutIndexTemplateResponse> listener =
    new ActionListener<PutIndexTemplateResponse>() {
        @Override
        public void onResponse(PutIndexTemplateResponse putTemplateResponse) {
            
        }

        @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 Index Template Response

edit

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

boolean acknowledged = putTemplateResponse.isAcknowledged(); 

Indicates whether all of the nodes have acknowledged the request