0
0
Elasticsearchquery~20 mins

Bucket aggregations (terms, histogram) in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bucket Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a terms aggregation query
What is the output of this Elasticsearch aggregation query on an index with documents containing a category field?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "category_counts": {
      "terms": {
        "field": "category.keyword",
        "size": 3
      }
    }
  }
}
A{"aggregations":{"category_counts":{"buckets":[{"key":"books","doc_count":5},{"key":"electronics","doc_count":3},{"key":"clothing","doc_count":2}]}}}
B{"aggregations":{"category_counts":{"buckets":[{"key":"books","doc_count":2},{"key":"electronics","doc_count":3},{"key":"clothing","doc_count":5}]}}}
C{"aggregations":{"category_counts":{"buckets":[]}}}
D{"error":"Field [category.keyword] not found"}
Attempts:
2 left
💡 Hint
Terms aggregation groups documents by unique values of the specified field and counts them.
Predict Output
intermediate
2:00remaining
Histogram aggregation bucket counts
Given this histogram aggregation query on a field price, what is the number of buckets returned?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "price_ranges": {
      "histogram": {
        "field": "price",
        "interval": 50
      }
    }
  }
}
A0
B10
C5
DError: interval must be positive
Attempts:
2 left
💡 Hint
Histogram buckets are created by dividing the range of values by the interval.
🧠 Conceptual
advanced
1:30remaining
Understanding bucket aggregation behavior
Which statement correctly describes how the terms aggregation orders buckets by default?
ABuckets are ordered by the term value in ascending order.
BBuckets are ordered by document count in ascending order.
CBuckets are ordered randomly each query execution.
DBuckets are ordered by document count in descending order.
Attempts:
2 left
💡 Hint
Think about which buckets Elasticsearch shows first by default.
Predict Output
advanced
1:30remaining
Error from invalid histogram interval
What error does this histogram aggregation query produce?
Elasticsearch
{
  "size": 0,
  "aggs": {
    "price_histogram": {
      "histogram": {
        "field": "price",
        "interval": 0
      }
    }
  }
}
AError: interval must be greater than 0
BError: interval must be an integer
CError: field [price] not found
DNo error, returns empty buckets
Attempts:
2 left
💡 Hint
Histogram interval cannot be zero or negative.
🚀 Application
expert
3:00remaining
Combining terms and histogram aggregations
You want to find the count of documents grouped first by category.keyword and then by price ranges of 100. Which aggregation structure achieves this?
A
{
  "size": 0,
  "aggs": {
    "categories": {
      "histogram": { "field": "category.keyword", "interval": 100 },
      "aggs": {
        "price_ranges": {
          "terms": { "field": "price" }
        }
      }
    }
  }
}
B
{
  "size": 0,
  "aggs": {
    "categories": {
      "terms": { "field": "category.keyword" },
      "aggs": {
        "price_ranges": {
          "histogram": { "field": "price", "interval": 100 }
        }
      }
    }
  }
}
C
{
  "size": 0,
  "aggs": {
    "price_ranges": {
      "histogram": { "field": "price", "interval": 100 },
      "aggs": {
        "categories": {
          "terms": { "field": "category.keyword" }
        }
      }
    }
  }
}
D
{
  "size": 0,
  "aggs": {
    "categories": {
      "terms": { "field": "category" },
      "aggs": {
        "price_ranges": {
          "histogram": { "field": "price.keyword", "interval": 100 }
        }
      }
    }
  }
}
Attempts:
2 left
💡 Hint
The first aggregation groups by category, then inside each category bucket, the histogram groups by price ranges.