# Retrieve both parent and child documents

**URL:** https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863
**Category:** Elasticsearch
**Created:** [March 16, 2017, 1:06pm UTC](https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863 "2017-03-16T13:06:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Fang\_Yan](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@Fang\_Yan](https://discuss.elastic.co/u/Fang_Yan)
#### Post date: [March 16, 2017, 1:06pm UTC](https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863/1 "2017-03-16T13:06:20Z")

</div>

I have a data model with parent-child relationship.

Parent type subject mapping:  
{  
"properties": {  
"MRN": {  
"type": "keyword"  
},  
"display\_name": {  
"type": "keyword"  
},  
"protocols": {  
"type": "keyword"  
},  
"gender": {  
"type": "keyword"  
},  
"dob": {  
"type": "date"  
}  
....  
}

Child type lab mapping:  
{  
"\_parent": {  
"type": "subject",  
"eager\_global\_ordinals": false  
},  
"properties": {  
"GUID": {  
"type": "keyword"  
},  
"blinded": {  
"type": "boolean"  
},  
"collect\_time": {  
"type": "date"  
},  
.....  
}

Each lab document has only 1 parent, while a subject can have many lab documents.

I want to retrieve both parent and child document so the subject's gender, dob etc can be returned as search result.

Sample query:  
{"bool": {  
"must": [  
{  
"has\_parent": {  
"parent\_type": "subject",  
"inner\_hits": {  
"\_source": {  
"includes": ["MRN", "display\_name", "gender", "race", "ethnic\_group", "dob"]  
}  
},  
"query": {  
"bool": {  
"must": [  
{  
"terms": {  
"protocols": ["18785-BTRIS-TEST-01"]  
}  
}  
]  
}  
}  
}  
},  
{  
"term": {  
"\_type": "lab"  
}  
},  
{  
"query\_string": {  
"default\_field": "[observation.name](http://observation.name)",  
"query": "glucose"  
}  
}  
]  
}}

This query gives the expected results. However, it's quite slow. (Takes about 5 seconds on my test environment for size 1000). If I remove the "inner\_hits" part, the same query run much faster (100~ milliseconds).

My question: how to retrieve both parent and child documents in a more efficient manner?

---

<div class="post-metadata">

### Author: ![Matthew\_Isett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_isett/32/57770_2.png) [@Matthew\_Isett](https://discuss.elastic.co/u/Matthew_Isett)
#### Post date: [March 16, 2017, 2:53pm UTC](https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863/2 "2017-03-16T14:53:20Z")

</div>

The most efficient retrieval is de-normalize the data needed into a single object. Which you built by placing the needed ["MRN", "display\_name", "gender", "race", "ethnic\_group", "dob"] in the child document.

If you upgrade to 5.2 - you can use the Search Profiler in Dev Tools to look at the time breakouts.

---

<div class="post-metadata">

### Author: ![Matthew\_Isett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_isett/32/57770_2.png) [@Matthew\_Isett](https://discuss.elastic.co/u/Matthew_Isett)
#### Post date: [March 28, 2017, 12:59pm UTC](https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863/4 "2017-03-28T12:59:39Z")

</div>

Can you test out this reverse- nested agg. See if it provides a performance increase over inner hits

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.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: [April 25, 2017, 1:00pm UTC](https://discuss.elastic.co/t/retrieve-both-parent-and-child-documents/78863/5 "2017-04-25T13:00:05Z")

</div>

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