一、date_histogram函数
histogram和date_histogram都是Bucket聚合中的常用聚合
[ES官方文档](https://www.elastic.co/guide/index.html)
Kibana写法
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "day" "format": "yyyy-MM-dd", "min_doc_count" : 0, "time_zone" : "+08:00", "extended_bounds" : { "min" : "2014-01-01", "max" : "2014-12-31" } } } } }
field ://你的字段
interval://日期,支持多种关键字year, quarter, month, week, day, hour, minute, second
format://日期格式
min_doc_count:0,//这个参数强制返回空 buckets
time_zone: //时区
extended_bounds://这个参数强制返回整年
java代码
builder.aggregation(AggregationBuilders.dateHistogram("signedAt")
.dateHistogramInterval(DateHistogramInterval.days(1))
.field("signedAt")
.format("yyyy-MM-dd HH:mm:ss")
.minDocCount(0)
.timeZone(DateTimeZone.forID("Asia/Shanghai"))
.offset("+8h")
//默认强制查询此日期范围
.extendedBounds( new ExtendedBounds(DateUtil.firstDay()+" 00:00:00", DateUtil.lastDay()+" 23:59:59"))
复制代码