Index templates help you set rules for new indexes automatically. This saves time and keeps data organized.
Index templates in Elasticsearch
PUT _index_template/template_name
{
"index_patterns": ["pattern*"],
"template": {
"settings": { ... },
"mappings": { ... },
"aliases": { ... }
},
"priority": number
}index_patterns defines which index names the template applies to.
template holds settings, mappings, and aliases for the index.
PUT _index_template/my_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"timestamp": {"type": "date"}
}
}
}
}PUT _index_template/user_data_template
{
"index_patterns": ["user-data-*"],
"template": {
"settings": {
"number_of_replicas": 2
},
"aliases": {
"user_data_alias": {}
}
},
"priority": 10
}This creates an index template named 'sample_template' for indexes starting with 'test-index-'. It sets shards, replicas, mappings for user, message, and created_at fields, and adds an alias.
PUT _index_template/sample_template
{
"index_patterns": ["test-index-*"],
"template": {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"user": {"type": "keyword"},
"message": {"type": "text"},
"created_at": {"type": "date"}
}
},
"aliases": {
"test_alias": {}
}
},
"priority": 5
}Templates only affect indexes created after the template is set.
Priority helps when multiple templates match the same index; higher priority wins.
You can update templates anytime to change future index settings.
Index templates automate settings and mappings for new indexes.
They use patterns to match index names.
Templates keep your data organized and consistent.
Practice
What is the main purpose of an index template in Elasticsearch?
Solution
Step 1: Understand what index templates do
Index templates define settings and mappings that apply automatically to new indexes matching a pattern.Step 2: Compare options with this purpose
Only To automatically apply settings and mappings to new indexes matching a pattern describes this automatic application of settings and mappings.Final Answer:
To automatically apply settings and mappings to new indexes matching a pattern -> Option DQuick Check:
Index templates = auto apply settings [OK]
- Thinking templates delete or backup data
- Confusing templates with query optimization
- Believing templates apply to existing indexes only
Which of the following is the correct way to specify an index pattern in an Elasticsearch index template?
{
"index_patterns": ["logs-*"]
}Solution
Step 1: Recall correct JSON syntax for index_patterns
Theindex_patternsfield expects an array of strings, so it must be inside square brackets.Step 2: Check each option's syntax
Only "index_patterns": ["logs-*"] uses an array with a string inside: ["logs-*"] which is correct JSON syntax.Final Answer:
"index_patterns": ["logs-*"] -> Option BQuick Check:
Patterns must be in array format [OK]
- Using a string instead of an array
- Using curly braces instead of square brackets
- Omitting quotes around the pattern
Given this index template:
{
"index_patterns": ["app-logs-*"],
"template": {
"settings": {"number_of_shards": 2},
"mappings": {"properties": {"user": {"type": "keyword"}}}
}
}What will be the number of shards and mapping type for the index app-logs-2024?
Solution
Step 1: Match index name with pattern
The index nameapp-logs-2024matches the patternapp-logs-*in the template.Step 2: Apply template settings and mappings
The template setsnumber_of_shardsto 2 and maps theuserfield askeywordtype.Final Answer:
2 shards, user field as keyword -> Option CQuick Check:
Matching pattern applies template settings [OK]
- Assuming default shard count instead of template value
- Confusing keyword with text mapping type
- Ignoring pattern matching rules
Identify the error in this index template snippet:
{
"index_patterns": "logs-*",
"template": {
"settings": {"number_of_replicas": 1}
}
}Solution
Step 1: Check the type of index_patterns
Theindex_patternsfield must be an array of strings, but here it is a single string.Step 2: Validate other fields
number_of_replicasis a valid setting,mappingsis optional, and the pattern syntax is correct.Final Answer:
"index_patterns" should be an array, not a string -> Option AQuick Check:
index_patterns must be an array [OK]
- Using string instead of array for index_patterns
- Thinking mappings are always required
- Assuming pattern syntax is wrong when it's correct
You want to create an index template that applies to all indexes starting with sales- but excludes those ending with -archive. Which approach correctly achieves this?
Solution
Step 1: Understand template priority and pattern matching
Elasticsearch applies templates by priority; higher priority templates override lower ones.Step 2: Exclude indexes by overriding with higher priority template
To excludesales-*-archive, create a separate template with higher priority that overrides or clears settings for those indexes.Final Answer:
Create a higher priority template for sales-*-archive to override settings -> Option AQuick Check:
Use priority to override unwanted index templates [OK]
- Assuming manual exclusion works automatically
- Using composed_of incorrectly for exclusion
- Not setting template priority to control overrides
Ready to prove it?
Pass the test and this pattern moves unknown
