如何使Spring Data Elasticsearch与java.time.LocalDateTime一起使用以获取日期 [英] How to make Spring Data Elasticsearch work with java.time.LocalDateTime for date

查看:1106
本文介绍了如何使Spring Data Elasticsearch与java.time.LocalDateTime一起使用以获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data对Elasticsearch的支持.这是时间戳字段映射:

I am using Spring Data support for Elasticsearch. Here is the timestamp field mapping:

@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true,
        format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
private LocalDateTime timestamp;

这导致在Elasticsearch中映射字段,如下所示:

This results in mapping of the field in Elasticsearch as follows:

"timestamp":{"type":"date","store":true,"format":"yyyy-MM-dd'T'HH:mm:ss.SSSZZ"}

当我使用java.util.Date时,一切正常.但是,当我如上所述切换到java.time.LocalDateTime时,发送到Elasticsearch的文档导致异常.这是文档(为简洁起见,时间戳字段):

When I use java.util.Date instead everything works fine. However, when I switch to java.time.LocalDateTime as above the document sent to Elasticsearch causes an exception. Here is the document (timestamp field only for brevity):

"timestamp": {
    "hour":7, "minute":56, "second":9, "nano":147000000, "year":2017, "month":"FEBRUARY",
    "dayOfMonth":13, "dayOfWeek":"MONDAY", "dayOfYear":44, "monthValue":2, "chronology": {
        "id":"ISO", "calendarType": "iso8601"
    }
}

还有一个例外:

MapperParsingException[failed to parse [timestamp]]; nested: IllegalArgumentException[unknown property [hour]];
(...)
Caused by: java.lang.IllegalArgumentException: unknown property [hour]

在对文档进行json转换时,似乎在这里忽略了该模式.有任何提示吗?也许您可能知道如何在Spring Data中使用内置" _timestamp字段?

It looks like the pattern is being ignored here when jsonizing the document. Any possible tips? Or perhaps you might know how to use the "built-in" _timestamp field with Spring Data?

推荐答案

检查 查看全文

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