You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Reactive Spring Data ElasticSearch插入@timestamp字段?

解决Reactive Spring Data ElasticSearch插入@timestamp字段的问题

要让Java实体类的属性映射到ElasticSearch的@timestamp字段,只需在@Field注解中显式指定name属性为"@timestamp"即可,具体修改如下:

修改后的实体类代码

@Document(indexName = "theindex")
public class ElasticPojo {

    @Field(name = "@timestamp", type = FieldType.Date)
    private long timestamp;

    @Id
    private String id;

    // 需补充属性的getter/setter方法,或使用Lombok注解简化代码,Spring Data需要访问属性进行序列化
}

关键说明

  • @Field(name = "@timestamp") 明确告知Spring Data ElasticSearch:将当前Java属性映射到ES中的@timestamp字段
  • 若你的时间戳是毫秒级long值,FieldType.Date可以正确解析并存储为ES的日期类型;如果更习惯使用Java日期API(如Instant、LocalDateTime),也可将属性类型替换为这些类型,Spring Data ElasticSearch会自动处理映射逻辑

仓库接口无需修改,保持原ReactiveCrudRepository实现即可,后续插入操作的逻辑与之前一致,最终写入ES的文档会包含@timestamp字段。

内容的提问来源于stack exchange,提问作者PatPanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:14:53