How to show day names,using date-histogram aggregation in elascticsearch

后端 未结 3 819
生来不讨喜
生来不讨喜 2020-12-09 22:42

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

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 23:19

    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.

提交回复
热议问题