Metric aggregations (avg, sum, min, max) in Elasticsearch - Time & Space Complexity
When using metric aggregations like average, sum, minimum, and maximum in Elasticsearch, it's important to know how the time to get results changes as data grows.
We want to understand how the work done grows when there are more documents to process.
Analyze the time complexity of the following code snippet.
GET /sales/_search
{
"size": 0,
"aggs": {
"average_price": { "avg": { "field": "price" } },
"total_revenue": { "sum": { "field": "revenue" } },
"lowest_price": { "min": { "field": "price" } },
"highest_price": { "max": { "field": "price" } }
}
}
This query calculates average, sum, minimum, and maximum values for fields across all matching documents.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each document once to read the field values.
- How many times: Once per document in the search results.
As the number of documents grows, the time to compute these metrics grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 field reads and updates |
| 100 | 100 field reads and updates |
| 1000 | 1000 field reads and updates |
Pattern observation: Doubling the number of documents roughly doubles the work done.
Time Complexity: O(n)
This means the time to compute these metrics grows linearly with the number of documents processed.
[X] Wrong: "Metric aggregations instantly return results regardless of data size."
[OK] Correct: Even though Elasticsearch is fast, it still needs to look at each document's field to calculate metrics, so more data means more work.
Understanding how metric aggregations scale helps you explain performance considerations clearly, showing you grasp how data size affects query speed.
"What if we added a filter to reduce documents before aggregation? How would the time complexity change?"
Practice
avg metric aggregation calculate in an Elasticsearch query?Solution
Step 1: Understand the purpose of avg aggregation
Theavgaggregation calculates the average (mean) of values in a numeric field.Step 2: Compare with other metric aggregations
Other aggregations likesum,min, andmaxcalculate total, minimum, and maximum respectively, not average.Final Answer:
The average value of a numeric field across documents -> Option AQuick Check:
avg = average value [OK]
- Confusing avg with sum or count
- Thinking avg returns min or max values
- Mixing avg with document count
price in an Elasticsearch aggregation?Solution
Step 1: Identify the aggregation type for sum
The aggregation type to calculate sum issum.Step 2: Check the syntax structure
The correct syntax uses"sum": { "field": "price" }insideaggs.Final Answer:
"aggs": { "total_price": { "sum": { "field": "price" } } } -> Option AQuick Check:
sum aggregation syntax = "aggs": { "total_price": { "sum": { "field": "price" } } } [OK]
- Using avg or max instead of sum
- Missing the field name inside sum
- Wrong nesting of aggregation keys
max_price aggregation?
{
"aggs": {
"max_price": {
"max": { "field": "price" }
}
}
}
Assuming the documents have prices: 10, 25, 7, 30, 15.Solution
Step 1: Understand max aggregation
Themaxaggregation returns the highest value of the specified field.Step 2: Identify the maximum price from the list
From the prices 10, 25, 7, 30, 15, the maximum is 30.Final Answer:
30 -> Option BQuick Check:
max price = 30 [OK]
- Choosing the second highest value instead of max
- Confusing max with avg or sum
- Ignoring the actual field values
age:
{
"aggs": {
"min_age": {
"min": "age"
}
}
}Solution
Step 1: Check the syntax for min aggregation
Theminaggregation requires the field name inside an object with key "field".Step 2: Identify the error in the given query
The query uses "min": "age" instead of "min": { "field": "age" } which is incorrect syntax.Final Answer:
The field name should be inside a nested object with key "field" -> Option DQuick Check:
min aggregation needs "field": "age" [OK]
- Passing field name as string directly without object
- Confusing aggregation type names
- Assuming query section is mandatory for aggregation
score using the avg, sum, min, and max aggregations in a single Elasticsearch query. Which aggregation structure correctly achieves this?Solution
Step 1: Understand how to combine multiple metric aggregations
Each metric aggregation must be a separate named aggregation insideaggs.Step 2: Analyze each option's structure
{ "aggs": { "avg_score": { "avg": { "field": "score" } }, "sum_score": { "sum": { "field": "score" } }, "min_score": { "min": { "field": "score" } }, "max_score": { "max": { "field": "score" } } } } correctly defines four separate aggregations for avg, sum, min, and max on the fieldscore. { "aggs": { "score_stats": { "stats": { "field": "score" } } } } usesstatswhich returns all metrics but is not separate aggregations. { "aggs": { "score_metrics": { "metric": [ { "avg": { "field": "score" } }, { "sum": { "field": "score" } } ] } } } and D have invalid syntax or unsupported structures.Final Answer:
Separate named aggregations for avg, sum, min, and max inside aggs -> Option CQuick Check:
Multiple metric aggs need separate keys inside aggs [OK]
- Trying to combine metrics inside one aggregation object
- Using unsupported array or metric keys
- Confusing stats aggregation with separate metric aggs
Ready to prove it?
Pass the test and this pattern moves unknown
