How to show day names using date and bar graph aggregation in elascticsearch

I am trying to use date histogram aggregation in elasticsearch and return the date as an era or in yy-mm-dd-mm-ss format. But I want to get the number of documents per week, like on Monday, Tuesday, etc. Is there a way I can do this?

+5
source share
3 answers

You need to take a different approach. Using scripts, you can convert the date to the day of the week. By this value, if you use term aggregation, it should work fine.

Script to convert a date value to a day of the week

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

Query to get 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 of using scripts in aggregates here .

+3
source

Maybe something is missing for me, but the answer is no simpler than the answer. Blame?

 "aggregations": { "timeslice": { "histogram": { "script": "doc['timestamp'].date.getHourOfDay()", "interval": 1, "min_doc_count": 0, "extended_bounds": { "min": 0, "max": 23 }, "order": { "_key": "desc" } } } 

This is good, since it will also include any hours with zero results, and this will expand the results to cover the entire 24-hour period (due to extended_sides).

You can use 'getDayOfWeek', 'getHourOfDay', ... (see "Joda Time" for more).

This is great for a few hours, but for a few days / months it will give you a number, not a name for the month. To work, you can get the time interval as a string - but , this will not work with an extended restriction, so you may have empty results (for example, [Mon, Tue, Fri, VC]).

In case you want it, it is here:

 "aggregations": { "dayOfWeek": { "terms": { "script": "doc['timestamp'].date.dayOfWeek().getAsText()", "order": { "_term": "asc" } } } 

Even if this does not help you, we hope that someone else will find it and benefit from it.

+7
source

For ES5 using painless, this works:

 "aggs": { "dayOfWeek": { "terms": { "script": { "inline": "doc['date_utc'].date.dayOfWeek", "lang": "painless" } } } } 
+1
source

Source: https://habr.com/ru/post/1203482/


All Articles