Using variable with ctx._source in painless

Hi,

I am using painless script in update_by_query. I have a field called DocFieldPath which is a location in a document and is different for all documents. I have to patch some information in all docs at this DocFieldPath.

For example,
"DocFieldPath": "ListOfResourcesForTheTeam.0.Resource"
"DocFieldPath": "Owner"

I have some data in params.ref . This is to be patched at the particular docFieldPath in the docs.

What the script should be doing is this:
ctx._source.ListOfResourcesForTheTeam.0.Resource.putAll(params.ref)
ctx._source.Owner.putAll(params.ref)

But I am not able to use variables with ctx._source
I tried many things:

"script":{ "lang": "painless", "source": " def docPath = ctx._source.DocFieldPath; ctx._source.$docPath.putAll(params.ref);" }

Error:
"type": "script_exception",
"reason": "compile error",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "unexpected character [$].",
"caused_by": {
"type": "lexer_no_viable_alt_exception",
"reason": null
}
}

"script":{ "lang": "painless", "source": " def docPath = ctx._source.DocFieldPath; ctx._source.{docPath}=params.ref;" } Error: "type": "illegal_argument_exception", "reason": "unexpected character [{].", "caused_by": { "type": "lexer_no_viable_alt_exception", "reason": null } "script":{ "lang": "painless", "source": " ctx._source.\"docPath\".putAll(params.ref);" }

Error:
"type": "script_exception",
"reason": "compile error",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "unexpected character ["].",
"caused_by": {
"type": "lexer_no_viable_alt_exception",
"reason": null
}
}

Can someone please give some pointers for this. Any leads will be appreciated.
Thanks.

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