【问题标题】:elastic search failed to parse date field with format [strict_date_optional_time||epoch_millis]弹性搜索无法解析格式为 [strict_date_optional_time||epoch_millis] 的日期字段
【发布时间】:2021-09-16 14:31:09
【问题描述】:

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

当我使用remember 用户的选项登录然后我退出时,我收到以下错误。

来自 ElasticSearch 的响应是

array:3 [▼
  "took" => 1
  "errors" => true
  "items" => array:1 [▼
    0 => array:1 [▼
      "index" => array:5 [▼
        "_index" => "users"
        "_type" => "_doc"
        "_id" => "313"
        "status" => 400
        "error" => array:3 [▼
          "type" => "mapper_parsing_exception"
          "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'"
          "caused_by" => array:3 [▼
            "type" => "illegal_argument_exception"
            "reason" => "failed to parse date field [2021-09-16 11:37:49] with format [strict_date_optional_time||epoch_millis]"
            "caused_by" => array:2 [▼
              "type" => "date_time_parse_exception"
              "reason" => "Failed to parse with all enclosed parsers"
            ]
          ]
        ]
      ]
    ]
  ]
]

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

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

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

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

UPDATED

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

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

【问题讨论】:

    标签: laravel elasticsearch laravel-scout


    【解决方案1】:

    您的应用程序似乎违反了 Elasticsearch 要求的规则(日期类型格式)。输入date格式默认为strict_date_optional_time||epoch_millis,见doc

    要么你修复你的应用程序,让它写2021-09-16T14:06:02.000000Z而不是2021-09-16 11:37:49,或者只是2021-09-16,因为默认格式 需要,请参阅文档Built In Formats:

    date_optional_timestrict_date_optional_time

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

    或者您更改 Elasticsearch 索引的映射以允许 2021-09-16 11:37:49 的格式在映射时使用 multiple date formats。您将需要明确设置索引的类型(如果需要,然后执行 _reindex 以将数据拉入新索引)。

    PUT my-index-000001
    {
      "mappings": {
        "properties": {
          "created_at": {
            "type":   "date",
            "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time ||epoch_millis"
          }
        }
      }
    }
    

    yyyy-MM-dd HH:mm:ss 格式应该能够解析类似于2021-09-16 11:37:49 的数据条目。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多