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?"