# Groovy to Painless; get current date time

**URL:** <https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471>\
**Category:** Elasticsearch\
**Created:** [August 2, 2017, 7:34am UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471 "2017-08-02T07:34:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karolinebryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karolinebryn/32/17095_2.png) [@Karolinebryn](https://discuss.elastic.co/u/Karolinebryn)\
**Post date:** [August 2, 2017, 7:34am UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471/1 "2017-08-02T07:34:34Z")

</div>

I am trying to convert this Groovy script to Painless:

`(0.08 / ((3.16*pow(10,-11.5)) * abs(DateTime.now().getMillis() - doc['created'].date.getMillis()) + 0.05)) + 1.0`

So far this is what I have, but I keep getting the error message "Variable [DateTime] is not defined". How do I get the current DateTime in Painless?

`(0.08 / ((3.16*Math.pow(10,-11.5)) * Math.abs(DateTime.now().getMillis() - doc['created'].date.getMillis()) + 0.05)) + 1.0`

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 2, 2017, 4:15pm UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471/2 "2017-08-02T16:15:24Z")

</div>

See this previous thread which explains what to do (passing in `now` through a param):

> [@Painless get date today](https://discuss.elastic.co/t/painless-get-date-today/81264/2):
>
> Painless intentionally doesn't have any access to now style variables so they can be better cached. The current recommendation is for you to send now style variables in over scripts params. BTW, you should know that this query isn't able to take advantage of the index at all. It'll have to load the @timestamp field for every document and compare it. This much much slower than extracting the day of the week on ingest and adding it to the document.

---

<div class="post-metadata">

**Author:** ![Karolinebryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karolinebryn/32/17095_2.png) [@Karolinebryn](https://discuss.elastic.co/u/Karolinebryn)\
**Post date:** [August 3, 2017, 6:53am UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471/3 "2017-08-03T06:53:27Z")

</div>

I ended up using System.currentTimeMillis(). Will this give me the same problem? And in that case, can you please give me an example of how you would solve it?

```
        {
          "script_score": {
            "script": "(0.08 / ((3.16*Math.pow(10,-11.5)) * Math.abs(System.currentTimeMillis() - doc['created'].date.getMillis()) + 0.05)) + 1.0"
          }
        }
```

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 3, 2017, 4:00pm UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471/4 "2017-08-03T16:00:53Z")

</div>

The problem there is that script will be executed on multiple nodes (where each shard of the index resides), each of which could have a different current time. The way to have a global and consistent time is to pass it in within the request.

```auto
{
  "script_score": {
    "script": {
      "inline": "(0.08 / ((3.16*Math.pow(10,-11.5)) * Math.abs(params['time'] - doc['created'].date.getMillis()) + 0.05)) + 1.0",
      "params": {
        "time": 123456789 /* time set in the request, in millis */
      }
    }
  }
}   

```

---

<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:** [August 31, 2017, 4:00pm UTC](https://discuss.elastic.co/t/groovy-to-painless-get-current-date-time/95471/5 "2017-08-31T16:00:53Z")

</div>

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