0
0
Elasticsearchquery~10 mins

Component templates in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Component templates
Define component template
Specify settings, mappings, aliases
Save component template
Use component template in index template
Create index matching template
Index created with combined settings/mappings
Component templates are reusable building blocks for index templates, defining settings, mappings, or aliases that can be shared across multiple index templates.
Execution Sample
Elasticsearch
PUT _component_template/my_template
{
  "template": {
    "settings": {"number_of_shards": 1},
    "mappings": {"properties": {"field1": {"type": "keyword"}}}
  }
}
This code creates a component template named 'my_template' with shard settings and a keyword field mapping.
Execution Table
StepActionRequest SentResponseResulting State
1Define component template 'my_template'PUT _component_template/my_template with settings and mappingsAcknowledged: trueComponent template 'my_template' stored
2Create index template using component templatePUT _index_template/my_index_template with component_templates: ['my_template']Acknowledged: trueIndex template 'my_index_template' references 'my_template'
3Create index matching template patternPUT my-index-000001Acknowledged: trueIndex created with settings and mappings from 'my_template'
4Verify index settings and mappingsGET my-index-000001/_settings and _mappingSettings and mappings include number_of_shards=1 and field1 as keywordIndex uses combined template settings
💡 Index created successfully using component template settings and mappings
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
component_template 'my_template'undefined{"template":{"settings":{"number_of_shards":1},"mappings":{"properties":{"field1":{"type":"keyword"}}}}}unchangedunchangedunchanged
index_template 'my_index_template'undefinedundefined{"component_templates":["my_template"]}unchangedunchanged
index 'my-index-000001'undefinedundefinedundefinedcreated with settings and mappings from 'my_template'exists with correct settings and mappings
Key Moments - 2 Insights
Why do we create a component template separately before using it in an index template?
Because component templates are reusable parts that can be shared by multiple index templates, defining them separately allows consistent settings and mappings across indexes. See execution_table step 1 and 2.
What happens if the index name does not match the index template pattern?
The index will not use the component template settings because the index template won't apply. This is why in step 3 the index name must match the pattern defined in the index template.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response after defining the component template?
AError: template not found
BAcknowledged: true
CAcknowledged: false
DNo response
💡 Hint
Check execution_table row 1, Response column
At which step is the index created using the component template?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table, Step column and Action describing index creation
If we change the component template's number_of_shards to 3, what changes in variable_tracker after Step 1?
Acomponent_template 'my_template' settings show number_of_shards=3
Bindex_template changes to number_of_shards=3
Cindex 'my-index-000001' is created with 1 shard
DNo change anywhere
💡 Hint
Check variable_tracker row for component_template after Step 1
Concept Snapshot
Component templates define reusable settings, mappings, and aliases.
They are created with PUT _component_template/{name}.
Index templates reference component templates by name.
When an index matches the index template pattern, it inherits combined settings.
This promotes reuse and consistency across indexes.
Full Transcript
Component templates in Elasticsearch are reusable parts that hold settings, mappings, or aliases. First, you create a component template with a PUT request including these definitions. Then, you create an index template that references this component template by name. When you create an index matching the index template's pattern, Elasticsearch applies the combined settings and mappings from the component template. This process helps keep your index configurations consistent and easy to manage. The execution steps show defining the component template, linking it in an index template, creating an index, and verifying the applied settings.