0
0
Elasticsearchquery~20 mins

Nested aggregations in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Aggregations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested terms aggregation
What is the output of the following Elasticsearch aggregation query on an index with documents containing category and brand fields?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": { "field": "category.keyword" },
      "aggs": {
        "by_brand": {
          "terms": { "field": "brand.keyword" }
        }
      }
    }
  }
}
AA flat list of all brands with their doc counts, ignoring categories
BA buckets list grouped by category, each containing buckets grouped by brand with their doc counts
CAn error because nested aggregations require a nested field type
DA single bucket with the total doc count of the index
Attempts:
2 left
💡 Hint
Think about how terms aggregations group documents hierarchically.
Predict Output
intermediate
2:00remaining
Result of nested aggregation with metric
Given this aggregation query, what is the value of max_price inside each by_category bucket?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": { "field": "category.keyword" },
      "aggs": {
        "max_price": { "max": { "field": "price" } }
      }
    }
  }
}
AThe highest price value among documents in each category bucket
BAn error because max aggregation cannot be nested inside terms
CThe average price value in each category bucket
DThe highest price value across the entire index, repeated for each category
Attempts:
2 left
💡 Hint
Max aggregation calculates the maximum value within its bucket.
🔧 Debug
advanced
2:00remaining
Identify the error in nested aggregation query
What error will this Elasticsearch query produce?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "nested_agg": {
      "nested": { "path": "comments" },
      "aggs": {
        "by_user": {
          "terms": { "field": "user.keyword" }
        }
      }
    }
  }
}
ASyntaxError due to missing closing brace
BNo error; query runs successfully and returns nested buckets
CField [user.keyword] not found because it is outside the nested path
DAggregation type [nested] is not supported
Attempts:
2 left
💡 Hint
Nested aggregations require fields inside the nested path.
🧠 Conceptual
advanced
1:30remaining
Purpose of nested aggregation in Elasticsearch
Why do we use nested aggregations in Elasticsearch?
ATo flatten nested documents into a single level
BTo speed up queries by avoiding filters
CTo perform aggregations only on root-level fields
DTo aggregate data inside nested objects or arrays within documents
Attempts:
2 left
💡 Hint
Think about how nested fields are stored and queried.
Predict Output
expert
3:00remaining
Output of deeply nested aggregation with filters
What is the output doc count of the innermost bucket in this nested aggregation?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "nested_comments": {
      "nested": { "path": "comments" },
      "aggs": {
        "filtered_comments": {
          "filter": { "term": { "comments.approved": true } },
          "aggs": {
            "by_user": {
              "terms": { "field": "comments.user.keyword" },
              "aggs": {
                "max_likes": { "max": { "field": "comments.likes" } }
              }
            }
          }
        }
      }
    }
  }
}
AThe count of approved comments grouped by user with their max likes
BThe total count of all comments ignoring approval status
CAn error because filter aggregation cannot be nested inside nested aggregation
DThe count of all users in the index regardless of comments
Attempts:
2 left
💡 Hint
Filter aggregation narrows down nested documents before terms aggregation.