0

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 had no luck. The query seems to work but nothing changes in the document.

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
                            }
                         }
                       }

This is the response I get:

{"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":[]}
Maged Makled
  • 1,918
  • 22
  • 25

1 Answers1

0

The _update_by_query endpoint only operates within the context of one document. You cannot "merge" documents that way.

If you want to merge documents you need to use the enrich processor and build an enrich index from some source index and then enrich the destination index from it. The source and destination index can be the same, which would work in your case.

Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks for the reply @Val but as you see in this query, it is only operating on one document, hence the `ctx._id`. I loaded the `events` array from the other document inside of my ruby code and sent it using `update_by_query` – Maged Makled Apr 08 '22 at 00:17
  • Then you don't need this line `ctx._id == params.to_model_id`. If you only need to update a single document, just use the `_update` endpoint or keep using the `_update_by_query` endpoint but add a query to select just that specific `_id`. – Val Apr 08 '22 at 03:29