# Joining of documents in search results

**URL:** https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581
**Category:** Elasticsearch
**Created:** [January 31, 2016, 11:06am UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581 "2016-01-31T11:06:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sandeep\_reddy](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@sandeep\_reddy](https://discuss.elastic.co/u/sandeep_reddy)
#### Post date: [January 31, 2016, 11:06am UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581/1 "2016-01-31T11:06:44Z")

</div>

We have a requirement where we need to combine the documents in search results. Below example depicts it.

Assuming we have two documents say  
Teachers.json  
{  
"id": "Teacher1",  
"contextType": "Teacher",  
"Subject" : "English"  
"foreign\_key": [  
"Student1"  
]  
}

Student1.json  
{  
"id": "Student1",  
"contextType": "STUDENT"  
}  
So, the requirement is that we need to display the combined output in a single document, as below

"response": {  
"numFound": 1,  
"start": 0,  
"docs": [  
{  
"id": "Teacher1",  
"contextType": "TEACHER",  
"Subject" : "English",  
"Student":[{  
"id": "Student1",  
"contextType": "STUDENT"  
}],  
}  
]  
}

I tried using parent child relationships in elastic search, but didnt had any luck.  
Please let me know if this is possible in elastic search

P.S : I am new bie to elastic search

---

<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: [February 1, 2016, 3:22am UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581/2 "2016-02-01T03:22:43Z")

</div>

It's not possible.

---

<div class="post-metadata">

### Author: ![Geunmoon\_Oh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/geunmoon_oh/32/7507_2.png) [@Geunmoon\_Oh](https://discuss.elastic.co/u/Geunmoon_Oh)
#### Post date: [February 1, 2016, 5:45am UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581/3 "2016-02-01T05:45:01Z")

</div>

it's not possible.  
but, how about using nested object like below?

{  
"id": "Teacher1",  
"contextType": "Teacher",  
"Subject" : "English"  
"student\_nested": [  
{  
"id": "Student1",  
"contextType": "STUDENT"  
}  
]  
}

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [February 2, 2016, 8:29am UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581/4 "2016-02-02T08:29:58Z")

</div>

It isn't possible to join arbitrary document in the response.  
However with either nested or parent/child it is possible to inline the children docs into the parent hit with inner hits: [https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-inner-hits.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-inner-hits.html)

---

<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:19pm UTC](https://discuss.elastic.co/t/joining-of-documents-in-search-results/40581/5 "2017-07-05T23:19:45Z")

</div>


