How to add “updatedAt” timestamp to elasticsearch documents

情到浓时终转凉″ 提交于 2021-01-28 08:29:49

问题


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.


回答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.



来源:https://stackoverflow.com/questions/44244230/how-to-add-updatedat-timestamp-to-elasticsearch-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!