Search code examples
elasticsearchiso8601elasticsearch-painless

How to add "updatedAt" timestamp to elasticsearch documents


I want to ensure that all documents of a certain doc_type have a "updatedAt" timestamp (ISO 8601) that gets updated whenever the document is updated. It needs to be a server-side timestamp as I don't know if I can trust that all of the clients times are in sync.

I use an ingest pipeline to add "createdAt" timestamps, but it seems that pipelines are not supported using the update API.

I've tried using update scripts (using the newly available 'ctx._now' value), but cannot get the parsing into ISO 8601 working. Further, I'm not sure that update scripts are the most maintainable way of doing this since every update type would require a custom script.


Solution

  • In my scripts I use following painless line to mark updatedAt timestamp:

    ctx._source.updatedAt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(ctx._now), ZoneId.of("Z"));
    

    Z zone id is for UTC timezone. The updatedAt field has date type set as date. What is weird is that just assigning ctx._now to field also works. But it then looks different in source than rest of my date fields so I prefer the above way to keep things consistent.