What is the equivalent of the removeAll function in Painless


#1

We had the following line of Groovy script in 5.X:

"inline":"ctx._source.all_phone.removeAll{it.phone_number == params.remove_id}

It throws the following error in 6.3.0:

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... ource.all_phone.removeAll{it.phone_number == param ...",
" ^---- HERE"
],
"script": "ctx._source.all_phone.removeAll{it.phone_number == params.remove_id}",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... ource.all_phone.removeAll{it.phone_number == param ...",
" ^---- HERE"
],
"script": "ctx._source.all_phone.removeAll{it.phone_number == params.remove_id}",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "invalid sequence of tokens near ['{'].",
"caused_by": {
"type": "no_viable_alt_exception",
"reason": null
}
}
},
"status": 500
}

What I want to do is: remove elements from a nested field which match a given criteria.


#2

I'm not sure if this is what I've originally wanted, but I think it's a nice workaround:

"source":"ctx._source.all_phone = ctx._source.all_phone.stream().filter(x -> x.phone_number != params.remove_id).collect(Collectors.toList())"

So this basically collects every item from the array which matches the given condition, then assigns the result back to the original variable.

Source: https://stackoverflow.com/questions/44679876/elasticsearch-how-do-i-remove-a-nested-object-element


(system) #3

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