# 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:** 1\
**Showing post:** 6

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

---

_[View the full topic](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758)._
