Index templates in Elasticsearch - Time & Space Complexity
When using index templates in Elasticsearch, it's important to understand how the time to apply templates grows as the number of templates or indices increases.
We want to know how the system handles matching templates to indices as data grows.
Analyze the time complexity of applying index templates during index creation.
PUT _index_template/template_1
{
"index_patterns": ["logs-*"],
"template": {
"settings": {"number_of_shards": 1}
}
}
PUT /logs-2024-06
{
"aliases": {"current_logs": {}}
}
This snippet creates a template matching indices starting with "logs-" and then creates an index that triggers template application.
When an index is created, Elasticsearch checks all templates to find matches.
- Primary operation: Matching index patterns against the new index name.
- How many times: Once per template for each index creation.
As the number of templates (n) grows, the system must check each template's pattern against the index name.
| Input Size (n templates) | Approx. Operations |
|---|---|
| 10 | 10 pattern checks |
| 100 | 100 pattern checks |
| 1000 | 1000 pattern checks |
Pattern observation: The number of checks grows directly with the number of templates.
Time Complexity: O(n)
This means the time to apply templates grows linearly with the number of templates.
[X] Wrong: "Applying templates happens instantly no matter how many templates exist."
[OK] Correct: Each template must be checked against the index name, so more templates mean more checks and more time.
Understanding how template matching scales helps you design efficient index management and shows you can reason about system behavior as data grows.
"What if we changed from multiple templates to a single template with multiple patterns? How would the time complexity change?"