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

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

问题描述

我一直在尝试在弹性搜索中使用日期直方图聚合,并将日期作为纪元或yy-mm-dd-mm-ss格式返回。但是我想要的是每周星期一获得文件数量,星期二等。有没有办法可以做到这一点?

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)

查询以获取值

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

你也可以找到关于在聚合中使用脚本的更多示例 here

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

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

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