elasticsearch mapping中的自定义日期格式

时间:2014-10-14 12:14:11

标签: elasticsearch

我正在尝试使用日期格式Tue May 14 17:06:01 PDT 2013索引数据。如Elasticsearch Date Format文档中所述,我需要使用自定义日期格式。我引用了DateTimeFormat文档,其格式为E M d H:m:s z Y

我能够创建映射,但是当我尝试索引数据时,它会给我错误。

映射: -

{
  "tweet": {
    "properties": {
      "user": {
        "type": "string",
        "index": "not_analyzed"
      },
      "message": {
        "type": "string",
        "null_value": "na"
      },
      "postDate": {
        "type": "date",
        "format": "E M d H:m:s z Y"
      },
      "priority": {
        "type": "integer"
      },
      "rank": {
        "type": "float"
      }
    }
  }
}

索引文件: -

curl -XPUT 'http://localhost:9200/tweets/tweet/1' -d '{
        "user" : "kimchy",
        "message" : "This is a tweet!",
        "postDate" : "Tue May 14 17:06:01 PDT 2013",
        "priority" : 4,
        "rank" : 12.3
}'

错误: -

{"error":"MapperParsingException[failed to parse [postDate]]; 
nested: MapperParsingException[failed to parse date field [Tue May 14 17:06:01 PDT 2013],
tried both date format [E M d H:m:s z Y], and timestamp number with locale []];
nested: IllegalArgumentException[Invalid format: \"Tue May 14 17:06:01 PDT 2013\"
is malformed at \"May 14 17:06:01 PDT 2013\"]; ","status":400}

有任何建议吗?

1 个答案:

答案 0 :(得分:7)

月份使用三个M' M。引自API docs

  

月份:3或以上,使用文字,否则使用数字。

因此,您提供的输入的正确映射应为:

    "postDate": {
      "type": "date",
      "format": "E MMM d H:m:s z Y"
    }