弹性搜索中的自定义日期格式

Custom date format in elastic-search

我正在尝试使用 joda-time 库格式在 elastic-search 中格式化日期字段。我想实现格式"Mon Sep 18 17:12:37 IST 2017"。首先我使用了以下代码:

"alarm_timestamp": {
    "type": "date",
    "format": "EEE MMM d H:m:s zzz YYYY"
}

没用。后来看了问的问题here。所以,我更改了代码:

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

在这两种情况下,映射都已成功创建。但是,当我尝试索引数据时出现错误。

它给出的错误是:

{
    "error": {
        "root_cause": [
            {
              "type": "mapper_parsing_exception",
              "reason": "failed to parse [alarm_timestamp]"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "failed to parse [alarm_timestamp]",
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Invalid format: \"Mon Sep 18 17:12:37 IST 2017\" is malformed at \"IST 2017\""
        }
    },
    "status": 400
}

有什么建议吗?

来自 Elasticsearch documentation for format:

Completely customizable date formats are supported. The syntax for these is explained in the Joda docs.

Joda docsz:

Zone names: Time zone names ('z') cannot be parsed.

问题是像 IST 这样的时区缩写可能会产生歧义。

Wikipedia Time Zone 页面内容如下:

Abbreviations
Time zones are often represented by alphabetic abbreviations such as "EST", "WST", and "CST", but these are not part of the international time and date standard ISO 8601 and their use as sole designator for a time zone is discouraged. Such designations can be ambiguous; for example, "ECT" could be interpreted as "Eastern Caribbean Time" (UTC−4h), "Ecuador Time" (UTC−5h), or "European Central Time" (UTC+1h).

所以你会发现有些缩写可以用,有些则不行。不鼓励使用这些缩写。而是使用以下之一:

  • E MMM d H:m:s Z Y 索引为 Mon Sep 18 17:12:37 +02:00 2017

  • E MMM d H:m:s ZZZ Y 索引为 Mon Sep 18 17:12:37 Europe/Istanbul 2017

这是 Joda 的 Available Time Zones 列表。