【问题标题】:How to add "updatedAt" timestamp to elasticsearch documents如何将“updatedAt”时间戳添加到弹性搜索文档
【发布时间】:2017-11-03 18:38:52
【问题描述】:

我想确保某个 doc_type 的所有文档都有一个“updatedAt”时间戳 (ISO 8601),只要文档更新,该时间戳就会更新。它需要是服务器端时间戳,因为我不知道我是否可以相信所有客户端时间都是同步的。

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

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

【问题讨论】:

标签: elasticsearch iso8601 elasticsearch-painless


【解决方案1】:

在我的脚本中,我使用以下无痛行来标记 updatedAt 时间戳:

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

Z 时区 ID 用于 UTC 时区。 updatedAt 字段的日期类型设置为 date。奇怪的是,将ctx._now 分配给字段也可以。但它的源代码看起来与我的其他日期字段不同,所以我更喜欢上述方式来保持一致。

【讨论】:

  • 用这个代替上面的 ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of(\"Z\"))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 2021-07-11
  • 2016-05-20
  • 1970-01-01
  • 2021-06-20
相关资源
最近更新 更多