Histogram Aggregation
The histogram aggregation groups numeric values into buckets of a specified interval. Let’s group products by price ranges with an interval of $100.
Query:
GET /products/_search
{
"size": 0,
"aggs": {
"price_ranges": {
"histogram": {
"field": "price",
"interval": 100
}
}
}
}
Output:
{
"aggregations": {
"price_ranges": {
"buckets": [
{
"key": 0,
"doc_count": 2
},
{
"key": 100,
"doc_count": 3
},
{
"key": 200,
"doc_count": 1
},
{
"key": 300,
"doc_count": 4
}
]
}
}
}
In this example, products are grouped into price ranges with an interval of $100, and the number of products in each range is counted.
Bucket Aggregation in Elasticsearch
Elasticsearch is a robust tool not only for full-text search but also for data analytics. One of the core features that make Elasticsearch powerful is its aggregation framework, particularly bucket aggregations. Bucket aggregations allow you to group documents into buckets based on certain criteria, making it easier to analyze and summarize your data.
This article will explain what bucket aggregations are, how they work, and provide detailed examples to help you understand their usage.