我正在使用下面的代码在elasticsearch索引中插入一些文档。但是当插入 es 时,时间将以 UTC 格式出现,而不是原始的 GMT+5:30Z 格式。索引之前的 Sysout 为我提供了正确的格式 2014-08-11T18:23:13.447+05:30 。我想要原始时间戳用于分析目的。请告诉我如何在文档中保留原始时间格式
public static IndexResponse addDocumentsQuota(Client client, String index,
String type, String categoryName) {
IndexResponse indexResponse = null;
DateTime date = new DateTime();
System.out.println(date);
try {
indexResponse = client.prepareIndex(index, type)
.setSource(jsonBuilder()
.startObject()
.field("@timestamp", date)
.field("category_name", categoryName)
.field("alert_message", "alert message")
.field("creation_time", date)
.endObject())
.execute()
.actionGet();
} catch (ElasticsearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return indexResponse;
}
索引后:
"_source": {
"@timestamp": "2014-08-11T11:57:59.839Z",
"category_name": "newCat1",
"alert_message": "alert message",
"creation_time": "2014-08-11T11:57:59.852Z"
}
谢谢, 苏巴迪普
最佳答案
如果您想在 _source
中保留特定的日期格式文档中,您需要将日期传递到 .field
作为String
。既然你说了 System.out.println()
以您想要的格式打印它,您只需更改为:
...
.field("creation_time", date.toString())
...
无论如何,ES 在内部都会将时间戳存储为 GMT。
关于java - 如何防止 Elasticsearch 在索引时插入日期类型文档作为 UTC 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25295340/