Nested aggregations in Elasticsearch - Time & Space Complexity
When using nested aggregations in Elasticsearch, we want to know how the work grows as we add more data or more levels of aggregation.
We ask: How does the time to get results change when the input size or nesting increases?
Analyze the time complexity of the following Elasticsearch nested aggregation query.
{
"size": 0,
"aggs": {
"by_category": {
"terms": { "field": "category" },
"aggs": {
"by_subcategory": {
"terms": { "field": "subcategory" }
}
}
}
}
}
This query groups documents first by category, then within each category by subcategory.
Look at the repeated steps in this nested aggregation.
- Primary operation: Grouping documents by category, then grouping each category's documents by subcategory.
- How many times: The subcategory grouping runs once for each category group found.
As the number of documents and groups grow, the work increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 documents, 2 categories, 3 subcategories | About 10 grouping steps plus 2 times 3 sub-groupings |
| 100 documents, 5 categories, 10 subcategories | About 100 grouping steps plus 5 times 10 sub-groupings |
| 1000 documents, 20 categories, 50 subcategories | About 1000 grouping steps plus 20 times 50 sub-groupings |
Pattern observation: The total work grows roughly with the number of documents plus the product of category and subcategory groups.
Time Complexity: O(n + k * m)
This means the time grows with the number of documents n plus the number of category groups k times the number of subcategory groups m.
[X] Wrong: "Nested aggregations always take time proportional only to the number of documents."
[OK] Correct: The number of groups created at each level also affects time, so more groups mean more work beyond just counting documents.
Understanding how nested aggregations scale helps you explain how search engines handle grouped data efficiently, a useful skill in many data-related roles.
"What if we added a third level of nested aggregation? How would the time complexity change?"
Practice
Solution
Step 1: Recall aggregation basics
Aggregations group data to summarize or analyze it.Step 2: Examine nested aggregations
Nested aggregations group data inside other groups for detailed multi-level insights.Final Answer:
To group data inside other groups for multi-level analysis -> Option BQuick Check:
Nested aggregations = multi-level grouping [OK]
- Confusing nested aggregations with filters
- Thinking nested aggregations sort data
- Assuming nested aggregations update documents
Solution
Step 1: Identify correct nested aggregation structure
Nested aggregations are placed inside the "aggs" key of a parent aggregation.Step 2: Check "aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } structure
"aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } shows a terms aggregation on "country" with a nested terms aggregation on "city" inside its "aggs" key, which is correct syntax.Final Answer:
"aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } -> Option AQuick Check:
Nested aggs inside parent's "aggs" key = "aggs": { "country": { "terms": { "field": "country" }, "aggs": { "city": { "terms": { "field": "city" } } } } } [OK]
- Placing multiple "aggs" keys at the same level
- Using "nested" keyword incorrectly
- Missing the "aggs" key inside parent aggregation
{
"aggs": {
"by_country": {
"terms": { "field": "country" },
"aggs": {
"by_city": {
"terms": { "field": "city" }
}
}
}
}
}What does the nested aggregation
by_city do?Solution
Step 1: Analyze the outer aggregation
"by_country" groups documents by the "country" field.Step 2: Analyze the nested aggregation
Inside each country group, "by_city" groups documents by the "city" field.Final Answer:
Groups documents by city inside each country group -> Option AQuick Check:
Nested terms aggregation groups inside parent group [OK]
- Thinking nested aggs filter data
- Assuming nested aggs sort data
- Ignoring the parent aggregation grouping
{
"aggs": {
"countries": {
"terms": { "field": "country" },
"terms": {
"field": "city"
}
}
}
}Solution
Step 1: Check aggregation structure
Each aggregation can have only one aggregation type key like "terms" at its level.Step 2: Identify nested aggregation placement
Nested aggregations must be inside an "aggs" key, but here "terms" is repeated instead of nesting inside "aggs".Final Answer:
Missing nested "aggs" key for the city aggregation -> Option DQuick Check:
Nested aggs require "aggs" key inside parent [OK]
- Repeating aggregation type keys at same level
- Not nesting inside "aggs" key
- Confusing aggregation names with field names
Solution
Step 1: Identify the required nesting order
We want to group first by region, then inside each region by category, then inside each category by brand.Step 2: Check each option's structure
{ "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } correctly nests "category" inside "region"'s "aggs", and "brand" inside "category"'s "aggs". Others either flatten or reverse order.Final Answer:
{ "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } -> Option CQuick Check:
Nested aggs inside parent's "aggs" in correct order = { "aggs": { "region": { "terms": { "field": "region" }, "aggs": { "category": { "terms": { "field": "category" }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } [OK]
- Placing all aggregations at top level without nesting
- Repeating aggregation keys at same level
- Reversing the desired grouping order
Ready to prove it?
Pass the test and this pattern moves unknown
