# Groovy script compile error and not sure why

**URL:** <https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171>\
**Category:** Elasticsearch\
**Created:** [October 4, 2016, 2:35pm UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171 "2016-10-04T14:35:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgt\_b2002](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@sgt\_b2002](https://discuss.elastic.co/u/sgt_b2002)\
**Post date:** [October 4, 2016, 2:35pm UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/1 "2016-10-04T14:35:57Z")

</div>

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.

> **[Elasticsearch Aggregations: Weekday and Hourly Analysis](https://qbox.io/blog/elasticsearch-aggregations-weekday-hourly-analysis)**
>
> So far we have seen the grouping of documents based on various aggregations like term, date histogram, and more. In this post we are going to see the limitations of the date\_histogram aggregation in certain cases, and how to overcome...

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:

> **[Java SimpleDateFormat Example](https://examples.javacodegeeks.com/core-java/text/java-simpledateformat-example/)**
>
> In this example we will show how to use java.text.SimpleDateFormat class so as to convert a Date int...

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.

---

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [October 26, 2016, 11:33am UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/2 "2016-10-26T11:33:29Z")

</div>

Did you figure it out?  
Thanks

---

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [October 26, 2016, 11:55am UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/3 "2016-10-26T11:55:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [October 27, 2016, 6:35am UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/4 "2016-10-27T06:35:40Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-security.html)

---

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [October 27, 2016, 7:28am UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/5 "2016-10-27T07:28:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yaswanth](https://avatars.discourse-cdn.com/v4/letter/y/94ad74/32.png) [@Yaswanth](https://discuss.elastic.co/u/Yaswanth)\
**Post date:** [January 6, 2017, 5:56pm UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/6 "2017-01-06T17:56:18Z")

</div>

Thankyou for your answer it helped me alot

---

<div class="post-metadata">

**Author:** ![Yaswanth](https://avatars.discourse-cdn.com/v4/letter/y/94ad74/32.png) [@Yaswanth](https://discuss.elastic.co/u/Yaswanth)\
**Post date:** [January 29, 2017, 9:58am UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/7 "2017-01-29T09:58:52Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:03pm UTC](https://discuss.elastic.co/t/groovy-script-compile-error-and-not-sure-why/62171/8 "2017-07-05T22:03:47Z")

</div>


