Groovy script compile error and not sure why

Hi everyone,

Based on some guidance in IRC I came across an article to perform a script aggregation based on converting a date field to put results into buckets based on day of week and hour.

This was precisely what I was looking for at the time and tried to leverage the Groovy script in ES but ran into trouble.

I'm running ES 2.3.5 and attempting to use a file-based script as follows:

$ cat /etc/elasticsearch/scripts/dateConversion.groovy
Date date = new Date(doc[date_field].value);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(format);
format.format(date)

Once the file is copied to that directory I get the following error in my ES log.

[2016-10-03 21:49:51,633][WARN ][script                   ] [test.local] failed to load/compile script [dateConversion]
ScriptException[failed to compile groovy script]; nested: MultipleCompilationErrorsException[startup failed:
90a12f64b30292c6e82909a8299f6e502d162b9c: 2: unable to resolve class java.text.SimpleDateFormat
 @ line 2, column 28.
   java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(format);

This tells me (not a Java or Groovy programmer) that SimpleDateFormat isn't present. However, the following example code compiles and runs fine from the ES node:

I'm clearly missing something, and I'm sure it's painfully obvious to others, but I'd appreciate a bit of guidance if you can. Thanks.

Did you figure it out?
Thanks

I'm not a groovy developer at all but after some research I came up with that solution. So I'm replying to myself.

My script is

Date date = new Date(doc[date_field].value);
date.format(format);

and my request is to get hourly stats.

 GET _search
 {
  "aggs": {
    "byDays": {
      "terms": {
        "script": {
          "file": "dateConversion",
          "params": {
            "date_field": "date",
            "format": "HH"
          } 
        }
      }
    }
  },
  "size": 0
}

HTH

I also found out that if you want to use java.text.SimpleDateFormat you have to update scripting security.

https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html

I'm surprise that nobody from ES team answered to that post. The answer is simple, just mention documentation about security.

Thankyou for your answer it helped me alot

Hi,
I am not getting accurate day for the input dates . Do i need to change the timezone to get a accurate day?

Thanks