Create or update index template APIedit


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(asList("pattern-1", "log-*")); 

The name of the template

The patterns of the template


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

    .put("index.number_of_shards", 3)
    .put("index.number_of_replicas", 1)

Settings for this template


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

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

The mapping, 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> properties = new HashMap<>();
        Map<String, Object> message = new HashMap<>();
        message.put("type", "text");
        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


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


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.


The order of the template


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


The version number of the template

Providing the whole sourceedit

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" +
    "    \"properties\": {\n" +
    "      \"message\": {\n" +
    "        \"type\": \"text\"\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 argumentsedit

The following arguments can optionally be provided:


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


Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

Synchronous executionedit

When executing a PutIndexTemplateRequest in the following manner, the client waits for the PutIndexTemplateResponse to be returned before continuing with code execution:

AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS);

Synchronous calls may throw an IOException in case of either failing to parse the REST response in the high-level REST client, the request times out or similar cases where there is no response coming back from the server.

In cases where the server returns a 4xx or 5xx error code, the high-level client tries to parse the response body error details instead and then throws a generic ElasticsearchException and adds the original ResponseException as a suppressed exception to it.

Asynchronous executionedit

Executing a PutIndexTemplateRequest can also be done in an asynchronous fashion so that the client can return directly. Users need to specify how the response or potential failures will be handled by passing the request and a listener to the asynchronous put-template method:

client.indices().putTemplateAsync(request, LEGACY_TEMPLATE_OPTIONS, 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. Failure scenarios and expected exceptions are the same as in the synchronous execution case.

A typical listener for put-template looks like:

ActionListener<AcknowledgedResponse> listener =
    new ActionListener<AcknowledgedResponse>() {
        public void onResponse(AcknowledgedResponse putTemplateResponse) {

        public void onFailure(Exception e) {

Called when the execution is successfully completed.

Called when the whole PutIndexTemplateRequest fails.


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