如何显示日期名称,在 Elasticsearch 中使用日期直方图聚合 [英] How to show day names,using date-histogram aggregation in elascticsearch

查看:23
本文介绍了如何显示日期名称,在 Elasticsearch 中使用日期直方图聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 elasticsearch 中使用日期直方图聚合,它以纪元或 yy-mm-dd-mm-ss 格式返回日期.但我想要的是每周获取文档计数,例如 monday 、 tuesday 等.有什么办法可以做到这一点吗?

解决方案

您需要采用不同的方法.使用脚本,您可以将日期时间转换为工作日.在这个值上,如果你应用术语聚合,它应该可以正常工作.

将日期时间值转换为工作日的脚本

Date date = new Date(doc['created_at'].value) ;java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');格式.格式(日期)

查询获取值

<代码>{aggs":{每周日":{条款":{"script": "Date date = new Date(doc['created_at'].value) ; java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');format.format(date)"}}}}

您还可以在此处找到更多关于在聚合中使用脚本的示例.>

I have been trying to use date histogram aggregation in elasticsearch and it returns the date as epoch or in yy-mm-dd-mm-ss format. But what I want is to get document count per week day like monday , tuesday etc. Is there any way in which I can do that?

解决方案

You need to go for a different approach. Using scripts , you can convert date time into week day. On this value if you apply terms aggregation , it should work fine.

Script to convert date time value into weekday

Date date = new Date(doc['created_at'].value) ; 
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');
format.format(date)

Query to get the values

{
  "aggs": {
    "perWeekDay": {
      "terms": {
        "script": "Date date = new Date(doc['created_at'].value) ; java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');format.format(date)"
      }
    }
  }
}

You can also find some more examples on using scripting in aggregations here.

这篇关于如何显示日期名称,在 Elasticsearch 中使用日期直方图聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆