Stats and extended stats in Elasticsearch - Time & Space Complexity
When using stats and extended stats in Elasticsearch, it's important to know how the time to get results grows as data grows.
We want to understand how the number of documents affects the time Elasticsearch takes to calculate these statistics.
Analyze the time complexity of the following Elasticsearch aggregation query.
{
"size": 0,
"aggs": {
"stats_example": {
"stats": { "field": "price" }
},
"extended_stats_example": {
"extended_stats": { "field": "price" }
}
}
}
This query calculates basic stats and extended stats on the "price" field for all matching documents.
Look at what repeats as Elasticsearch processes the data.
- Primary operation: Scanning each document's "price" field to update stats.
- How many times: Once for every document that matches the query.
As the number of documents increases, the work to calculate stats grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations scanning "price" values |
| 100 | 100 operations scanning "price" values |
| 1000 | 1000 operations scanning "price" values |
Pattern observation: The number of operations grows directly with the number of documents.
Time Complexity: O(n)
This means the time to compute stats grows in direct proportion to the number of documents.
[X] Wrong: "Stats calculations are instant no matter how many documents there are."
[OK] Correct: Elasticsearch must look at each document's field value to calculate stats, so more documents mean more work and more time.
Understanding how stats aggregations scale helps you explain performance in real projects and shows you can think about data size impact clearly.
"What if we added a filter to reduce the documents before stats calculation? How would the time complexity change?"
Practice
stats aggregation in Elasticsearch provide by default?Solution
Step 1: Understand the purpose of stats aggregation
Thestatsaggregation provides basic statistical data about numeric fields.Step 2: Identify the default metrics included
By default, it returns count, min, max, avg, and sum values.Final Answer:
Count, min, max, avg, and sum -> Option AQuick Check:
Stats aggregation = basic stats [OK]
- Confusing stats with extended_stats
- Thinking stats includes variance
- Assuming median is included
my_stats on the field price?Solution
Step 1: Check the aggregation type and field key
The aggregation type must beextended_statsand the field key must befield.Step 2: Verify the JSON structure
The correct syntax usesaggsas the root key for aggregations, notaggregations.Final Answer:
{ "aggs": { "my_stats": { "extended_stats": { "field": "price" } } } } -> Option AQuick Check:
Useextended_statswithfieldinsideaggs[OK]
- Using "field_name" instead of "field"
- Using "aggregations" instead of "aggs"
- Confusing stats with extended_stats
{
"aggs": {
"price_stats": {
"extended_stats": {
"field": "price"
}
}
}
}Which of the following metrics will NOT be included in the response?
Solution
Step 1: Identify metrics returned by extended_stats
Theextended_statsaggregation returns count, min, max, avg, sum, sum_of_squares, variance, and std_deviation.Step 2: Check which metric is missing
Median is not included in eitherstatsorextended_statsaggregations.Final Answer:
Median -> Option BQuick Check:
Median not in extended_stats [OK]
- Assuming median is part of extended_stats
- Confusing sum_of_squares with sum
- Thinking variance is not included
{
"aggs": {
"my_stats": {
"extended_stats": {
"field_name": "age"
}
}
}
}Why does this query fail?
Solution
Step 1: Check the field key in extended_stats
The correct key to specify the field isfield, notfield_name.Step 2: Verify other parts of the query
The aggregation name "my_stats" is valid, and "aggs" is the correct root key. The field "age" can be used if it exists.Final Answer:
The field key should be "field", not "field_name" -> Option DQuick Check:
Use "field" key for field name [OK]
- Using "field_name" instead of "field"
- Changing aggregation root key incorrectly
- Assuming field name is invalid without checking
Solution
Step 1: Identify metrics needed
You want average, variance, and standard deviation, which describe spread and central tendency.Step 2: Choose aggregation that provides all metrics
extended_statsprovides avg, variance, std_deviation, sum_of_squares, and more in one aggregation.Step 3: Compare other options
statslacks variance and std deviation;termsgroups data;percentilesgives percentile info, not variance.Final Answer:
Use extended_stats aggregation on price -> Option CQuick Check:
Extended stats = avg + variance + std deviation [OK]
- Using stats and missing variance
- Confusing terms with stats
- Choosing percentiles for variance
Ready to prove it?
Pass the test and this pattern moves unknown
