TBA TBA - 3 months ago 56
Groovy Question

Date Difference with Groovy Script Elastic Search

I am trying to create a Sort based on an inline Groovy Script with following code.

Basically trying to minus a date field with current time and sort it using the returned difference.

"sort": [
{
"_script": {
"script": "DateTime date = new DateTime(doc['enddate'].value); return date.minus(date , '2016-08-12T12:24:03');",
"type": "number",
"order": "asc"
}
}]


However this method is not correct.

Val Val
Answer

You can try this

{
  "sort": [
    {
      "_script": {
        "script": "DateTime.now().millis - doc.enddate.date.millis",
        "type": "number",
        "order": "asc"
      }
    }
  ]
}