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.
{
"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:
For example, to fetch the information about the template_1 we created above, we can run the query as shown:
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:
Resulting output:
To fetch the information about all the available index templates, we can send a request as shown:
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.