具有日期格式的索引映射,使用 curl 抛出解析异常

Index mapping having date format, using curl throws parsing exception

正在尝试为 ES 7.6 中的 date 字段指定格式(在索引映射中)。以下任何一项均不被接受:

        "createdAt" : {
          "type" : "date",
          "format": "yyyy-MM-dd'''T'''HH:mm:ss.SSSZZ"
        },
        "createdAt" : {
          "type" : "date",
          "format": "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
        },

错误总是一样的:

"type" : "illegal_argument_exception", "reason" : "Invalid format: [yyyy-MM-ddTHH:mm:ss.SSSZZ]: Unknown pattern letter: T",

这里是要重现的完整示例:

curl -X DELETE "localhost:9200/example?pretty"
curl -X PUT   "localhost:9200/example/_mappings?pretty" -H 'Content-Type: application/json' -d' {
      "dynamic": false,
      "properties" : {
        "name" : {
          "type" : "text"
        },
        "createdAt" : {
          "type" : "date",
          "format" : "yyyyMMdd'T'HHmmss.SSSZ"
        }
      }
}'

您可以在 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html 上查看各种支持的日期格式:

您的日期格式的正确格式如下

"format" : "yyyyMMdd'T'HHmmss.SSSZ"(yyyyMMdd 之间没有 -

我刚刚创建了一个格式如下的索引,你可以自己试试:

{
  "mappings": {
    "properties": {
      "date": {
        "type": "date" ,
        "format" : "yyyyMMdd'T'HHmmss.SSSZ" --> notice there is no `-` in yyyyymmdd
      }
    }
  }
}

编辑:- 根据 OP 的最新更新,他正在使用 curl 命令创建索引,因此他需要转义出现在日期 T 字段。

正确的curl命令如下:

curl -X PUT "localhost:9500/example/_mappings?pretty" -H 'Content-Type: application/json' -d' {
      "dynamic": false,
      "properties" : {
        "name" : {
          "type" : "text"
        },
        "createdAt" : {
          "type" : "date",
          "format" : "yyyyMMdd'\''T'\''HHmmss.SSSZ" --> notice escape `T`
        }
      }
}'

在 curl 中给出正确的输出:

{
  "acknowledged" : true
}