ElasticSearch无法解析格式为[strict_date_optional_time]的日期字段||纪元_米利斯]

xfyts7mz  于 2023-01-20  发布在  ElasticSearch
关注(0)|答案(2)|浏览(747)

开始,存储在索引中的日期格式为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
但是,我仍然不知道如何解决这个问题。

ldxq2e6h

ldxq2e6h1#

看起来你的应用程序违反了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_timestrict_date_optional_time
通用ISO日期时间解析器是可选的,其中日期必须至少包括年份和时间(以T分隔)。示例:yyyy-MM-dd'T'HH:mm:ss.SSSZyyyy-MM-dd
或者您可以更改Elasticsearch索引的Map,以允许2021-09-16 11:37:49在Map时使用多种日期格式。您需要显式设置索引的类型(如果需要,之后执行_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的数据条目。
希望这能有所帮助。

7bsow1i6

7bsow1i62#

我刚刚面临同样的问题,这种格式解决了我的问题:2023-01-10 20:15:25.000+0300.
其他格式我已经尝试,但不工作:

  • 2022-01-01
  • 2021-09-16 11:37:49
  • 2023-01-19 05:30:00.000Z
  • 2023-01-19 05:30:00.0000

相关问题