Elastic Search字段映射修改失败:reindex报mapper_parsing_exception求助
Elasticsearch Reindex 失败:mapper_parsing_exception 问题解决
错误原因分析
报错提示object mapping for [sec_1] tried to parse field [null] as object, but found a concrete value,说明原索引templates中存在不符合nested类型要求的脏数据:
- 部分文档的
sec_1字段是null - 部分文档的
sec_1不是数组/对象(比如字符串、数字,因为原映射是text类型,允许存储这类值) - 少数文档的
sec_1是单个对象而非数组(nested类型要求字段值是对象数组)
解决步骤
1. 排查原索引中的脏数据
执行以下查询,找出所有sec_1字段异常的文档:
GET /templates/_search { "query": { "bool": { "should": [ {"term": {"sec_1": null}}, {"script": {"source": "!(ctx._source.sec_1 instanceof List)"}} ], "minimum_should_match": 1 } } }
查看返回的文档内容,确认具体的异常数据类型。
2. 修复脏数据并重新执行Reindex
根据脏数据类型,在Reindex时用脚本处理异常值:
POST _reindex { "source": { "index": "templates" }, "dest": { "index": "templates_new" }, "script": { "source": """ // 处理null值,转为空数组 if (ctx._source.sec_1 == null) { ctx._source.sec_1 = []; } // 处理单个对象,转为数组 else if (!(ctx._source.sec_1 instanceof List)) { ctx._source.sec_1 = [ctx._source.sec_1]; } """ } }
如果存在字符串等完全无法转为nested对象的脏数据,可选择跳过对应文档:
POST _reindex { "source": { "index": "templates" }, "dest": { "index": "templates_new" }, "script": { "source": """ if (typeof ctx._source.sec_1 === 'string') { ctx.op = 'noop'; // 跳过该文档 } else if (ctx._source.sec_1 == null) { ctx._source.sec_1 = []; } else if (!(ctx._source.sec_1 instanceof List)) { ctx._source.sec_1 = [ctx._source.sec_1]; } """ } }
3. 验证新索引映射与数据
执行以下命令确认新索引的sec_1类型为nested:
GET /templates_new/_mapping
返回结果中应包含类似结构:
"sec_1": { "type": "nested", "properties": { "name": {"type": "text"}, "des": {"type": "text"} } }
再随机查询部分文档,确认sec_1字段格式符合要求。
内容的提问来源于stack exchange,提问作者MBD
相关产品推荐
相关产品推荐

