Is it possible to apply a timezone to a date in a scripted field?


(Oliver) #1

In the aggregation below I need the bucketing to take account of a passed in timezone. How do I do this? Painless is java based, no? So is it possible to import some sort of timezone module and use that?

  aggs: {
    hourOfDay: {
      terms: {
        script: {
          inline: "doc['created'].date.hourOfDay",
          lang: 'painless',
        },
      },
    },
  },

Aggregation based on hour of day and timezone
(Oliver) #2

In the end this was solved using the aggregation below but at the cost of using Groovy in place of Painless and it also means the security risk of having script.engine.groovy.inline.aggs enabled. Can anyone suggest a method that is more performant and secure?

Is better support for timezones on the roadmap?

{
  "size": 0,
  "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"
          }
        }
      },
      "aggs": {
        "hourOfDay": {
          "terms": {
            "script": {
              "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.hourOfDay",
              "lang": "groovy",
              "params": {
                "tz": "Europe/London"
              }
            }
          }
        }
      }
    }
  }
}

(system) #3

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