Search code examples
elasticsearchkibana

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.


Solution

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