# Combine multiple document into one document with limited Fields ( merging of documents )

**URL:** <https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758>\
**Category:** Elasticsearch\
**Created:** [May 8, 2020, 3:20pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758 "2020-05-08T15:20:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramakrushna.sahu](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ramakrushna.sahu](https://discuss.elastic.co/u/ramakrushna.sahu)\
**Post date:** [May 8, 2020, 3:20pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/1 "2020-05-08T15:20:16Z")

</div>

Combine multiple documents into one document

I have a few documents, for example, 3 documents  
Document - 1  
{  
...  
test: "a"  
...  
}

Document - 2  
{  
...  
test: "b"  
...  
}

Document - 3  
{  
...  
test: "c"  
...  
}

===============Output I am looking for ==============  
Document -  
{  
...  
test: "a", "b","c"  
...  
}

This is like the merging of multiple documents into a single document ( could be in different index).

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 8, 2020, 3:21pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/2 "2020-05-08T15:21:30Z")

</div>

You will need to do that in your application. You can not format results like that in Elasticsearch.

---

<div class="post-metadata">

**Author:** ![ramakrushna.sahu](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ramakrushna.sahu](https://discuss.elastic.co/u/ramakrushna.sahu)\
**Post date:** [May 9, 2020, 4:34am UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/3 "2020-05-09T04:34:16Z")

</div>

Thank you, Christian for the reply. I need some information.  
As per my understating,

1. This can not be achieved using the KQL, DSL directly.
2. This has to be done using the java rest API

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 9, 2020, 6:07am UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/4 "2020-05-09T06:07:34Z")

</div>

You need to have a custom step to post process the response. It can as far as I know not be done in Kibana.

---

<div class="post-metadata">

**Author:** ![ramakrushna.sahu](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ramakrushna.sahu](https://discuss.elastic.co/u/ramakrushna.sahu)\
**Post date:** [May 10, 2020, 2:44pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/5 "2020-05-10T14:44:44Z")

</div>

I got it. Thank you, Christian, for your quick reply.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [May 10, 2020, 3:37pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/6 "2020-05-10T15:37:03Z")

</div>

You can use a [transform](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/transforms.html) for it. The `group_by` must be based on a common criteria, e.g. a common id.

For the combining step, you can use a scripted metric, e.g.:

```auto
"all_docs": {
  "scripted_metric": {
    "init_script": "state.docs = []",
    "map_script": "state.docs.add(new HashMap(params['_source']))",
    "combine_script": "return state.docs",
    "reduce_script": "def docs = []; for (s in states) {for (d in s) { docs.add(d);}}return docs"
  }
}

```

this would create an array with the original docs. If you want to merge them or merge only a specific field, you need to change the example accordingly, the docs contain some more [ideas](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/transform-painless-examples.html).

Hope that helps.

---

<div class="post-metadata">

**Author:** ![ramakrushna.sahu](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ramakrushna.sahu](https://discuss.elastic.co/u/ramakrushna.sahu)\
**Post date:** [May 18, 2020, 5:54pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/7 "2020-05-18T17:54:01Z")

</div>

Thank you for the information. We are able to achieve using Painless script. is there any other way as well?

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [May 18, 2020, 6:20pm UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/8 "2020-05-18T18:20:37Z")

</div>

I am not aware of another solution and I think the painless solution should perform well. Painless is compiled into java byte code and therefore comparable in terms of speed with a pure java implementation.

The only real downside I see is ease of use.

---

<div class="post-metadata">

**Author:** ![ramakrushna.sahu](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@ramakrushna.sahu](https://discuss.elastic.co/u/ramakrushna.sahu)\
**Post date:** [May 22, 2020, 3:33am UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/9 "2020-05-22T03:33:34Z")

</div>

Thank you, Hendrik\_Muhs

---

<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:** [June 19, 2020, 3:33am UTC](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/10 "2020-06-19T03:33:37Z")

</div>

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