pravindot17
pravindot17

Reputation: 1273

elasticsearch - get day of the week in text format like Monday, Tuesday etc

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.

Upvotes: 2

Views: 4374

Answers (3)

Bhupinder Sehra
Bhupinder Sehra

Reputation: 11

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)"
       }
    }
}
}

Upvotes: 1

llermaly
llermaly

Reputation: 2500

Or you could do it like this

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

Upvotes: 0

pravindot17
pravindot17

Reputation: 1273

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'))))))))))"
       }
    }
}

Upvotes: 1

Related Questions