[英]Java how to format a date to insert in Elastic Search
我正在編寫一個 Java 應用程序以在 Elastic Search 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
創建該格式的掩碼是什么?
謝謝你的幫助
根據文檔,您可以為字段“日期時間”指定多種日期格式。
上面錯誤消息中的日期時間2018-11-23 10:38:00
需要用yyyy-MM-dd HH:mm:ss
映射才能被索引。
請考慮設置時區,甚至更好地使用ISO 8601格式,因為 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時間)
如果您想了解有關 Java 中日期和時間處理的更多背景信息,請查看此。 舊,但金。
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.