elasticsearch 索引和范围查询\u时间戳数据,elasticsearch,elasticsearch" /> elasticsearch 索引和范围查询\u时间戳数据,elasticsearch,elasticsearch" />

elasticsearch 索引和范围查询\u时间戳数据

elasticsearch 索引和范围查询\u时间戳数据,elasticsearch,elasticsearch,我正在努力理解时间戳以及如何正确使用它。我正在存储日志数据,所以时间戳非常重要。我希望能够存储时间戳并按范围查询它。每当我使用一个名为_timestamp的字段进行查询时,我似乎永远不会得到任何结果。无论我是使用ISO8601文本格式存储和查询,还是使用毫秒作为长度,都会发生这种情况。有没有人有这样一个完整的例子 使用文本格式时的测试代码(FWIW,这是作为本地节点运行的单元测试代码) 当我使用匹配项执行相同的查询时,我可以看到时间戳字段 "hits" : { "total" : 2,

我正在努力理解时间戳以及如何正确使用它。我正在存储日志数据,所以时间戳非常重要。我希望能够存储时间戳并按范围查询它。每当我使用一个名为_timestamp的字段进行查询时,我似乎永远不会得到任何结果。无论我是使用ISO8601文本格式存储和查询,还是使用毫秒作为长度,都会发生这种情况。有没有人有这样一个完整的例子

使用文本格式时的测试代码(FWIW,这是作为本地节点运行的单元测试代码)

当我使用匹配项执行相同的查询时,我可以看到时间戳字段

"hits" : {
  "total" : 2,
  "max_score" : 1.0,
  "hits" : [ {
    "_index" : "audit-events-2013-03-04",
    "_type" : "log", 
    "_id" : "3d584830-8506-11e2-8365-24be05270b5c",
    "_score" : 1.0,
    "fields" : {
      "_timestamp" : "2013-03-04T20:01:02.003Z"
    }
  }, {
    "_index" : "audit-events-2013-03-04",
    "_type" : "log",
    "_id" : "e6e382e0-84f5-11e2-8365-24be05270b5c",
    "_score" : 1.0,
    "fields" : {
      "_timestamp" : "2013-03-04T18:04:05.006Z"
    }
  } ]

我不明白什么?

答案是需要使用IndexRequest的timestamp方法显式设置时间戳。仅将其设置在名为_timestamp的字段上是不起作用的

IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, "type1", auditEvent.getId().toString())
                .setSource(jsonBuilder()
                        .startObject().
                        field("eventType", auditEvent.getEventType().toString()).
                        auditEvent.getEventTime().toString(ISODateTimeFormat.dateTime())).
                        field("userId", auditEvent.getUserId()).                          endObject()).setTimestamp(Long.toString(auditEvent.getEventTime().getMillis()));
indexRequestBuilder
        .execute()
        .actionGet();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, "type1", auditEvent.getId().toString())
                .setSource(jsonBuilder()
                        .startObject().
                        field("eventType", auditEvent.getEventType().toString()).
                        auditEvent.getEventTime().toString(ISODateTimeFormat.dateTime())).
                        field("userId", auditEvent.getUserId()).                          endObject()).setTimestamp(Long.toString(auditEvent.getEventTime().getMillis()));
indexRequestBuilder
        .execute()
        .actionGet();