Index patterns for time-series in Elasticsearch - Time & Space Complexity
When working with time-series data in Elasticsearch, how fast queries run depends on how indexes are organized.
We want to understand how the number of indexes affects the time it takes to search data.
Analyze the time complexity of this index pattern query.
GET /logs-2023.06.01,logs-2023.06.02,logs-2023.06.03/_search
{
"query": {
"range": {
"timestamp": {
"gte": "2023-06-01T00:00:00",
"lt": "2023-06-04T00:00:00"
}
}
}
}
This query searches across three daily indexes for logs within a date range.
Look at what repeats when the query runs.
- Primary operation: Searching each index separately.
- How many times: Once per index included in the pattern.
As you add more daily indexes to the pattern, the query runs on more indexes.
| Input Size (number of indexes) | Approx. Operations |
|---|---|
| 10 | 10 searches |
| 100 | 100 searches |
| 1000 | 1000 searches |
Pattern observation: The work grows directly with the number of indexes searched.
Time Complexity: O(n)
This means the query time grows linearly as you add more indexes to search.
[X] Wrong: "Searching multiple indexes is as fast as searching one because Elasticsearch is fast."
[OK] Correct: Each index adds extra work because Elasticsearch searches them in parallel, but the total work still grows with the number of indexes.
Understanding how index patterns affect query time helps you design better data storage and faster searches in real projects.
"What if we combined daily indexes into one big index? How would the time complexity change?"
Practice
Solution
Step 1: Understand index patterns role
Index patterns help combine many time-based indexes so you can search them all at once.Step 2: Compare options
Only grouping multiple time-based indexes matches the main purpose; others are unrelated.Final Answer:
To group multiple time-based indexes for easier searching -> Option AQuick Check:
Index patterns = group time-based indexes [OK]
- Thinking index patterns store data permanently
- Confusing index patterns with dashboards
- Assuming index patterns encrypt data
Solution
Step 1: Identify date format in index pattern
Elasticsearch uses dash-separated date parts like YYYY-MM-DD, sologs-2024-01-*matches daily logs in January 2024.Step 2: Check other options
logs-2024.01.*uses dots instead of dashes, which is incorrect.logs_2024-01-*uses underscore instead of dash.logs-01-2024-*has wrong order of date parts.Final Answer:
<code>logs-2024-01-*</code> -> Option BQuick Check:
Date format YYYY-MM-DD with dashes =logs-2024-01-*[OK]
- Using dots instead of dashes in date format
- Mixing underscores and dashes incorrectly
- Reordering date parts wrongly
metrics-2023.12.* and indexes metrics-2023.12.01, metrics-2023.11.30, metrics-2023.12.15, which indexes will match the pattern?Solution
Step 1: Understand the pattern
This pattern matches indexes starting with 'metrics-2023.12.' followed by anything, so it matches December 2023 indexes.metrics-2023.12.*Step 2: Check each index
metrics-2023.12.01andmetrics-2023.12.15match December 2023;metrics-2023.11.30is November, so it does not match.Final Answer:
<code>metrics-2023.12.01</code> and <code>metrics-2023.12.15</code> -> Option CQuick Check:
Pattern matches December indexes only =metrics-2023.12.01andmetrics-2023.12.15[OK]
- Assuming wildcard matches all months
- Ignoring exact prefix before wildcard
- Confusing dot and dash separators
logs-2023-1* to match logs from January 2023, but it also matches October and November indexes. What is the likely mistake?Solution
Step 1: Analyze wildcard usage in
The wildcard * matches any characters after '1', so it matches '1...' (January) but also '10', '11' because '10' and '11' start with '1' in pattern matching.logs-2023-1*Step 2: Understand why October and November match
Because the pattern matches any string starting with '1', it also matches '10' and '11' incorrectly.Final Answer:
Using wildcard * after 1 matches months starting with 1 and 10 -> Option DQuick Check:
Wildcard * after 1 matches more than intended = Using wildcard * after 1 matches months starting with 1 and 10 [OK]
- Thinking * matches only one character
- Confusing dash and dot separators
- Believing index patterns can't match months
events-2024-03-15. Which pattern correctly matches all days in March 2024 but excludes other months?Solution
Step 1: Understand pattern to match daily dates
Daily indexes have two-digit day numbers, so??matches exactly two characters (days 01 to 31).Step 2: Compare options
events-2024-03-*uses *, which matches any length, possibly including invalid days.events-2024-03-??uses??to match exactly two digits.events-2024-03-??*allows extra characters after two digits, which is incorrect.events-2024-03-*andevents-2024-04-*includes April, which should be excluded.Final Answer:
<code>events-2024-03-??</code> -> Option AQuick Check:
Two-question marks match exact two-digit days =events-2024-03-??[OK]
- Using * which matches any length, not exact days
- Including extra months by mistake
- Using patterns that allow invalid day formats
Ready to prove it?
Pass the test and this pattern moves unknown
