弹性(搜索):查询结果日期格式与存储格式不同

Elastic(search): Query result date format differs from stored format

当我解析查询结果时,弹性日期格式转换出现问题。所以我在日期字段上有一个默认映射如下:

"timestamp" : {
  "type" : "date",
  "format" : "dateOptionalTime"
}

存储为"timestamp":"2015-05-06T08:52:56.387Z" 如果我在该字段上执行最大聚合,我会得到一个长值:

"timestamp_max": {
  "value": 1430902071110
}

但是我希望值与存储的值相同。我读到可以在聚合中指定格式,但它不起作用。我试过了:

"aggregations":{
  "timestamp_max":{
    "max":{
      "field":"timestamp",
      "format" : "dateOptionalTime"
    }
  }
}

但这给出了 SearchParseException ... SearchParseException[[logstash-2015.05.07][0]: query[ConstantScore(BooleanFilter(+no_cache(timestamp:[1429357190515 TO 1431949190515])))],from[-1],size[-1]: Parse Failure [Unexpected token VALUE_STRING in [timestamp_max].]]; ...

我做错了什么?

此致, 一月

你快到了。您只需要使用 correct formatting pattern 指定日期格式,如下所示:

"aggregations":{
  "timestamp_max":{
    "max":{
      "field":"timestamp",
      "format" : "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    }
  }
}

请注意,这仅适用于 ES 1.5.0 以上版本。参见 ES 上的 related issue github。