2

I want to get the aggregation result as day of week in text format like Monday, Tuesday etc. I have the following aggregation in which I am getting the result as day of week but in number format like 1 for monday, 2 for tuesday etc.

"aggs": {
   "perWeekDay": {
       "terms": {
           "script": "doc['order_datetime'].date.dayOfWeek"
       }
    }
}

Update: I am doing this using script because I want to add custom field in kibana where I need to mention this script.

pravindot17
  • 1,199
  • 1
  • 15
  • 32
  • you could have a simple mapping on the client-side to map numbers to day names, right? – Val Aug 24 '17 at 11:22
  • Yes, but I am trying to findout is there way in elasticsearch as doc['timestamp'].date.dayOfWeek().getAsText() I have seen this in answers(https://stackoverflow.com/a/31852124/6582942), is there a way to get this in newer version as it's not working. – pravindot17 Aug 24 '17 at 11:30
  • You can always try to format your date with the `EEE` pattern to get the corresponding three-letter day. – Val Aug 24 '17 at 11:39

3 Answers3

1

Resolved the same by doing some script work using conditions.

"aggs": {
   "perWeekDay": {
       "terms": {
           "script": "(doc['order_datetime'].date.dayOfWeek == 1 ? 'Monday' : (doc['order_datetime'].date.dayOfWeek == 2 ? 'Tuesday' : ((doc['order_datetime'].date.dayOfWeek == 3 ? 'Wednesday' : ((doc['order_datetime'].date.dayOfWeek == 4 ? 'Thursday' : ((doc['order_datetime'].date.dayOfWeek == 5 ? 'Friday' : ((doc['order_datetime'].date.dayOfWeek == 6 ? 'Saturday' : 'Sunday'))))))))))"
       }
    }
}
pravindot17
  • 1,199
  • 1
  • 15
  • 32
0

Or you could do it like this

doc['timestamp'].date.dayOfWeek + " (" + ["", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][doc['timestamp'].date.dayOfWeek] + ")"

llermaly
  • 2,331
  • 2
  • 16
  • 29
0

You can easily get that by doing this:

GET <YourIndexName>/_search
{
"size": 0,
"aggs": {
   "perWeekDay": {
       "terms": {
           "script": "doc['order_datetime'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)"
       }
    }
}
}