# Update parent doc in scripted \_update\_by\_query

**URL:** <https://discuss.elastic.co/t/update-parent-doc-in-scripted-update-by-query/189048>\
**Category:** Elasticsearch\
**Created:** [July 5, 2019, 8:56am UTC](https://discuss.elastic.co/t/update-parent-doc-in-scripted-update-by-query/189048 "2019-07-05T08:56:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJK181](https://avatars.discourse-cdn.com/v4/letter/h/4da419/32.png) [@HJK181](https://discuss.elastic.co/u/HJK181)\
**Post date:** [July 5, 2019, 8:56am UTC](https://discuss.elastic.co/t/update-parent-doc-in-scripted-update-by-query/189048/1 "2019-07-05T08:56:05Z")

</div>

I have a parent/child relation with join datatype. Now I want to update a parent document, based on a query which find's X children. I need to add the value of a field from each child to a field of the parent of that child.

Is it possible to access the parent in a \_update\_by\_query statement? Or is it possible to query the parent document by ID in the script? I could not find any answer yet. Here is what I try to achieve:

```
POST test/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "clusterId": {
              "value": "123"
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "joinType": {
              "value": "c"
            }
          }
        }
      ]
    }
  },
  "script": {
    "source": "if (ctx._source['parent'].queries == null){ctx._source['parent'].queries = new ArrayList()}ctx._source['parent'].queries.add(ctx._source['query'])",
    "lang": "painless"
  }
}
```

---

<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:** [August 2, 2019, 8:56am UTC](https://discuss.elastic.co/t/update-parent-doc-in-scripted-update-by-query/189048/2 "2019-08-02T08:56:10Z")

</div>

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