# Upsert - merge existing attributes/fields

**URL:** <https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181>\
**Category:** Elasticsearch\
**Created:** [November 7, 2016, 7:58am UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181 "2016-11-07T07:58:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amolbpatil](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@amolbpatil](https://discuss.elastic.co/u/amolbpatil)\
**Post date:** [November 7, 2016, 7:58am UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/1 "2016-11-07T07:58:57Z")

</div>

I'm using ES 2.3.1 and I have a use case where there could be more than 1 documents with given ID and we want to merge those documents.

Example:

- document1 comes with "name:abc"
- again document1 comes with "name: xyz"
- Now I want document1 to have: "name: [abc, xyz]" i.e. merged document.

I thought using upsert will solve my problem, but upsert adds new attributes/fields without merging existing/overlapping fields between 2 documents.

How can I achieve what I want with ES ?

Thanks,  
..... Amol

---

<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:** [November 7, 2016, 8:53am UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/2 "2016-11-07T08:53:31Z")

</div>

Hey,

how about a [scripted upsert](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-update.html#_literal_scripted_upsert_literal) that allows you to write a script that takes care of merging?

--Alex

---

<div class="post-metadata">

**Author:** ![amolbpatil](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@amolbpatil](https://discuss.elastic.co/u/amolbpatil)\
**Post date:** [November 7, 2016, 12:34pm UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/3 "2016-11-07T12:34:25Z")

</div>

Thanks Alexander. Seems script is going to work, but before I do that (enable and try scripts), is there any way to tell ES to merge attributes while calling upsert (i.e. ES would take care of merging instead of we writing a script)?

Thanks,  
..... Amol

---

<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:** [November 7, 2016, 1:45pm UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/4 "2016-11-07T13:45:54Z")

</div>

Hey,

ES cannot really take care of merging, as it is hard to tell what merging means. For one persons it means, all elements of a list should be concatenated together. For someone else it means, that all elements of a list should be concatenated together, but without duplicates. What about deeper inner objects, how should those be merged?

As it is really hard to answer this questions without knowing the application, scripting is there, so you can solve this.

--Alex

---

<div class="post-metadata">

**Author:** ![amolbpatil](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@amolbpatil](https://discuss.elastic.co/u/amolbpatil)\
**Post date:** [November 8, 2016, 8:24am UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/5 "2016-11-08T08:24:57Z")

</div>

Thanks again Alexander.  
I tried scripts and it is working well for me. Thanks again for the pointers.

Thanks,  
..... Amol

---

<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:** [December 6, 2016, 8:25am UTC](https://discuss.elastic.co/t/upsert-merge-existing-attributes-fields/65181/6 "2016-12-06T08:25:02Z")

</div>

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