使用Elasticsearch时,需要用到parent-child API,建立parent的mapping后,动态更新child的field字段时,出现常见的NEP——NullPointerException。log日志异常如下:
[2016-04-01 12:04:53,986][INFO ][rest.suppressed ] /test/_mapping/name/ Params: {index=test, type=name} java.lang.NullPointerException at org.elasticsearch.index.mapper.MappedFieldType.checkCompatibility(MappedFieldType.java:246) at org.elasticsearch.index.mapper.internal.ParentFieldMapper.merge(ParentFieldMapper.java:391) at org.elasticsearch.index.mapper.Mapping.merge(Mapping.java:113) at org.elasticsearch.index.mapper.DocumentMapper.merge(DocumentMapper.java:396) at org.elasticsearch.cluster.metadata.MetaDataMappingService$2.execute(MetaDataMappingService.java:389) at org.elasticsearch.cluster.service.InternalClusterService$UpdateTask.run(InternalClusterService.java:388) at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:231) at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:194) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)
head插件显示内容如下:
"error": { "root_cause": [ { "type": "null_pointer_exception", "reason": null } ], "type": "null_pointer_exception", "reason": null }, "status": 500 }
执行的相关操作如下:
- 建立parent mapping
curl -XPUT localhost:9200/test -d‘
{
"mappings":{
"user":{
"dynamic":true,
"properties":{
"id":{"type":"string"},
"age":{"type":"string"},
"gender":{"type":"string"}
}
},
"name":{"_parent":{"type":"user"}}
}
}‘
- 更新child mapping
curl -XPUT localhost:9200/test/_mapping/name -d‘
{
"name": {
"properties": {
"title": {
"type": "string",
"fields": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "long"
}
}
}
}
}
}‘使用中出现这个异常也是没谁了,查找相关文档以及官方说明,最后在git上找到线索,https://github.com/elastic/elasticsearch/issues/15997#issuecomment-171938807。有在ES中查到此问题说明,https://discuss.elastic.co/t/updating-child-mapping-with-new-field-fails-with-nullpointerexception/39256,最终这个问题在这篇文章中给了阐释,https://github.com/elastic/elasticsearch/pull/16013,原来这个问题是ES 2.1.x和2.0.x版本的bug,不过官方在2.2新版本后进行了修复,不过对于第一个链接提到的同此的_source问题,在2.3的官方文档的bug修复中才看到。所以问题的解决办法就是,升级ES到新的版本2.3。