Applying timezone for painless based time aggregation


(Anil G) #1

I want to apply non standard time interval aggregation (hourOfDay, dayOfWeek etc.) on my date field.

{
   "size":0,
   "aggregations":{
      "groupby_timeinterval":{
         "terms":{
            "script":{
               "source":"def value = doc.start_time.value; return value.hourOfDay",
               "lang":"painless"
            }
         }
      }
   }
}

In ES 2.x, this above term aggregation used to allow "time_zone" parameter that helped us get result aggregates also considering time_zone but that support is no more from ES 5.x.

How can we apply timezone to the above query?
Painless documentation doesn't given out any clues. When I tried to improvise and apply functions on the "value" (of type org.joda.time.MutableDateTime) I am getting "Unable to find dynamic method" error.


(Ryan Ernst) #2

Can you be more specific in the error you get? It is likely the method you are trying to invoke is not whitelisted. Currently, doc values for date fields are exposed as joda time's ReadableDateTime, but the methods allowed are limited. Instead, we are slowly moving towards using the java time api. You can convert to using java time like this:

Instant mydate = Instant.ofEpochMilli(doc.start_time.value.millis);

Then you can convert to your time zone with mydate.atZone(someZoneId)


(system) #3

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