Elasticsearch无法使用自定义格式解析日期

时间:2015-02-24 22:04:05

标签: parsing date elasticsearch jodatime datetime-format

根据文档(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html),应该可以使用Joda时间兼容的日期模式来解析日期。

我的数据包含以下日期:“2015-02-09 02:10:05,245”。

可以使用以下模式使用Joda时间解析这些:“yyyy-MM-dd HH:mm:ss,SSS”。

但是,当我告诉我的索引使用此模式来解析日期时,将数据加载到Elasticsearch会失败并显示以下错误:

org.elasticsearch.index.mapper.MapperParsingException: failed to parse [mydate]
    at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:416)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeValue(ObjectMapper.java:709)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:500)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:542)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:491)
    at org.elasticsearch.index.shard.service.InternalIndexShard.prepareCreate(InternalIndexShard.java:392)
    at org.elasticsearch.action.bulk.TransportShardBulkAction.shardIndexOperation(TransportShardBulkAction.java:444)
    at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:150)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:512)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:419)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.index.mapper.MapperParsingException: failed to parse date field [2015-02-09 02:10:05,245], tried both date format [dateOptionalTime], and timestamp number with locale []
    at org.elasticsearch.index.mapper.core.DateFieldMapper.parseStringValue(DateFieldMapper.java:621)
    at org.elasticsearch.index.mapper.core.DateFieldMapper.innerParseCreateField(DateFieldMapper.java:549)
    at org.elasticsearch.index.mapper.core.NumberFieldMapper.parseCreateField(NumberFieldMapper.java:235)
    at org.elasticsearch.index.mapper.core.AbstractFieldMapper.parse(AbstractFieldMapper.java:406)
    ... 12 more
Caused by: java.lang.IllegalArgumentException: Invalid format: "2015-02-09 02:10:05,245" is malformed at " 02:10:05,245"
    at org.elasticsearch.common.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:754)
    at org.elasticsearch.index.mapper.core.DateFieldMapper.parseStringValue(DateFieldMapper.java:615)
    ... 15 more

在索引模板中,'mydate'的指定方式如下:

{
  "template": "te*",
  "mappings": {
    "_default_" : {
      "properties": {
        "mydate": {
          "index": "analyzed",
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss,SSS"
        }
      }
    }
  }
}

应该这样做,对吧?

1 个答案:

答案 0 :(得分:1)

这适用于ES 1.4.4

PUT hilden1

PUT hilden1/type1/_mapping
{
  "properties": {
    "dt": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss,SSS"}
  }
}

POST hilden1/type1
{
  "dt": "2015-02-09 02:10:05,245"
}

GET hilden1/type1/_search