Bird
Raised Fist0
Elasticsearchquery~10 mins

Component templates in Elasticsearch - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a component template in Elasticsearch?
easy
A. To manage user access permissions
B. To store actual index data
C. To store reusable index settings and mappings
D. To perform search queries

Solution

  1. Step 1: Understand component template role

    Component templates hold reusable parts like settings and mappings for indexes.
  2. Step 2: Differentiate from other features

    They do not store data or handle queries or permissions directly.
  3. Final Answer:

    To store reusable index settings and mappings -> Option C
  4. Quick Check:

    Component templates = reusable settings/mappings [OK]
Hint: Component templates hold reusable settings, not data or queries [OK]
Common Mistakes:
  • Confusing component templates with actual index data storage
  • Thinking component templates handle search queries
  • Assuming component templates manage user permissions
2. Which of the following is the correct way to create a component template using Elasticsearch API?
easy
A. PUT /_component_template/my_template { "template": { "settings": { "number_of_shards": 1 } } }
B. POST /my_template { "settings": { "number_of_shards": 1 } }
C. GET /_component_template/my_template { "settings": { "number_of_shards": 1 } }
D. DELETE /_component_template/my_template

Solution

  1. Step 1: Identify correct HTTP method and endpoint

    Creating a component template uses PUT method on /_component_template/{name}.
  2. Step 2: Check request body structure

    The body must include a template object with settings or mappings.
  3. Final Answer:

    PUT /_component_template/my_template { "template": { "settings": { "number_of_shards": 1 } } } -> Option A
  4. Quick Check:

    PUT + /_component_template + template body = A [OK]
Hint: Use PUT with /_component_template and include 'template' in body [OK]
Common Mistakes:
  • Using POST instead of PUT for creation
  • Using GET or DELETE for creating templates
  • Omitting the 'template' wrapper in the JSON body
3. Given this component template creation request, what will be the value of number_of_replicas in the resulting index if used in an index template?
PUT /_component_template/replica_template
{
  "template": {
    "settings": {
      "number_of_replicas": 2
    }
  }
}
medium
A. 2
B. 1
C. 0
D. Undefined

Solution

  1. Step 1: Read the component template settings

    The template sets number_of_replicas to 2 explicitly.
  2. Step 2: Understand inheritance in index templates

    When this component template is used in an index template, the setting applies to the index.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Component template sets replicas = 2 [OK]
Hint: Settings in component templates apply directly when used [OK]
Common Mistakes:
  • Assuming default replica count 1 overrides component template
  • Thinking replicas default to 0 if not set elsewhere
  • Confusing component template with index data
4. You tried to create a component template but got an error. Which of these is the most likely cause?
PUT /_component_template/my_template
{
  "settings": {
    "number_of_shards": 3
  }
}
medium
A. Using PUT instead of POST
B. number_of_shards cannot be set in component templates
C. Incorrect endpoint; should be /_index_template
D. Missing the 'template' wrapper around settings

Solution

  1. Step 1: Check JSON structure for component template

    Component templates require a template key wrapping settings and mappings.
  2. Step 2: Identify missing wrapper

    The request body lacks the "template" object, causing the error.
  3. Final Answer:

    Missing the 'template' wrapper around settings -> Option D
  4. Quick Check:

    Component template body must have 'template' key [OK]
Hint: Always wrap settings inside 'template' in component templates [OK]
Common Mistakes:
  • Omitting the 'template' key in JSON body
  • Confusing component template endpoint with index template
  • Thinking PUT is wrong method for creation
5. You want to create an index template that uses two component templates: one for settings and one for mappings. Which is the correct way to reference both component templates in the index template?
hard
A. { "index_patterns": ["logs-*"], "template": { "settings": {}, "mappings": {} } }
B. { "index_patterns": ["logs-*"], "composed_of": ["settings_template", "mappings_template"] }
C. { "index_patterns": ["logs-*"], "components": ["settings_template", "mappings_template"] }
D. { "index_patterns": ["logs-*"], "use_components": ["settings_template", "mappings_template"] }

Solution

  1. Step 1: Recall index template syntax for component templates

    Index templates use the composed_of array to list component templates.
  2. Step 2: Match correct JSON structure

    { "index_patterns": ["logs-*"], "composed_of": ["settings_template", "mappings_template"] } correctly uses composed_of with component template names.
  3. Final Answer:

    { "index_patterns": ["logs-*"], "composed_of": ["settings_template", "mappings_template"] } -> Option B
  4. Quick Check:

    Use 'composed_of' to combine component templates [OK]
Hint: Use 'composed_of' array to combine component templates [OK]
Common Mistakes:
  • Using wrong key like 'components' or 'use_components'
  • Trying to put settings and mappings directly instead of referencing components
  • Confusing index template syntax with component template syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard