1

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.

David Morton
  • 1,744
  • 2
  • 15
  • 20
  • Does this answer your question? [How to make elasticsearch add the timestamp field to every document in all indices?](https://stackoverflow.com/questions/17136138/how-to-make-elasticsearch-add-the-timestamp-field-to-every-document-in-all-indic) – Aleksi Nov 13 '20 at 05:12

1 Answers1

1

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.

slawek
  • 2,709
  • 1
  • 25
  • 29
  • use this instead of above ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of(\"Z\")) – Naidu May 12 '21 at 19:16