如何添加"updatedAt"Elasticsearch文档的时间戳 [英] How to add "updatedAt" timestamp to elasticsearch documents

查看:88
本文介绍了如何添加"updatedAt"Elasticsearch文档的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保某个doc_type的所有文档都具有一个"updatedAt"时间戳(ISO 8601),该时间戳将在文档更新时进行更新.它必须是服务器端的时间戳,因为我不知道我是否可以相信所有客户端时间都是同步的.

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.

我使用摄取管道添加"createdAt"时间戳,但似乎不支持使用更新API的管道.

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

我尝试使用更新脚本(使用新提供的'ctx._now'值),但是无法将解析转换为ISO 8601.此外,我不确定更新脚本是否是最可维护的方法,因为每种更新类型都需要自定义脚本.

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.

推荐答案

在我的脚本中,我使用下面的无痛行来标记"updatedAt timestamp":

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区域ID是UTC时区. updatedAt 字段的日期类型设置为 date .奇怪的是,仅将 ctx._now 分配给字段也可以.但是它在来源上看起来与我的其他日期字段不同,因此我更喜欢采用上述方法来保持一致.

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.

这篇关于如何添加"updatedAt"Elasticsearch文档的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆