The Date Histogram Aggregation is a multi-bucket aggregation that groups documents based on a date or timestamp field into buckets representing specific time intervals. It's particularly useful for time-based analysis and visualizations.
Syntax
"<aggregation_name>": {
"date_histogram": {
"field": "<date_field>",
"calendar_interval": "<interval>",
"format": "<date_format>",
"time_zone": "<time_zone>"
}
}
For detailed options and parameters, refer to the official Elasticsearch documentation.
Example Usage
GET /my-index/_search
{
"size": 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "date",
"calendar_interval": "month",
"format": "yyyy-MM-dd",
"time_zone": "UTC"
}
}
}
}
This example groups documents by month based on the "date" field, formatting the results in "yyyy-MM-dd" format and using UTC time zone.
Common Issues
- Incorrect field type: Ensure the specified field is of a date type.
- Invalid interval: Use appropriate interval values (e.g., "year", "quarter", "month", "week", "day", "hour", "minute", "second").
- Time zone discrepancies: Be aware of time zone settings to avoid unexpected results.
- Performance with large datasets: Consider using appropriate date ranges and intervals to manage performance.
Best Practices
- Use
calendar_interval
for calendar-aware intervals andfixed_interval
for exact time durations. - Combine with other aggregations (e.g., sum, avg) for more insightful time-based analytics.
- Utilize the
extended_bounds
parameter to include empty buckets for continuous date ranges. - Consider using
min_doc_count
to filter out buckets with insufficient data.
Frequently Asked Questions
Q: How does the Date Histogram Aggregation handle daylight saving time (DST)?
A: The Date Histogram Aggregation respects DST changes when using calendar_interval
. For consistent bucket sizes regardless of DST, use fixed_interval
.
Q: Can I use Date Histogram Aggregation with nested fields?
A: Yes, you can use it with nested fields by wrapping the Date Histogram Aggregation inside a Nested Aggregation.
Q: How can I get the count of documents for each bucket in the Date Histogram?
A: The count is automatically included for each bucket. You don't need to specify an additional sub-aggregation for this.
Q: Is it possible to have custom date formats in the results?
A: Yes, you can specify custom date formats using the format
parameter in the aggregation definition.
Q: How does Date Histogram Aggregation handle documents with missing date fields?
A: By default, documents with missing date fields are excluded. You can use the missing
parameter to specify a default value for such documents.