Setting timezone in painless source

I am having trouble setting timezone in a query. I am attempting to group data into day of the week over a time period of a month (so all mondays, tuesdays, etc would be grouped). I am doing this by doing a query for the past month using range to start. Then I have aggs with a painless script to group by day of week. This all works fine, except that I cannot seem to add timezone into the painless script.

What I am doing I have seen in another post, though it does not work. I've also tried using atZone() which also throws errors.

This code returns data, but the timezone isn't set. It makes sense to me that this doesn't work since the params variable is not used in the script. I got it from another example though, so included it here.
"total_energy_per_day_of_week": {
"terms": {
"script": {
"lang": "painless",
"source": "doc['timestamp'].value.dayOfWeekEnum",
"params": {
"timeZone": "America/Vancouver"
}
}
}

This snippet throws an error.
"total_energy_per_day_of_week": {
"terms": {
"script": {
"lang": "painless",
"source": "doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",
"params": {
"timeZone": "America/Vancouver"
}
}
},

The error thrown is:
"failures": [
{
"shard": 0,
"index": "index-sensor-state",
"node": "Z6EqbxK7QPezKdaKc615KQ",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",
" ^---- HERE"
],
"script": "doc['timestamp'].value.atZone(ZoneId.of(params.timeZone)).dayOfWeekEnum",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, atZone/1] not found"
}
}

If anyone has any insight into my issue, it would be greatly appreciated!

atZone is a method on LocalDateTime, to return a ZonedDateTime. The value returned from doc values for a date field is a ZonedDateTime (actually a compatible layer over it that mimics both joda and java date apis). I think you want withSameLocalDate(ZoneId), which will return a ZonedDateTime shifted to the given timezone. All date time objects returned from doc values are in UTC timezone.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.