# How to join 2 index using 2 common values

**URL:** <https://discuss.elastic.co/t/how-to-join-2-index-using-2-common-values/234248>\
**Category:** Elasticsearch\
**Created:** [May 26, 2020, 6:32am UTC](https://discuss.elastic.co/t/how-to-join-2-index-using-2-common-values/234248 "2020-05-26T06:32:47Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 26, 2020, 10:19am UTC](https://discuss.elastic.co/t/how-to-join-2-index-using-2-common-values/234248/4 "2020-05-26T10:19:55Z")

</div>

With [transform](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) you can query 2 or more indices and if they have a common field, like an id, you can "join" the results by grouping the common id.

To combine the documents you can use a scripted metric aggregation. It's possible to "collapse" the other documents, which means creating an array of all source documents, or you "join" them, meaning one overwrite the other one. For the later you have to decide the order.

Example post:

> [@Combine multiple document into one document with limited Fields ( merging of documents )](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/6):
>
> 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.: "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 do…

---

_[View the full topic](https://discuss.elastic.co/t/how-to-join-2-index-using-2-common-values/234248)._
