Elasticsearch & Lucene (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are Metric Aggregations vs Bucket Aggregations?
senior LevelCode ExampleSystem Design
In Simple Words: Aggregations provide real-time analytics across documents:
- Metric Aggregations: Calculate mathematical summaries over a field:
avg,sum,min,max,cardinality(distinct count using HyperLogLog++),percentiles. - Bucket Aggregations: Group documents into sets/buckets based on criteria:
terms(group by value),date_histogram(group by time intervals),range(group by numeric ranges). - Buckets can nest sub-aggregations infinitely (e.g. group by country -> group by month -> calculate average order value).
json
// Group orders by country and calculate total sales:
{
"size": 0,
"aggs": {
"by_country": {
"terms": { "field": "country.keyword" },
"aggs": {
"total_revenue": { "sum": { "field": "amount" } }
}
}
}
}