2

Is there a way to convert this code using Painless? the index have a field named created, and the goal is to convert the value to a different timezone, currently it's in UTC.

  "aggregations": {
    "dayOfWeek": {
      "terms": {
        "script": {
          "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.dayOfWeek",
          "lang": "groovy",
          "params": {
            "tz": "Europe/London"
          }
        }
      }
    }
  }
Jane S.
  • 215
  • 1
  • 4
  • 12

1 Answers1

2

after a long long search on google for my own need. This works for me (using painless):

def myDate = doc['created'].value.withZoneSameInstant(ZoneId.of('Europe/London')); 
myDate.dayOfWeek;

I found help on this page: https://gitlab.kveer.fr/upstream/elasticsearch/commit/81f60652d8c4763b3470d148239c96bcd0c29553

Mikael B
  • 21
  • 2
  • Awesome! This made my day .. was looking for that for a few days already and finally his snippet worked! Thanks. – Andy Feb 16 '22 at 15:40