Aman Aggarwal Aman Aggarwal - 1 month ago 9
Java Question

Get time adjusted to timezone

I have a date field in my Elasticsearch document.
I am aggregating on that field.

This is my query

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


Now I want to pass the timezone information(offset or timezone name-whichever works) in this line :

"script": "doc['ad_inTime'].date.getHourOfDay()",


and get the date adjusted to timezone.Is there any way to do it?

Val Val
Answer

You can pass the timezone as parameter to your script like this:

{
  "size": 0,
  "aggregations": {
    "timeslice": {
      "histogram": {
        "script": {
          "inline": "doc['openDate'].date.setZone(DateTimeZone.forID(tz)); doc['openDate'].date.getHourOfDay()",
          "params": {
            "tz": "Europe/London"
          }
        },
        "interval": 1,
        "min_doc_count": 0,
        "extended_bounds": {
          "min": 0,
          "max": 23
        },
        "order": {
          "_key": "desc"
        },
        "offset": 0
      }
    }
  }
}
Comments