Merging two nested fields with update_by_query

325 views Asked by At

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":[]}
1

There are 1 answers

2
Val On

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.