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

How to index document containing ZonedDateTime field in elasticsearch

我正在尝试用 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]

我不明白为什么会收到此错误消息。

我知道怎么做了: ObjectMapper默认不使用JavaTimeModule,我们需要注册它。

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());