# Optimized inner hits

**URL:** <https://discuss.elastic.co/t/optimized-inner-hits/79763>\
**Category:** Elasticsearch\
**Created:** [March 23, 2017, 3:03pm UTC](https://discuss.elastic.co/t/optimized-inner-hits/79763 "2017-03-23T15:03:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![niaz](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@niaz](https://discuss.elastic.co/u/niaz)\
**Post date:** [March 23, 2017, 3:03pm UTC](https://discuss.elastic.co/t/optimized-inner-hits/79763/1 "2017-03-23T15:03:57Z")

</div>

Hello,

I would like to know, if there is a possibility to retrieve the inner hits in an optimized way?

Consider following example:

PUT my\_index  
{  
"mappings": {  
"my\_parent": {},  
"my\_child": {  
"\_parent": {  
"type": "my\_parent"  
}  
}  
}  
}

PUT my\_index/my\_parent/1  
{  
"text": "This is a parent document"  
}

PUT my\_index/my\_child/2?parent=1  
{  
"text": "This is a child document"  
}

PUT my\_index/my\_child/3?parent=1&refresh=true  
{  
"text": "This is another child document"  
}

PUT my\_index/my\_parent/11  
{  
"text": "This is a parent 11 document"  
}

PUT my\_index/my\_child/12?parent=11  
{  
"text": "This is a child 12 document"  
}

PUT my\_index/my\_child/13?parent=11&refresh=true  
{  
"text": "This is another child 13 document"  
}

When I search for inner hits using following search, it will include the "parent" for each of its child. For smaller results this is not a problem, consider when we have several child documents and then for each child document the same parent document is copied in the results. Do we have any possibility to have a separate list of parents and the inner hits contain only its corresponding parent ID? This way much traffic can be saved.

POST my\_index/my\_child/\_search  
{  
"query": {  
"has\_parent": {  
"parent\_type": "my\_parent",  
"query": {  
"match": {  
"text": "parent"  
}  
},  
"inner\_hits" : {  
"name" : "abc"  
}  
}  
}  
}

Thanks,  
Niaz

---

<div class="post-metadata">

**Author:** ![niaz](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@niaz](https://discuss.elastic.co/u/niaz)\
**Post date:** [March 23, 2017, 3:12pm UTC](https://discuss.elastic.co/t/optimized-inner-hits/79763/2 "2017-03-23T15:12:41Z")

</div>

Please also mention, if there is another option to retrieve the parents of matched children efficiently? I mean, if somehow I can use the results of the first query to make the second query in same http call? Thanks.

---

<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:** [March 28, 2017, 7:01am UTC](https://discuss.elastic.co/t/optimized-inner-hits/79763/3 "2017-03-28T07:01:31Z")

</div>

No, inner hits will not do that. It will include the matching parent doc  
per child hit and doesn't take into account that child documents may have  
the same parent doc.

If you like to optimize for that then maybe you can fetch the parent  
document yourself from the client side via a subsequent multi search  
request? So you will then not be using inner hits and when your search  
request returns you then execute a multi search for all unique \_parent ids  
in your search result.

---

<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, 7:01am UTC](https://discuss.elastic.co/t/optimized-inner-hits/79763/4 "2017-04-25T07:01:32Z")

</div>

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