# Using variable with ctx.\_source in painless

**URL:** <https://discuss.elastic.co/t/using-variable-with-ctx-source-in-painless/222112>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 4, 2020, 4:21pm UTC](https://discuss.elastic.co/t/using-variable-with-ctx-source-in-painless/222112 "2020-03-04T16:21:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![uddhav](https://avatars.discourse-cdn.com/v4/letter/u/67e7ee/32.png) [@uddhav](https://discuss.elastic.co/u/uddhav)\
**Post date:** [March 4, 2020, 4:21pm UTC](https://discuss.elastic.co/t/using-variable-with-ctx-source-in-painless/222112/1 "2020-03-04T16:21:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 1, 2020, 4:22pm UTC](https://discuss.elastic.co/t/using-variable-with-ctx-source-in-painless/222112/2 "2020-04-01T16:22:01Z")

</div>

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