0
votes

I'm performing a query in ElasticSearch that contains the following code:

"aggs": {
  "day_of_week": {
    "terms": {
      "script": "def myDate = new DateTime(doc['timestamp'].date); myDate.withZone(DateTimeZone.forID(timeZoneInfo)).getDayOfWeek()"
      "size": 0
    }
  }
}

The issue is that dynamic scripting is now disabled by default for security reasons, so this will fail with the error "ScriptException[dynamic scripting for [groovy] disabled]". See this page for more information on dynamic scripting: https://www.elastic.co/blog/running-groovy-scripts-without-dynamic-scripting

I'm aware that the suggested way of getting around this is to use a script file. Unfortunately, this does not seem to be supported by the terms aggregation (is it only valid for filter?).

Any help would be much appreciated to get around this, as I can't really leave dynamic scripting enabled for security purposes.

1

1 Answers

1
votes

So it looks like this is possible - I was just using the incorrect format. In my case, I created a groovy script file called "date_time_zoned_hour_of_day" and added it to elasticsearch/config/scripts.

The code looks like:

"aggs": {
  "day_of_week": {
    "terms": {
      "script": "date_time_zoned_hour_of_day",
      "params": {
          "timeZone": "America/Vancouver"
      },
      "size": 0
    }
  }
}