📖 Scenario: You are managing an Elasticsearch cluster for a company that stores logs from different applications. To keep the index settings consistent, you want to use component templates to define common settings and mappings.
🎯 Goal: Build an Elasticsearch setup where you create a component template with specific settings and mappings, then create an index template that uses this component template.
📋 What You'll Learn
Create a component template named logs_settings with number_of_shards set to 1
Add a mapping in the component template for a timestamp field of type date
Create an index template named logs_template that uses the logs_settings component template
Apply the index template to indices matching logs-*
💡 Why This Matters
🌍 Real World
Component templates help keep Elasticsearch index settings and mappings consistent across many indices, especially in logging or monitoring systems.
💼 Career
Knowing how to use component and index templates is important for Elasticsearch administrators and developers to manage scalable and maintainable data indexing.
Progress0 / 4 steps
1
Create the component template with settings
Write a PUT request to create a component template called logs_settings with number_of_shards set to 1 in the settings.
Elasticsearch
Hint
Use the PUT _component_template/{name} API and include settings inside template.
2
Add a mapping for the timestamp field
Update the logs_settings component template to add a mappings section with a timestamp field of type date.
Elasticsearch
Hint
Inside template, add a mappings object with properties defining the timestamp field.
3
Create an index template using the component template
Write a PUT request to create an index template called logs_template that applies to index patterns matching logs-* and uses the component template logs_settings.
Elasticsearch
Hint
Use PUT _index_template/{name} with index_patterns and composed_of referencing the component template.
4
Verify the index template is applied
Write a GET request to retrieve the index template named logs_template and print the response.
Elasticsearch
Hint
Use GET _index_template/{name} to see the index template details.
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
Step 1: Understand component template role
Component templates hold reusable parts like settings and mappings for indexes.
Step 2: Differentiate from other features
They do not store data or handle queries or permissions directly.
Final Answer:
To store reusable index settings and mappings -> Option C
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
Step 1: Check JSON structure for component template
Component templates require a template key wrapping settings and mappings.
Step 2: Identify missing wrapper
The request body lacks the "template" object, causing the error.
Final Answer:
Missing the 'template' wrapper around settings -> Option D
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?