Bucket aggregations (terms, histogram) in Elasticsearch - Time & Space Complexity
When using bucket aggregations like terms or histogram in Elasticsearch, it's important to know how the work grows as data grows.
We want to understand how the number of operations changes when we group data into buckets.
Analyze the time complexity of the following code snippet.
GET /sales/_search
{
"size": 0,
"aggs": {
"sales_per_category": {
"terms": { "field": "category.keyword", "size": 10 }
}
}
}
This code groups sales documents by category, returning the top 10 categories with their counts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each document once to assign it to a bucket.
- How many times: Once per document in the index.
As the number of documents grows, Elasticsearch processes each document to place it in the right bucket.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks |
| 100 | About 100 document checks |
| 1000 | About 1000 document checks |
Pattern observation: The work grows directly with the number of documents.
Time Complexity: O(n)
This means the time to complete the aggregation grows linearly with the number of documents.
[X] Wrong: "Bucket aggregations only look at the top N buckets, so time stays the same no matter how many documents."
[OK] Correct: Even if we limit buckets, Elasticsearch must scan all documents to count and assign them before picking the top buckets.
Understanding how bucket aggregations scale helps you explain how search engines handle grouping and counting large data sets efficiently.
What if we changed the aggregation to a histogram on a numeric field with many buckets? How would the time complexity change?
Practice
What does the terms aggregation do in Elasticsearch?
Solution
Step 1: Understand the purpose of
Thetermsaggregationtermsaggregation groups documents by unique values of a specified field.Step 2: Recognize the output of
It counts how many documents fall into each unique value group.termsaggregationFinal Answer:
Groups documents by unique values of a field and counts them -> Option BQuick Check:
termsaggregation = grouping by unique values [OK]
- Confusing terms with filtering
- Thinking terms sorts documents
- Mixing terms with metric aggregations
Which of the following is the correct syntax for a histogram aggregation on a field named price with interval 50?
{
"aggs": {
"price_ranges": {
"histogram": {
"field": "price",
"interval": 50
}
}
}
}Solution
Step 1: Check the structure of histogram aggregation
Thehistogramaggregation requires afieldand a numericintervalinside its block.Step 2: Verify the given JSON syntax
The example correctly placesfieldandintervalinside thehistogramobject with interval as a number.Final Answer:
Correct as shown -> Option AQuick Check:
Histogram syntax with numeric interval = correct [OK]
- Using string instead of number for interval
- Confusing terms and histogram aggregations
- Placing interval outside histogram object
Given this aggregation query:
{
"aggs": {
"by_category": {
"terms": { "field": "category.keyword" }
}
}
}What will the aggregation return?
Solution
Step 1: Identify the aggregation type and field
The query uses atermsaggregation on thecategory.keywordfield, which groups by unique values.Step 2: Understand the output of terms aggregation
It returns buckets for each unique category with counts of documents in each bucket.Final Answer:
A list of unique categories with document counts for each -> Option CQuick Check:
Terms aggregation output = unique value buckets with counts [OK]
- Thinking histogram groups string fields
- Expecting average instead of counts
- Confusing filtering with aggregation
Identify the error in this aggregation query:
{
"aggs": {
"price_buckets": {
"histogram": {
"field": "price",
"interval": "100"
}
}
}
}Solution
Step 1: Check the type of the interval parameter
Theintervalmust be a numeric value, but here it is given as a string "100".Step 2: Understand impact of wrong interval type
Using a string instead of a number causes a syntax error or unexpected behavior in Elasticsearch.Final Answer:
The interval value should be a number, not a string -> Option DQuick Check:
Interval must be numeric, not string [OK]
- Using string instead of number for interval
- Assuming field name is wrong without checking
- Thinking histogram can't be inside aggs
You want to group sales data by price ranges of 100 units and then within each price range, group by product category. Which aggregation structure should you use?
Solution
Step 1: Identify the primary grouping by price ranges
The outer aggregation should be ahistogramon thepricefield with interval 100 to group sales by price ranges.Step 2: Nest the category grouping inside each price range bucket
Inside each price range bucket, use atermsaggregation oncategory.keywordto group by product category.Final Answer:
Aggregation with histogram on price and nested terms on category -> Option AQuick Check:
Histogram outer, terms nested = correct structure [OK]
- Reversing the order of aggregations
- Using terms for numeric price instead of histogram
- Using histogram on keyword fields
Ready to prove it?
Pass the test and this pattern moves unknown
