Spring data动态创建索引引用实体字段

发布于 2025-01-12 12:32:25 字数 493 浏览 3 评论 0原文

我正在尝试用 @Document 注释我的实体。

为什么要动态创建索引来引用字段lastTime

例如:lastTime = "2022-03-8 00:00:00"

索引应创建为aaa-2022-03-08

我应该如何设置@Doucument中的indexName

@Data
@Document(indexName = "xth_alert-#{}")
public class EsAlert {

  
    @Nullable
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date lastTime;
}

谢谢

I am trying to annotate my entity with @Document.

Are there any why to create Index dynamiclly reference to the field lastTime?

for example: lastTime = "2022-03-8 00:00:00"

Index should be created as aaa-2022-03-08.

How should I set the indexName in @Doucument?

@Data
@Document(indexName = "xth_alert-#{}")
public class EsAlert {

  
    @Nullable
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date lastTime;
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2025-01-19 12:32:25

简短回答:

是的,您可以使用 SpEL 模板表达式摄取实体以存储在索引上,例如

"log-#{T(java.time.LocalDate).now().toString()}"

如果你的lastTime字段是根据当地时间生成的,结果将是这样的,

@Data
@Document(indexName = "log-#{T(java.time.LocalDate).now().toString()}")
public class EsAlert {

  
    @Nullable
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date lastTime;
}

Short answer :

Yes, you can ingest entities to store on indexes with SpEL template expression like

"log-#{T(java.time.LocalDate).now().toString()}"

And if you lastTime field is generated by local time the result will be like this,

@Data
@Document(indexName = "log-#{T(java.time.LocalDate).now().toString()}")
public class EsAlert {

  
    @Nullable
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date lastTime;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文