【问题标题】:failed to parse field [datefield] of type [date]无法解析 [date] 类型的字段 [datefield]
【发布时间】:2019-06-11 21:18:48
【问题描述】:

我正在尝试索引很多记录,但是在索引 publish_up 字段时遇到了一些麻烦。我默认将该字段映射为日期和格式,但是 我收到此错误:

错误:400 {"error":{"root_cause":[{"type":"mapper_parsing_exception","re​​ason":"无法解析 [date] 类型的字段 [publish_up]"}],"type" :"mapper_parsing_exception","re​​ason":"无法解析 [date] 类型的字段 [publish_up]","caused_by":{"type":"illegal_argument_exception","re​​ason":"格式无效:\"2015-02 -11 00:00:00\" 在 \" 00:00:00\""}},"status":400}

处格式不正确

这是我配置索引的方式:

$params = [
    'index' => 'attachments',
    'body' => [
        'settings' => [ 
            'number_of_shards' => 1,
            'analysis' => [ 
                'analyzer' => [
                    'custom_analizer_texto_sub' => [
                        'type' => 'custom',
                        'tokenizer' => 'keyword',
                        'filter' => ['lowercase']
                    ]
                ]
            ]
        ],
        'mappings' => [
            'article' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'iddoc' => [ 'type' => 'integer'],
                    'publish_up' => [ 'type' => 'date'],//, 'format' => 'YYYY-mm-dd HH:mm:ss'], //Y/m/d H:i:s
                    'textofull' => [ 'type' => 'keyword']
                ]
            ]
        ]
    ]
];
$response = $client->indices()->create($params);

和索引代码(这里我得到错误):

    $params = [
        'index' => 'attachments',
        'type' => 'documentos',
        'id' => $datos->id,
        'body' => [
            'iddoc' => $datos->id,
            'publish_up' => $datos->publish_up,
            'textofull' => $datos->fulltext
        ]
    ];
    $response = $client->index($params);

注意: $datos->publish_up 的日期格式为 2015-02-11 00:00:00。我检查了Documentation,但我无法解决我的问题。

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    由于您的日期格式不是标准的 ISO8601(日期和时间之间缺少T),您需要在映射中添加格式。你做到了,但模式是错误的,因为你使用 YYYY 多年而不是 yyyymm 几个月而不是 MM。试试这样:

                    'publish_up' => [ 'type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'],
                                                                       ^  ^
                                                                       |  |
                                                                   change these
    

    【讨论】:

      【解决方案2】:

      导致此错误的其他一些原因:

      • Elasticsearch 自动映射了一些字段,例如“2021-10-09”,将字段设置为日期,然后发现"" 不是有效日期。解决方案是改用null,或尝试https://stackoverflow.com/a/15950434/733092 中的选项。
      • 您尝试发布从 MongoDB 复制的“JSON”文档,但它实际上是 Mongo 的 Extended JSON 格式,它将日期表示为 {"$date":"2021-10-08T21:46:10.840Z"}。在这种情况下,只需删除包装并将其保存为"2021-10-08T21:46:10.840Z"

      【讨论】:

        猜你喜欢
        • 2020-12-17
        • 2017-05-09
        • 1970-01-01
        • 2022-11-14
        • 2021-01-01
        • 1970-01-01
        • 2021-04-24
        • 2019-04-16
        • 2015-10-22
        相关资源
        最近更新 更多