Basic Histogram Aggregation
To start with histogram aggregation, let’s use the price field to group sales into price ranges. We’ll use an interval of 100.
Query:
GET /sales/_search
{
"size": 0,
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 100
}
}
}
}
Output:
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_histogram": {
"buckets": [
{
"key": 0,
"doc_count": 2
},
{
"key": 1000,
"doc_count": 1
}
]
}
}
}
In this example, the aggregation named price_histogram shows two buckets: one for prices between 0 and 100, and another for prices between 1000 and 1100. The doc_count field indicates the number of sales in each price range.
Data Histogram Aggregation in Elasticsearch
Elasticsearch is a powerful search and analytics engine that allows for efficient data analysis through its rich aggregation framework. Among the various aggregation types, histogram aggregation is particularly useful for grouping data into intervals, which is essential for understanding the distribution and trends within your data.
In this article, we will delve into data histogram aggregation in Elasticsearch, explain its use cases, and provide detailed examples to help you master this powerful feature.