# How to INNER JOIN documents from different indexes

**URL:** https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767
**Category:** Elasticsearch
**Created:** [July 19, 2017, 1:13pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767 "2017-07-19T13:13:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![MatheusVellone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusvellone/32/20237_2.png) [@MatheusVellone](https://discuss.elastic.co/u/MatheusVellone)
#### Post date: [July 19, 2017, 1:13pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/1 "2017-07-19T13:13:37Z")

</div>

I have two different indexes:

1. Persons
2. Houses

Then, I need a relationship between them in a way that when I serach the houses it brings me the owner (the person associated) of the house. The inverse isn't required, but would be nice if possible.

I've searched about ElasticSearch relationships and found the **Parent/Child** one, that would solve the problem like a charm: Parent as the Persons and Child as the Houses.  
But I read (and tested) that **Parent/Child** relationship requires both Parent and Child to be in the same index, and in my case I can't put all of them in the same index because that would be a mess due to all data residing in a single index.

In my tests I can create the indexes with types and add documents, but the error occurs when I try to search children with the message `[has_parent] query configured 'parent_type' [parent] is not a valid type` or `[has_child] Type [child] points to a non existent parent type [parent]`

Is there any way to accomplish this behavior using different indexes?

---

<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: [July 19, 2017, 1:30pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/2 "2017-07-19T13:30:21Z")

</div>

The restriction around parent/child is actually even stricter than what you described - all related parents and children have to receive in the same shard within an index. There is no way to achieve this in Elasticsearch, so a common workaround is to perform a client side join in the application.

Depending on how frequently your data is updated, it may be worthwhile looking into de-normalizing the data and storing the two entities together.

---

<div class="post-metadata">

### Author: ![MatheusVellone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusvellone/32/20237_2.png) [@MatheusVellone](https://discuss.elastic.co/u/MatheusVellone)
#### Post date: [July 19, 2017, 1:51pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/3 "2017-07-19T13:51:59Z")

</div>

Hmm, got it.

A client side join can be done, but I'm looking for an option to this.

My **Person** data won't be updated to frequently, but the **Houses** will. In this scenario, the de-normalize approach still a good option ?  
If so, this approach has any limitation or performance risk due to the number of relationships ? ie, in my case this relationship (1-N), the N number can be a high number, something like 1000, or even 100000.

---

<div class="post-metadata">

### Author: ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)
#### Post date: [July 19, 2017, 3:31pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/4 "2017-07-19T15:31:05Z")

</div>

One way to map 1:N relationships in ElasticSearch is to use the [Nested Datatype](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html).

What you could do is create an index for a person with a nested attribute for houses...

---

<div class="post-metadata">

### Author: ![MatheusVellone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusvellone/32/20237_2.png) [@MatheusVellone](https://discuss.elastic.co/u/MatheusVellone)
#### Post date: [July 19, 2017, 5:33pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/5 "2017-07-19T17:33:15Z")

</div>

But I need them to reside in different indexes, or else there would exist only one index for all the documents

---

<div class="post-metadata">

### Author: ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)
#### Post date: [July 20, 2017, 2:37pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/6 "2017-07-20T14:37:23Z")

</div>

You can maintain 3 separate indices, one for Person, one for Houses and another one for Person-HouseMap. This would add a lot of redundancy in your data though.

You won't be able to join the data from one index to another, so I don't know the exact reason behind why you would want to store the person and house data in a separate index...Is there a specific requirement that makes it necessary to store houses and persons in their own separate index?

---

<div class="post-metadata">

### Author: ![MatheusVellone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusvellone/32/20237_2.png) [@MatheusVellone](https://discuss.elastic.co/u/MatheusVellone)
#### Post date: [July 20, 2017, 4:36pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/7 "2017-07-20T16:36:09Z")

</div>

> [@Rory](#):
>
> You can maintain 3 separate indices, one for Person, one for Houses and another one for Person-HouseMap. This would add a lot of redundancy in your data though.

I'm looking for a way to avoid any data redundancy, so this isn't an option.

> [@Rory](#):
>
> Is there a specific requirement that makes it necessary to store houses and persons in their own separate index?

I'm just worried about performance (I may be wrong about that), because in my head separate indexes work better for indexing high volume of data because they're better separated.

I'm not 100% sure about that so please correct me if I'm wrong: If I put all data in a single index, wouldn't I have performance issues because all documents resides on the same index ? If not, I could set the child/parent relationship between the data and my problem is solved.

---

<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: [August 17, 2017, 4:36pm UTC](https://discuss.elastic.co/t/how-to-inner-join-documents-from-different-indexes/93767/8 "2017-08-17T16:36:11Z")

</div>

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