_update_by_query throws null_pointer_exception

I've created an index, and I'm trying to add one field. using this script:
POST test_kris_assignment/_update_by_query
{
"script": {
"source": "ctx._source.field1.field2_1 =10",
"lang": "painless"
},
"query": {
"match_all": {}
}
}

field1 and field2_1 fields don't exist, the idea it's to have field1 at parent levell and then values for fiels2_1, field2_2, field2_3.... at same child level. Thank you. The complete error:

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.field1.field2_1.index1 =10",
" ^---- HERE"
],
"script": "ctx._source.field1.field2_1.index1 =10",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.field1.field2_1.index1 =10",
" ^---- HERE"
],
"script": "ctx._source.field1.field2_1.index1 =10",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
},
"status": 400
}

please take your time to properly format your messages. This forums support markdown which in turn allows for super readable snippets.

You need to instantiate the ctx._source.field1 first, before assign a variable inside of that map. Try ctx._source.field1 = [:] first and then your inside assignments.

1 Like

Thank you so much, this was really helpful