Elasticsearch重索引时移除platform.platforms数组元素报空指针异常
Elasticsearch Reindex 空指针异常修复方案
问题根因
空指针异常由字段缺失导致:原索引devops-ucd-000001中存在部分文档没有platform顶层字段,或是platform对象下不存在platforms数组字段,脚本直接调用removeAll方法时访问了空对象触发报错。
修正后执行语句
POST /_reindex { "source": { "index": "devops-ucd-000001" }, "dest": { "index": "temp-ucd" }, "conflicts": "proceed", "script": { "lang": "painless", "inline": """ if (ctx._source.platform != null && ctx._source.platform.platforms != null && ctx._source.platform.platforms instanceof List) { ctx._source.platform.platforms.removeAll(Collections.singleton('1')); } """ } }
注意事项
- 若
platforms数组中存储的1为数字类型而非字符串类型,将代码中的'1'修改为1即可 - 脚本新增的三层校验依次验证
platform字段存在、platform.platforms字段存在、platform.platforms为数组类型,全部通过后才执行元素删除逻辑,自动跳过结构不匹配的文档避免异常
内容的提问来源于stack exchange,提问作者Tom Smith
相关产品推荐
相关产品推荐

