gpt4 book ai didi

java - 如何设置日期格式以插入 Elasticsearch

转载 作者:行者123 更新时间:2023-12-02 01:08:14 26 4
gpt4 key购买 nike

我正在编写一个 Java 应用程序以在 Elasticsearch 7.5.1 中插入数据。创建索引时,属性设置如下:

"datetime":{
"type":"date"
}

现在插入日期时出现此错误:

org.elasticsearch.index.mapper.MapperParsingException: failed to parse field [datetime] of type [date] in document with id '195'. Preview of field's value: '2018-11-23 10:38:00'

我目前正在这样做:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String v = dateFormat.format(date);

检查工作索引,我可以看到它的格式如下,例如:2019-10-09T11:11:38.879-04:00

创建该格式的掩码是什么?

最佳答案

根据docs ,您可以为字段“datetime”指定多种日期格式。

上述错误消息中的日期时间 2018-11-23 10:38:00 需要使用 yyyy-MM-dd HH:mm:ss 进行映射> 以便被索引。

请考虑设置时区或更好地使用 ISO 8601 format as elastic 内部将所有日期存储为 UTC。如果您不提供时区,则您正在索引的日期时间将被假定为 UTC,但您的应用程序可能会在不同的时区中运行。这可能会引起麻烦;)

返回解决方案。为日期时间字段定义映射,如下所示:

PUT my_index
{
"mappings": {
"properties": {
"datetime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSSZ||epoch_millis"
}
}
}
}

它将处理日期时间值,例如

  • 2018-11-23 10:38:00(您当前的格式)

  • 2001-07-04T12:08:56.235-0700 (ISO 8601)

  • 1095379198 ( unix time )

如果您想了解有关 Java 中日期和时间处理的更多背景信息,请查看 this 。老了,但是金子。

干杯!

关于java - 如何设置日期格式以插入 Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742496/

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