作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试存储在 es 中归档的带有 zonedDateTime 的文档,但出现解析错误:org.elasticsearch.index.mapper.MapperParsingException:无法解析类型为 [date] 的字段 [creationDate]
这是我的文档定义
@Document(indexName = "index", type = "myType")
public class myDocument {
@Id
private String id;
private String text;
@Field(type = FieldType.Date)
private ZonedDateTime creationDate;
....
我收到此错误:
org.elasticsearch.index.mapper.MapperParsingException: failed to parse field [creationDate] of type [date]
at org.elasticsearch.index.mapper.FieldMapper.parse(FieldMapper.java:301) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.index.mapper.DocumentParser.parseObjectOrField(DocumentParser.java:482) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.index.mapper.DocumentParser.parseObject(DocumentParser.java:499) ~[elasticsearch-6.4.3.jar:6.4.3]
...
Caused by: java.lang.IllegalStateException: Can't get text on a START_OBJECT at 1:509
at org.elasticsearch.common.xcontent.json.JsonXContentParser.text(JsonXContentParser.java:86) ~[elasticsearch-x-content-6.4.3.jar:6.4.3]
at org.elasticsearch.common.xcontent.support.AbstractXContentParser.textOrNull(AbstractXContentParser.java:269) ~[elasticsearch-x-content-6.4.3.jar:6.4.3]
at org.elasticsearch.index.mapper.DateFieldMapper.parseCreateField(DateFieldMapper.java:444) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.index.mapper.FieldMapper.parse(FieldMapper.java:295) ~[elasticsearch-6.4.3.jar:6.4.3]
我不明白为什么会收到此错误消息。
最佳答案
我知道如何做到这一点:JavaTimeModule默认不被ObjectMapper使用,我们需要注册它。
public static class CustomEntityMapper implements EntityMapper {
private final ObjectMapper objectMapper;
public CustomEntityMapper() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.registerModule(new CustomGeoModule());
objectMapper.registerModule(new JavaTimeModule());
}
@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
最后将其添加到ElasticsearchTemplate:
new ElasticsearchTemplate(client, new CustomEntityMapper());
关于java - 如何在elasticsearch中索引包含ZonedDateTime字段的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55964635/
我是一名优秀的程序员,十分优秀!