es聚合之实现日期按周一到周日的聚合统计 原创 2021-02-02 13:40:15.0 阅读(5236)次 #### 前要说明 由于业务要查看日志,按周一到周天展示日志统计量  我们数据是存储在es中,找了半天聚合函数,发现可以用脚本定义周一到周日。 这里就需要用到es中的日期类org.elasticsearch.script.JodaCompatibleZonedDateTime 它其中就提供了一个函数getDayOfWeek()获取一周星期几对应的数值,这个函数刚好可以实现我们的需求。 #### getDayOfWeek函数的使用 我们先用script_fields来输出一下这个函数具体有哪些数值。 ```json { "script_fields":{ "day":{ "script" : { "lang": "painless", "source":"def day = doc['collect_time'].value.getDayOfWeek(); return day;" } } }, "size": 10, "query": { "range" : { "collect_time" : { "gte" : "2020-11-01 00:00:00", "lte" : "2020-11-08 23:59:59" } } } } ``` 结果如下: ```json { "took" : 10, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3612, "max_score" : 1.0, "hits" : [ { "_index" : "base_engs_comp_clg_result", "_type" : "doc", "_id" : "3ad92e63785df1cef01aa4af113aa31", "_score" : 1.0, "fields" : { "day" : [ 7 ] } }, { "_index" : "base_engs_comp_clg_result", "_type" : "doc", "_id" : "6974580b43297c9a3e1475f3fb298f17", "_score" : 1.0, "fields" : { "day" : [ 7 ] } } ] } } ``` 上面用day字段显示了getDayOfWeek()函数的返回值为1到7,表示周一到周日。 ps:这里我只展示部分数据,大家可以根据自己的数据来测试这个函数。 #### 实现周一到周天的聚合查询 那如何在聚合中用到这个函数呢? 我查了很久Es的文档,发现它的范围查询可以支持上面的脚本。具体实现的DSL语句如下: ```json { "size": 0, "query": { "range" : { "collect_time" : { "gte" : "2020-11-01 00:00:00", "lte" : "2020-11-08 23:59:59" } } }, "aggs" : { "collect_time_ranges" : { "range" : { "script" : { "lang": "painless", "source": "doc['collect_time'].value.getDayOfWeek()" }, "ranges" : [ { "key":"1","from" : 1, "to" : 2 }, { "key":"2","from" : 2, "to" : 3 }, { "key":"3","from" : 3, "to" : 4 }, { "key":"4","from" : 4, "to" : 5 }, { "key":"5","from" : 5, "to" : 6 }, { "key":"6","from" : 6, "to" : 7 }, { "key":"7","from" : 7 } ] } } } } ``` 返回结果如下: ```json { "took" : 47, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3612, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "collect_time_ranges" : { "buckets" : [ { "key" : "1", "from" : 1.0, "to" : 2.0, "doc_count" : 1063 }, { "key" : "2", "from" : 2.0, "to" : 3.0, "doc_count" : 371 }, { "key" : "3", "from" : 3.0, "to" : 4.0, "doc_count" : 233 }, { "key" : "4", "from" : 4.0, "to" : 5.0, "doc_count" : 159 }, { "key" : "5", "from" : 5.0, "to" : 6.0, "doc_count" : 107 }, { "key" : "6", "from" : 6.0, "to" : 7.0, "doc_count" : 0 }, { "key" : "7", "from" : 7.0, "doc_count" : 1679 } ] } } } ``` elasticsearch 上一篇:mybatis一条语句实现一对一,一对多查询 下一篇:java stream List转Map与List转List与Map转List以及List转Map
相关文章 es7.x版本如何获取查询命中的文档总数(4731) ES各版本的新特性官网地址(2360) 干货 | 吃透Elasticsearch 堆内存以及堆内存为什么不能超过32GB(1837) docker中启动elasticsearch报错:Error opening log file 'logs/gc.log': Permission denied(8171) java xpack密码连接elasticsearch 7.1集群(3316) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(11274) Es创建索引mapping时报错:Root mapping definition has unsupported parameters: [doc(8934) Es 7.x版本批量写入数据路由字段报错:Action/metadata line [1] contains an unknown parameter [_routing](4026) 推荐文章 elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) solr时区设置解决时间多8小时问题(1) linux下MySQL5.7.18安装过程(1) linux下MySQL5.6.2安装过程(1) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) 使用spring4实现websocket连接(1) jquery对象与dom对象互转(1) 热门文章 clickhouse如何删除数据(40016) clickhouse清空删除表数据(18228) redis报错远程主机强迫关闭了一个现有的连接以及超时问题(16275) clickhouse执行查询内存超出限制问题:Memory limit (total) exceeded(14617) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(11061) clickhouse如何修改字段名(9931) druid执行clickhouse报错:sql injection violation, dbType clickhouse , druid-version 1.2.2, syntax error(8915) Es创建索引mapping时报错:Root mapping definition has unsupported parameters: [doc(8786) Clickhouse批量删除分区(8708) postgresql生成uuid(8257) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab