java - 如何防止 Elasticsearch 在索引时插入日期类型文档作为 UTC 时区

标签 java datetime elasticsearch

我正在使用下面的代码在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/

相关文章:

java.lang.OutOfMemory错误: bitmap size exceeds VM budget

node.js - 索引新文档并在同一查询中获取索引文档

java - Elasticsearch 传输客户端连接

java - 写入二进制文件

java - 所有 Maven 项目模块具有相同的时间戳值

php - 如何从 PHP 中的 DateTimeInterface 创建 DateTime?

c# - 从 DateTime 月份和日期(字符串)中删除 '0'(数字)

ruby - Time.parse 和 DateTime.parse 返回不同的结果

elasticsearch - 使用 logstash 将 CSV 地理数据作为 geo_point 类型导入 elasticsearch

java - 在 Javafx 中切换场景时如何保持窗口大小?