# Merging two nested fields with update\_by\_query

**URL:** <https://discuss.elastic.co/t/merging-two-nested-fields-with-update-by-query/301797>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 6, 2022, 7:46pm UTC](https://discuss.elastic.co/t/merging-two-nested-fields-with-update-by-query/301797 "2022-04-06T19:46:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![magedmakled](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magedmakled/32/69937_2.png) [@magedmakled](https://discuss.elastic.co/u/magedmakled)\
**Post date:** [April 6, 2022, 7:46pm UTC](https://discuss.elastic.co/t/merging-two-nested-fields-with-update-by-query/301797/1 "2022-04-06T19:46:45Z")

</div>

Hello elastic community,

I'm trying to merge two nested fields from two different documents. The nested fields have objects inside of them.

I tried to load the nested array from the first document and add it to the nested field in the second document but have no luck. The query seems to work but nothing change in the document.

```auto
client.update_by_query index: 'event_lists', conflicts: "proceed",
                           body: {
                             "script": {"source": "
                             ArrayList events = params.events;
                             if (ctx._id == params.to_model_id) { 
                               for(event in events) {	
                         	  ctx._source.events.add(event);
                             }}",
                            "lang": "painless",
                            "params": {
                              to_model_id => to_model_id,
                              events => events
                            }
                         }
                       }

```

Here is the response I get

```auto
{"took":9078,"timed_out":false,"total":8,"updated":8,"deleted":0,"batches":1,"version_conflicts":0,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[]}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 7, 2022, 7:19am UTC](https://discuss.elastic.co/t/merging-two-nested-fields-with-update-by-query/301797/2 "2022-04-07T07:19:30Z")

</div>

Hey,

I am not sure I get understand request, maybe you can add a little more details. My biggest issue is, that you cannot access the values of another document in an update by query.

Can you provide a **complete, but minimal example** someone can copy & paste in Kibana Dev Tools including index/document creation and that update by query call?

Thank you!

---

<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:** [May 5, 2022, 7:20am UTC](https://discuss.elastic.co/t/merging-two-nested-fields-with-update-by-query/301797/3 "2022-05-05T07:20:18Z")

</div>

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