# Is it possible to merge results of a search for two indexes based on a unique identifier in common? Like a join

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547>\
**Category:** Elasticsearch\
**Created:** [June 17, 2020, 11:33pm UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547 "2020-06-17T23:33:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brendow\_Adriel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brendow_adriel/32/56141_2.png) [@Brendow\_Adriel](https://discuss.elastic.co/u/Brendow_Adriel)\
**Post date:** [June 17, 2020, 11:33pm UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/1 "2020-06-17T23:33:05Z")

</div>

Elasticsearch 7.7 using Java API REST

from what I understand, the merge generates a new index. I just want to merge the final result of 2 indexes based on an identifier. As a join query of a relational database. i'ts possible?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 17, 2020, 11:42pm UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/2 "2020-06-17T23:42:20Z")

</div>

You'd need to extract, merge, and then reindex the data in your own code outside Elasticsearch. But it's possible.

---

<div class="post-metadata">

**Author:** ![Brendow\_Adriel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brendow_adriel/32/56141_2.png) [@Brendow\_Adriel](https://discuss.elastic.co/u/Brendow_Adriel)\
**Post date:** [June 18, 2020, 12:47am UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/3 "2020-06-18T00:47:39Z")

</div>

For example, would I use multiple search for these 2 different indexes and aggregate the results in one? I have a need to create cached searches of a relational database, for performance improvements and various search possibilities. I imagine it is a good use case despite the fact that elasticsearch is not so used for this purpose.

The ideal scenario would be to be able to make a request to the 3 indexes and obtain the results already merged into an object list.

 ![elastic_map](https://us1.discourse-cdn.com/elastic/original/3X/3/9/39f024b07817acfbf91aded800fca793b51149a9.png)

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 18, 2020, 4:14am UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/4 "2020-06-18T04:14:26Z")

</div>

Elasticsearch is used as a cache for a lot of things, it's not uncommon.  
You could also consider [https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-apis.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-apis.html)

Regarding your image, if the document structures are the same in all 3 indices then you can do a single, multi-index query.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 18, 2020, 7:25am UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/5 "2020-06-18T07:25:17Z")

</div>

If there is a one to one relationship between documents sharing an id then an efficient approach is to use the scroll api across the indices, sorted by the common id. Your client can fuse the pairs of docs as it reads through the sorted stream.

---

<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:** [July 16, 2020, 7:25am UTC](https://discuss.elastic.co/t/is-it-possible-to-merge-results-of-a-search-for-two-indexes-based-on-a-unique-identifier-in-common-like-a-join/237547/6 "2020-07-16T07:25:29Z")

</div>

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