# 映射解析异常：无法解析文档 ID 'doc123' 中类型为 [date] 的字段 [created_at]。字段值预览：'2024-13-01'

- **ID:** `elasticsearch/mapping-conflict-on-date-format`
- **领域:** elasticsearch
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

字段值与映射中定义的预期日期格式不匹配，例如无效月份或时间戳模式不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| elasticsearch 7.17 | active | — | — |
| elasticsearch 8.11 | active | — | — |
| elasticsearch 8.12 | active | — | — |

## 解决方案

1. ```
   更新索引映射以接受多种日期格式：PUT /my_index/_mapping { "properties": { "created_at": { "type": "date", "format": "yyyy-MM-dd||yyyy/MM/dd||epoch_millis" } } }
   ```
2. ```
   使用摄取管道在索引前预处理日期字段：PUT _ingest/pipeline/date_fix { "description": "修复日期格式", "processors": [ { "date": { "field": "created_at", "target_field": "created_at", "formats": [ "yyyy-MM-dd" ], "timezone": "UTC", "locale": "en" } } ] }
   ```
3. ```
   使用脚本重新索引数据以转换日期：POST _reindex { "source": { "index": "my_index" }, "dest": { "index": "my_index_new" }, "script": { "source": "if (ctx._source.created_at == '2024-13-01') { ctx._source.created_at = '2024-01-01' }" } }
   ```

## 无效尝试

- **** — Converting date to text loses date-related query capabilities (e.g., range queries, date histograms) and may break existing queries. (70% 失败率)
- **** — This only masks the issue; malformed dates are stored as null, leading to data loss and unexpected query results. (60% 失败率)
- **** — If the incoming data uses a different format (e.g., 'yyyy/MM/dd'), it will still fail after re-creation. (65% 失败率)
