# Join on ElasticSearch

**URL:** <https://discuss.elastic.co/t/join-on-elasticsearch/28643>\
**Category:** Elasticsearch\
**Created:** [September 3, 2015, 8:45pm UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643 "2015-09-03T20:45:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 3, 2015, 8:45pm UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/1 "2015-09-03T20:45:33Z")

</div>

Hello ElasticSearch experts,

If I want to do inner join of two query result sets, and wondering if any good examples or documents to refer to? I mean if inner join could be done by ElasticSearch itself of the two sub-query so that I can fetch final results directly? Thanks.

thanks in advance,  
Lin

---

<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:** [September 4, 2015, 2:05am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/2 "2015-09-04T02:05:32Z")

</div>

You cannot do joins in ES. They need to be done with external code.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 4, 2015, 3:23am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/3 "2015-09-04T03:23:26Z")

</div>

@warkolm, could you provide a bit more details what are the external code do you mean and their basic logics? Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 4, 2015, 5:00am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/4 "2015-09-04T05:00:36Z")

</div>

There is a Join like feature I'd say with Parent/Child feature but may be not what you are looking for. And it comes with a memory cost.

If you are coming from a relational model , you' better have to rethink your model instead of trying to apply the same concepts in a Document oriented search engine.

My cents.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 4, 2015, 7:32am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/5 "2015-09-04T07:32:21Z")

</div>

@dadoonet, good points and confused how Parent/Child feature is related to join? More hints are appreciated.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 4, 2015, 7:52am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/6 "2015-09-04T07:52:48Z")

</div>

Well, Parent/Child does join in memory between one parent and the children documents attached to it.

So you can run queries like:

```auto
{
    "has_parent" : {
        "parent_type" : "blog",
        "query" : {
            "term" : {
                "tag" : "something"
            }
        }
    }
}

```

Read this excellent chapter for details: [https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html)

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 4, 2015, 7:27pm UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/7 "2015-09-04T19:27:10Z")

</div>

@dadoonet, thanks but I still feel lost how Parent/Child join related to normal database join concept, suppose I have a customer table and another order table, and I want to join by customer ID to find customer who has ordered something. How to implement that with Parent/Child join? Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 4, 2015, 7:54pm UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/8 "2015-09-04T19:54:38Z")

</div>

Create a customer doc.  
Then create order documents with the customer as the parent doc.

But as I said, don't apply relational model in document oriented system. Re-think your model based on your use case.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 5, 2015, 2:18am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/9 "2015-09-05T02:18:52Z")

</div>

@dadoonet, I think with Parent/Chid model, we can do exact what relational model support? Are there any join cases you think Parent/Child model cannot cover, comparing to relational inner join case? Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 5, 2015, 6:36am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/10 "2015-09-05T06:36:15Z")

</div>

May be. But again, forget all you know about relational databases and start learning document oriented data models. They are really different beasts.

For example, I'd most likely copy in "order" document all information about the "customer". Instead of trying to link the entities.

---

<div class="post-metadata">

**Author:** ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)\
**Post date:** [September 18, 2015, 1:29am UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/11 "2015-09-18T01:29:49Z")

</div>

@dadoonet, thanks a lot and these days, I have some new thinkings and let us discuss there =\> [Parent/child document join](https://discuss.elastic.co/t/parent-child-document-join/29530), looking forward to your help and guidance. 😄

regards,  
Lin

---

<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 5, 2017, 11:49pm UTC](https://discuss.elastic.co/t/join-on-elasticsearch/28643/12 "2017-07-05T23:49:32Z")

</div>


