使用弹性搜索版本:6.4.3.我想按字段(类型为日期)分组,几天之间有小时



我想按start_time分组,小时在20190701和20190710之间,但不是每天每小时都是一个桶,我希望数据分为24个桶,例如:20190701,20190801,20190901...落入01桶,20190702,20190802,20190902...落入02桶等等。

这是每天每小时都是一个桶,结果不是我想要的,怎么解决这个问题?

start_time字段类型如下:

"start_time": 
{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_second"
}

我的代码如下:

GET qd_analysis/kw/_search
{
"size": 0, 
"query": {
"bool": {
"must": [
{
"term": {
"uin":   {
"value": "111"
}
}
},
{
"range": {
"imp_date": {
"gte": "20190701",
"lte": "20190710"
}
}
}
]
}
},
"aggs": {
"result": {
"date_histogram": {
"field": "start_time",
"time_zone": "+08:00",
"interval": "hour",
"format": "HH",
"order": {
"_count": "desc"
}
}
}
}
}

我想按start_time分组,小时在20190701和20190710之间,但不是每天每小时都是一个桶,我希望数据分为24个桶,例如:20190701,20190801,20190901... 落入01桶,20190702,20190802,20190902... 落入02桶等等。

您需要使用术语聚合,并使用脚本来提取一天中的小时:

{
"aggs": {
"hour_of_day": {
"terms": {
"script": "doc['@timestamp'].date.hourOfDay"
}
}
}
}

最新更新