Nested aggregations help you group and analyze data inside other groups. This lets you explore details step by step, like looking inside boxes within boxes.
Nested aggregations in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"aggs": {
"group1": {
"terms": { "field": "field1.keyword" },
"aggs": {
"group2": {
"terms": { "field": "field2.keyword" }
}
}
}
}
}Each aggregation can contain another aggregation inside the aggs key.
The inner aggregation works on the results of the outer aggregation.
Examples
Elasticsearch
{
"aggs": {
"by_category": {
"terms": { "field": "category.keyword" },
"aggs": {
"by_brand": {
"terms": { "field": "brand.keyword" }
}
}
}
}
}Elasticsearch
{
"aggs": {
"by_year": {
"date_histogram": {
"field": "date",
"calendar_interval": "year"
},
"aggs": {
"by_month": {
"date_histogram": {
"field": "date",
"calendar_interval": "month"
}
}
}
}
}
}Sample Program
This query counts documents grouped by country, then inside each country by city.
Elasticsearch
{
"size": 0,
"aggs": {
"by_country": {
"terms": { "field": "country.keyword" },
"aggs": {
"by_city": {
"terms": { "field": "city.keyword" }
}
}
}
}
}Important Notes
Nested aggregations let you explore data step by step, like peeling layers of an onion.
Make sure the fields you use for grouping are keyword or not analyzed fields for exact matches.
Summary
Nested aggregations group data inside other groups.
Use the aggs key inside an aggregation to add a nested aggregation.
This helps analyze data in multiple levels, like country then city.
Practice
1. What is the main purpose of nested aggregations in Elasticsearch?
easy
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]
Hint: Nested aggs always use 'aggs' inside another aggregation [OK]
Common Mistakes:
- Confusing nested aggregations with filters
- Thinking nested aggregations sort data
- Assuming nested aggregations update documents
2. Which of the following is the correct syntax to add a nested aggregation inside a terms aggregation in Elasticsearch?
easy
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]
Hint: Nested aggs go inside parent's "aggs" key, not outside [OK]
Common Mistakes:
- Placing multiple "aggs" keys at the same level
- Using "nested" keyword incorrectly
- Missing the "aggs" key inside parent aggregation
3. Given this Elasticsearch aggregation query snippet:
What does the nested aggregation
{
"aggs": {
"by_country": {
"terms": { "field": "country" },
"aggs": {
"by_city": {
"terms": { "field": "city" }
}
}
}
}
}What does the nested aggregation
by_city do?medium
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]
Hint: Nested terms aggs group data inside parent buckets [OK]
Common Mistakes:
- Thinking nested aggs filter data
- Assuming nested aggs sort data
- Ignoring the parent aggregation grouping
4. Identify the error in this nested aggregation query:
{
"aggs": {
"countries": {
"terms": { "field": "country" },
"terms": {
"field": "city"
}
}
}
}medium
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]
Hint: Nested aggs must be inside "aggs", not repeated aggregation keys [OK]
Common Mistakes:
- Repeating aggregation type keys at same level
- Not nesting inside "aggs" key
- Confusing aggregation names with field names
5. You want to analyze sales data by region, then by product category, and finally by brand using nested aggregations. Which of the following query structures correctly implements this three-level nested aggregation?
hard
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]
Hint: Nest each level inside previous level's "aggs" key [OK]
Common Mistakes:
- Placing all aggregations at top level without nesting
- Repeating aggregation keys at same level
- Reversing the desired grouping order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
