Templates help you reuse search or index settings easily. Priority and composition decide which templates apply when multiple templates match.
Template priority and composition in Elasticsearch
PUT _index_template/template_name
{
"index_patterns": ["pattern*"],
"priority": number,
"composed_of": ["template1", "template2"],
"template": {
"settings": { ... },
"mappings": { ... },
"aliases": { ... }
}
}priority is a number. Higher means this template applies first.
composed_of lets you combine other templates by name.
PUT _index_template/base_template
{
"index_patterns": ["logs-*"],
"priority": 5,
"template": {
"settings": {
"number_of_shards": 1
}
}
}PUT _index_template/high_priority_template
{
"index_patterns": ["logs-2024*"],
"priority": 10,
"template": {
"settings": {
"number_of_replicas": 2
}
}
}PUT _index_template/composed_template
{
"index_patterns": ["logs-2024*"],
"priority": 15,
"composed_of": ["base_template", "high_priority_template"],
"template": {
"settings": {
"refresh_interval": "30s"
}
}
}This example creates three templates. The last one combines the first two and adds a refresh interval. When you create an index named 'app-2024-01', Elasticsearch applies the composed template with the highest priority. The index will have 2 shards, 1 replica, a refresh interval of 1 second, and mappings for 'user' and 'timestamp'.
PUT _index_template/base_template
{
"index_patterns": ["app-*"],
"priority": 5,
"template": {
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"user": { "type": "keyword" }
}
}
}
}
PUT _index_template/high_priority_template
{
"index_patterns": ["app-2024*"],
"priority": 10,
"template": {
"settings": {
"number_of_replicas": 1
},
"mappings": {
"properties": {
"timestamp": { "type": "date" }
}
}
}
}
PUT _index_template/composed_template
{
"index_patterns": ["app-2024*"],
"priority": 15,
"composed_of": ["base_template", "high_priority_template"],
"template": {
"settings": {
"refresh_interval": "1s"
}
}
}
GET app-2024-01/_settings
GET app-2024-01/_mappingTemplates with higher priority override settings from lower priority templates.
When using composed_of, the order of templates matters for merging.
Always test your templates by creating a new index and checking its settings and mappings.
Templates let you reuse index settings and mappings easily.
Priority controls which template applies first when multiple match.
Composition lets you combine templates for flexible configuration.
Practice
priority setting control in Elasticsearch index templates?Solution
Step 1: Understand the role of priority in templates
Theprioritysetting determines which template Elasticsearch applies first when multiple templates match the same index.Step 2: Differentiate priority from other settings
Priority does not affect shard count, field order, or refresh interval; it only controls template application order.Final Answer:
Which template applies first when multiple templates match an index -> Option AQuick Check:
Priority controls template application order = B [OK]
- Confusing priority with shard settings
- Thinking priority affects field order
- Assuming priority controls refresh interval
Solution
Step 1: Check the priority value type
Priority must be a numeric value, such as 10, not a string or boolean.Step 2: Validate JSON structure
{ "index_patterns": ["logs-*"], "priority": 10, "template": { "settings": {} } } correctly uses an integer 10 for priority and valid JSON structure for template.Final Answer:
{ "index_patterns": ["logs-*"], "priority": 10, "template": { "settings": {} } } -> Option CQuick Check:
Priority is a number, not string or boolean = C [OK]
- Using string like "high" instead of number
- Setting priority to true or null
- Incorrect JSON structure for template
logs-2024. Which template settings will Elasticsearch apply first?Solution
Step 1: Understand priority order
Elasticsearch sorts matching templates by priority in ascending order (lowest first) and merges their configurations, with later (higher priority) templates overriding earlier ones.Step 2: Compare priorities of Template A and B
Template A has priority 5, which is lower than Template B's 10, so Template A applies first.Final Answer:
Template A settings will apply first -> Option DQuick Check:
Lower priority number applies first = B [OK]
- Thinking higher priority applies first
- Thinking templates apply simultaneously
- Expecting error on priority conflict
composed_of referencing Template Y, but Template Y is missing. What will happen when you try to create an index matching Template X?Solution
Step 1: Understand template composition with
Templates usingcomposed_ofcomposed_ofrequire referenced templates to exist.Step 2: Check behavior when referenced template is missing
If Template Y is missing, Elasticsearch throws an error during index creation because it cannot compose Template X fully.Final Answer:
Index creation fails with an error about missing template -> Option BQuick Check:
Missing composed_of template causes error = A [OK]
- Assuming missing templates are ignored
- Expecting automatic creation of missing templates
- Thinking index creation succeeds without all templates
Solution
Step 1: Use base template for common settings with low priority
The base template should have a lower priority so it is merged first in the composition order, providing common settings that higher priority templates can override.Step 2: Use
Specialized templates compose the base template and have higher priority to add or override mappings.composed_ofin specialized templates with higher priorityStep 3: Combine priority and composition for flexible layering
This setup allows reusable base settings and flexible specialized additions.Final Answer:
Create a base template with low priority and usecomposed_ofin specialized templates with higher priority -> Option AQuick Check:
Base low priority + composed_of in higher priority = D [OK]
- Setting base template with highest priority
- Not using composition for reusable settings
- Using same priority for all templates
Ready to prove it?
Pass the test and this pattern moves unknown
