Date Range Aggregation
The date range aggregation groups date values into custom ranges. Let’s group products by custom-sold date ranges.
Query
GET /products/_search
{
"size": 0,
"aggs": {
"sales_date_ranges": {
"date_range": {
"field": "sold_date",
"ranges": [
{ "to": "2023-01-01" },
{ "from": "2023-01-01", "to": "2023-02-01" },
{ "from": "2023-02-01" }
]
}
}
}
}
Output
{
"aggregations": {
"sales_date_ranges": {
"buckets": [
{
"key": "*-2023-01-01T00:00:00.000Z",
"to": 1672531199999,
"doc_count": 2
},
{
"key": "2023-01-01T00:00:00.000Z-2023-02-01T00:00:00.000Z",
"from": 1672531200000,
"to": 1675209599999,
"doc_count": 5
},
{
"key": "2023-02-01T00:00:00.000Z-*",
"from": 1675209600000,
"doc_count": 3
}
]
}
}
}
In this example, products are grouped into custom-sold date ranges, and the number of products sold 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.