ES的按时间的(按天/月/年)聚合统计

本文介绍如何使用DSL查询语句统计特定日期范围内每天的货品种类及数量,通过构建复杂的查询结构实现精确的数据筛选与聚合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我要统计最近一周的每天货品的个数。也就是我查询需要返回哪一天2020-07-18,种类,数量这三个字段,dsl怎么写呢??
比如按天统计
searchdsl := map[string]interface{}{
   "query": map[string]interface{}{
    "bool": map [string]interface{}{      "filter": map[string]interface{}{
          "bool": map[string]interface{}{
              "must": map[string]interface{}{
                  "range": map[string]interface{}{
                    "你的时间": map[string]interface{}{
                      "gte": "2020-07-01 00:00:00",
                      "lte": "2020-07-20 00:00:00"
                    },
                  },
                },
            },
        },
     },
   },
   "size": 0,
   "aggs": map[string]interface{}{
      "group": map[string]interface{}{
         "date_histogram": map[string]interface{}{
            "field": "你的字段",
            "calendar_interval": "day",  //按天统计
            //"time_zone":"+08:00",
            "format": "yyyy-MM-dd",
            "min_doc_count": 0,
         },
         "aggs": map[string]interface{}{
            "group": map[string]interface{}{
               "terms": map[string]interface{}{
                  "field": "你的第二个维度统计",
               },
            },
         },
      },
   },
}

核心:

"aggs": {
"xxxxxx": {
"date_histogram": {
"field": "createdAt",
"interval": "day",
"format": "yyyy-MM-dd",
"time_zone": "+08:00"
}
}
},
配合多维字段统计

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值