Working with fields containing underline in painless scripts


#1

Hi, I am writing a painless script for removing values from arrays as described in https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156 and https://stackoverflow.com/questions/45980054/how-to-remove-arraylist-value-in-elastic-search-using-curl but my field names contain underline which cause error. For example first script work fine:
{
"script": {
"lang": "painless",
"inline": "ctx._source.my_field.removeAll(Collections.singleton('valueToRemove'))"
}
}
But second script:
{
"script":
{
"inline": "ctx._source.11_my_field.removeAll(Collections.singleton('valueToRemove'))"
},
"query":
{
"match_all": {}
}
}
caused this error:
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"ctx._source.11_my_field.removeAll(Colle ...",
" ^---- HERE"
],
"script": "ctx._source.11_my_field.removeAll(Collections.singleton('new_name2'))",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"ctx._source.11_my_field.removeAll(Colle ...",
" ^---- HERE"
],
"script": "ctx._source.11_my_field.removeAll(Collections.singleton('new_name2'))",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "unexpected token ['_my_field'] was expecting one of [{, ';'}]."
}
},
"status": 500
}

What should I do? How can I write this script for fields that start with number and contain underline?


(Val Crettaz) #2

Can you try this way?

ctx._source['11_my_field'].removeAll(Collections.singleton('new_name2'))

#3

Thanks, It worked!


(Val Crettaz) #4

Awesome, glad we figured it out!


(system) #5

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