elasticsearch-aggregation - 使用弹性搜索版本: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"
}
}
}
}
推荐阅读
- c# - 从python(jupyter notebook)运行c#函数
- scala - 见证抽象类型实现了类型类
- apache-spark - 使用 Spark JDBC 和 Avatica 从 Apache Druid 中的表中读取记录
- python - 无法理解为什么并且无法正常工作
- python - (Python)为什么我的继承不起作用?
- python - AttributeError:当使用数据框处理时间时,只能使用具有 datetimelike 值的 .dt 访问器
- node.js - 从 POSTMAN nodeJs 向 Azure 服务总线发送消息
- python - 尽管 delay_factor=4,但 Netmiko 超时 - OSError:在 send_command_expect 中从未检测到搜索模式
- docker - Dockerfile - 如何使用 ENV 指令附加 PATH?
- sql - 如何使用条件设置自动递增列值