Adding template for index patterns – How to solve this Elasticsearch error

Opster Team

Aug-23, Version: 6.8-8.9

Briefly, this error occurs when there’s an issue with adding a new index template in Elasticsearch. This could be due to incorrect syntax, invalid template settings, or conflicts with existing templates. To resolve this, ensure the template syntax is correct and valid according to Elasticsearch’s standards. Check for any conflicts with existing templates and resolve them. Also, ensure that the Elasticsearch version supports the template settings you’re trying to add. Lastly, check the Elasticsearch logs for more detailed error information.

In addition we recommend you run the Elasticsearch Template Optimizer to fix problems in your data modeling.

It will analyze your templates to detect issues and improve search performance, reduce indexing bottlenecks and optimize storage utilization. The Template Optimizer is free and requires no installation.

What this error means

This log message is an INFO message saying that a template has been created for a given index pattern. Elasticsearch applies templates to new indices based on an index pattern that matches the index name.

Below is an example of an index template, applied only at the index creation time for all the indices matching opster-* and elastic-* pattern.

Adding template for index patterns

What are index templates?

Index templates initialize the indices with predefined mapping and settings. Templates do not affect the existing indexes, but are applied when new indices are created. Whenever we create an index that matches the corresponding template, the template will be applied and the index will have the mappings and settings defined in the template.

How to create an index template

You can create an index template as shown below that will match any indices matching the names opster-* and elastic-*.

PUT/_template/opsterelasticsearch 
{
 "index_patterns": [
   "opster-*",
   "elastic-*"
 ],
 "mappings": {
   "properties": {
     "id": {
       "type": "keyword"
     },
     "location": {
       "type": "geo_point"
     },
     "movie": {
       "type": "text"
     }
   }
 }
}

In response, you will get:

{
 "acknowledged": true
}

When the index template is created, the following log is generated:

adding template [opsterelasticsearch] for index patterns [opster-*, elasticsearch-*]

Now you can create an index that will match the template’s definition and add data to it:

POST/ opster-1/_doc/1
 
{
 "id": 158,
 "location": "1.486912, 2.493157",
 "movie": "Harry Potter"
}

Get a list of all the templates using:

GET  / _cat/templates

Log Context

Log “adding template [{}] for index patterns {}” classname is MetadataIndexTemplateService.java.
We extracted the following from Elasticsearch source code for those seeking an in-depth context :

            return currentState;
        }

        Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(template);

        logger.info("adding template [{}] for index patterns {}"; request.name; request.indexPatterns);
        return ClusterState.builder(currentState).metadata(builder).build();
    }

    /**
     * Finds index templates whose index pattern matched with the given index name. In the case of

 

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?