我用以下映射制作了一个Elasticsearch(7.8.1版)文档:
{
"transaction": {
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"settlementEntries": {
"type": "nested",
"properties": {
"settlementDate": {
"type" : "date",
"format" : "uuuu-MM-dd"
},
"settlementId": {
"type": "long"
}
}
},
"transactionId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
我使用的格式为yyyy-MM-dd来存储沉降日期。使用curl进行查询时,可以使用CURL -X GET localhost:9200/transaction/_search
查看数据。但是,当我尝试通过springboot进行相同操作时,会抛出错误。我的实体是:public class TransactionBo {
@Id
private String transactionId;
@Field(type = FieldType.Nested)
private SettlementEntryBo settlementEntries;
}
和public class SettlementEntryBo {
@Id
private Long settlementId;
@Nullable
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
private Date settlementDate;
}
从我可以看出来的问题在于这里的映射:@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd")
private Date settlementDate;
[错误片段]:2020-09-05 02:51:18.105错误10080-[nio-8090-exec-4] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()路径[]引发异常[请求处理失败;嵌套异常为java.time.DateTimeException:无法从TemporalAccessor获取即时消息:{},ISO解析为类型为java.time.format.Parsed的2016-01-01,其根本原因是
java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:InstantSeconds
最佳答案
更改模式以使用uuuu代替yyyy;这is documented here,Elasticsearch中的更改负责:https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats
我看到的另一件事:
您正在使用仅包含年,月和日的日期格式。那是个简单的约会。但是java.util.Date
不是日期,而是UTC区域中的时间瞬时-包括时间戳。
因此,您应该像Ole在其注释中建议的那样更改属性类型java.time.LocalDate
。这些类是在Java 8中引入的,以克服java.util.Date
的所有不足。
关于java - 无法从TemporalAccessor获取即时消息:{},ISO解析为java.time.format.Parsed类型的2018-01-01,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63747516/