1

As the title suggests, I want to get the timestamp (as a number) from a date type in an elastic search painless script. The following attempts didn't work: doc["date_field"].value, doc["date_field"].date.getMillis().

Murukan
  • 189
  • 1
  • 4
  • 15
  • Are you trying to get only the time value from a date type field ? For ex. get 00:10:10 from date field value 18th October, 2018 00:10:10 ? – ben5556 Oct 17 '18 at 12:03
  • @ben5556 I want to get a timestamp, like milliseconds since unix epoch – Murukan Oct 17 '18 at 14:29

1 Answers1

4

According to the Painless docs you should be able to access the milliseconds since epoch like this: doc.date_field.millis.

Date fields are exposed as ReadableDateTime, so they support methods like getYear, getDayOfWeek or e.g. getting milliseconds since epoch with getMillis. To use these in a script, leave out the get prefix and continue with lowercasing the rest of the method name.

You should also be able to use the methods from ReadableDateTime. https://www.elastic.co/guide/en/elasticsearch/painless/6.4/painless-api-reference.html#painless-api-reference-org-joda-time-ReadableDateTime

Tim
  • 1,276
  • 11
  • 23
  • 1
    I have retested with `doc["date_field"].date.millis` and it worked! it seems that I had a bug on previous attempt – Murukan Oct 17 '18 at 14:39