弹性搜索无法解析格式为 [strict_date_Optional_time||epoch_millis] 的日期字段

Ore*_*Ric 6 elasticsearch laravel laravel-scout

首先,存储在索引中的日期格式是2021-09-16T14:06:02.000000Z

\n

当我使用用户选项登录remember然后注销时,出现以下错误。

\n

ElasticSearch 的响应是

\n
array:3 [\xe2\x96\xbc\n  "took" => 1\n  "errors" => true\n  "items" => array:1 [\xe2\x96\xbc\n    0 => array:1 [\xe2\x96\xbc\n      "index" => array:5 [\xe2\x96\xbc\n        "_index" => "users"\n        "_type" => "_doc"\n        "_id" => "313"\n        "status" => 400\n        "error" => array:3 [\xe2\x96\xbc\n          "type" => "mapper_parsing_exception"\n          "reason" => "failed to parse field [created_at] of type [date] in document with id '313'. Preview of field's value: '2021-09-16 11:37:49'"\n          "caused_by" => array:3 [\xe2\x96\xbc\n            "type" => "illegal_argument_exception"\n            "reason" => "failed to parse date field [2021-09-16 11:37:49] with format [strict_date_optional_time||epoch_millis]"\n            "caused_by" => array:2 [\xe2\x96\xbc\n              "type" => "date_time_parse_exception"\n              "reason" => "Failed to parse with all enclosed parsers"\n            ]\n          ]\n        ]\n      ]\n    ]\n  ]\n]\n
Run Code Online (Sandbox Code Playgroud)\n

发生这种情况是因为当用户注销时,remember_token属性会被修改,并且由于User模型被修改,索引也会更新。

\n

问题是,当它尝试更新索引时,它尝试存储在索引中的日期格式不再2021-09-16T14:06:02.000000Z

\n

相反,现在是日期格式2021-09-16 11:37:49,因此索引中已有的日期格式和它尝试存储的日期格式存在冲突。

\n

仅当用户framework更新模型时才会发生这种情况。\n如果我自己更新任何模型的属性,则不会发生这种情况。Userlogs out

\n

UPDATED

\n

我刚刚注意到,然后laravel更新remember_token,它禁用了timestamps,这就是日期格式更改为 的原因2021-09-16 11:37:49

\n

但是,我仍然不知道如何解决这个问题。

\n

小智 11

您的应用程序似乎违反了 Elasticsearch 所需的规则(日期类型格式)。输入date格式默认为strict_date_optional_time||epoch_millis,请参阅文档

您可以修复您的应用程序,使其写入2021-09-16T14:06:02.000000Z而不是写入2021-09-16 11:37:49,或者只是写入2021-09-16,因为默认格式需要这样做,请参阅文档内置格式

date_optional_time或者strict_date_optional_time

通用 ISO 日期时间解析器是可选的,其中日期必须至少包含年份和时间(用 T 分隔)。示例:yyyy-MM-dd'T'HH:mm:ss.SSSZyyyy-MM-dd

或者您更改 Elasticsearch 索引的映射以允许在映射时2021-09-16 11:37:49使用多种日期格式。您将需要显式设置索引的类型(_reindex如有必要,然后执行将数据拉入新索引)。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "created_at": {
        "type":   "date",
        "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time ||epoch_millis"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

yyyy-MM-dd HH:mm:ss格式应该能够像2021-09-16 11:37:49.

希望这会有所帮助。