gpt4 book ai didi

java - 如何在elasticsearch中索引包含ZonedDateTime字段的文档

转载 作者:行者123 更新时间:2023-12-02 06:02:38 25 4
gpt4 key购买 nike

我正在尝试存储在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com