# Search between two indexes

**URL:** <https://discuss.elastic.co/t/search-between-two-indexes/262216>\
**Category:** Elasticsearch\
**Created:** [January 26, 2021, 9:56am UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216 "2021-01-26T09:56:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohit\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_pandey/32/82910_2.png) [@rohit\_pandey](https://discuss.elastic.co/u/rohit_pandey)\
**Post date:** [January 26, 2021, 9:56am UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/1 "2021-01-26T09:56:23Z")

</div>

I have two indexes:

**Index1**

**| id | name |**  
| 1 | A |  
| 2 | B |  
| 3 | Z |

**Index2**

**| id | stmt\_id | description |**  
| 1 | 11 | AAA  
| 2 | 11 | BBB  
| 3 | 11 | BBC  
| 4 | 22 | DDD  
| 5 | 22 | EEE  
| 6 | 33 | FFF

All records are synced to elasticsearch for both indexes.

I want to match **Index1 --\> name** with **Index2 --\> description**.  
My Input: [1, 2, 3] ( id of Index2 records.)  
Now I want to match all **Index1** records **name** with some **Index2** records **description**.  
And need output something like this:

```
{
    1(id of Index1 record) : [1] (id of index2 record). -- A matching with AAA,
    2 : [2, 3] -- B matching with BBB, BBC,
    3 : [] -- Z is not matching with any Index2 records description.
}

```

I want a query for doing this or any idea will be very helpful.  
Thanks.

---

<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:** [January 26, 2021, 9:37pm UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/2 "2021-01-26T21:37:19Z")

</div>

Welcome to our community! 😃

The Elasticsearch approach to this would be to merge both sets of data into the same document and then index them. There is no way to do a join query like this with Elasticsearch.

---

<div class="post-metadata">

**Author:** ![rohit\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_pandey/32/82910_2.png) [@rohit\_pandey](https://discuss.elastic.co/u/rohit_pandey)\
**Post date:** [January 27, 2021, 3:22am UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/3 "2021-01-27T03:22:18Z")

</div>

Hi,

Can you give me more idea about merging both sets of data into the same document?

---

<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:** [January 27, 2021, 3:25am UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/4 "2021-01-27T03:25:09Z")

</div>

It'd basically be something like;

```auto
| id | name | stmt_id | description |

```

But as a json document, merged before it's sent to Elasticsearch.

---

<div class="post-metadata">

**Author:** ![rohit\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_pandey/32/82910_2.png) [@rohit\_pandey](https://discuss.elastic.co/u/rohit_pandey)\
**Post date:** [January 27, 2021, 3:27am UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/5 "2021-01-27T03:27:54Z")

</div>

I have only 200 documents for **Index 1** and unknown documents for **Index 2**.

---

<div class="post-metadata">

**Author:** ![rohit\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_pandey/32/82910_2.png) [@rohit\_pandey](https://discuss.elastic.co/u/rohit_pandey)\
**Post date:** [February 4, 2021, 12:42pm UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/6 "2021-02-04T12:42:07Z")

</div>

Done with `_msearch` and `script_fields`.

---

<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:** [March 4, 2021, 12:42pm UTC](https://discuss.elastic.co/t/search-between-two-indexes/262216/7 "2021-03-04T12:42:52Z")

</div>

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