Painless script_exception : ctx._source : null_pointer_exception

Hello,
I am working with version elasticsearch 6.5.4, I added a new field to my existing index and I want to give it a value equal to the sum of some other fields. I used the following query in Kibana but it does not work, only the first field of the addition operation is recognized :

POST my_index/_update_by_query
{
"script": {
"lang": "painless",
"source": "ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_comments']"
},
"query": {
"match_all": {}
}
}

######Result :
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_comments']",
" ^---- HERE"
],
"script": "ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_comments']",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_comments']",
" ^---- HERE"
],
"ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_comments']",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
},
"status": 500
}

#######

thank you.

Are you certain that all of your documents have a non-null num_comments field?

thank you for your reply;

The fields I would like to add are by default equal to 0 (type: long). If I change the order of the fields on the addition operation, the second field is pointed with the error.

I tested this script on other configuration and it works, now I do not know what is the origin of the problem.

just to correct:
ctx._source['num_attractivity']=ctx._source['num_comments']+ctx._source['num_reactions']

What I meant is that if any of those two fields is not present in any source document, then it is not 0 by default, it is null. So you either need to make sure the each document has both of those fields or to protect against null pointer exceptions in your code.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.