Elastic Search

Elasticsearch Get Index Template

An index template refers to the layout or a schema-like structure that tells the Elasticsearch engine how to configure an index during creation. A template is created before an index creation. Index templates are reusable blocks that can be exported and used in a different cluster to replicate an index with a similar index. A template will include mappings, index settings, aliases, and more.

This tutorial will demonstrate how to fetch an index template and view all its information.

Elasticsearch Create Index Template

The following example request shows how to create a simple index template for illustration purposes.

If you have an existing index template, feel free to skip to the next section where we discuss how to fetch the information stored in a given index template.

PUT _index_template/template_1
{
  "index_patterns": ["kibana*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
      "mydata": { }
    }
  },
  "priority": 500,
  "version": 3,
  "_meta": {
    "description": "my custom"
  }
}

 
The request above should create a simple index template with the specified properties. If you are new to creating index templates, check out our tutorial on the topic to learn more.

Elasticsearch Get Index Template

We can use the index template API to fetch the details of an existing index template. For example, the request syntax is as shown below:

GET /_index_template/<index-template>

 
For example, to fetch the information about the template_1 we created above, we can run the query as shown:

curl -XGET "http://localhost:9200/_index_template/template_1" -H "kbn-xsrf: reporting"

 
Return value:

{
  "index_templates": [
    {
      "name": "template_1",
      "index_template": {
        "index_patterns": [
          "kibana*"
        ],
        "template": {
          "settings": {
            "index": {
              "number_of_shards": "1"
            }
          },
          "mappings": {
            "_source": {
              "enabled": true
            },
            "properties": {
              "created_at": {
                "format": "EEE MMM dd HH:mm:ss Z yyyy",
                "type": "date"
              },
              "host_name": {
                "type": "keyword"
              }
            }
          },
          "aliases": {
            "mydata": {}
          }
        },
        "composed_of": [],
        "priority": 500,
        "version": 3,
        "_meta": {
          "description": "my custom"
        }
      }
    }
  ]
}

 
You can also fetch information about index templates matching a specific pattern. For example, to fetch all the information about the .kibana index templates, we can run the request as shown:

GET /_index_template/.kibana*

 
Resulting output:


To fetch the information about all the available index templates, we can send a request as shown:

GET _index_templates

 
The above should return information about all the available index templates.

Conclusion

In this article, you learned how to use the Elasticsearch get index template to create and fetch information about an existing index template.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list