Search code examples
elasticsearchelasticsearch-painless

How to get timestamp from date in elastic search script


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().


Solution

  • 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