# Return matched nested object instead of root parent document

**URL:** <https://discuss.elastic.co/t/return-matched-nested-object-instead-of-root-parent-document/29464>\
**Category:** Elasticsearch\
**Created:** [September 17, 2015, 8:40am UTC](https://discuss.elastic.co/t/return-matched-nested-object-instead-of-root-parent-document/29464 "2015-09-17T08:40:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shekhar](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@shekhar](https://discuss.elastic.co/u/shekhar)\
**Post date:** [September 17, 2015, 8:40am UTC](https://discuss.elastic.co/t/return-matched-nested-object-instead-of-root-parent-document/29464/1 "2015-09-17T08:40:18Z")

</div>

I want nested query to return only matched nested object for nested field instead of root parent document, I s it possible ?

---

<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:** [September 18, 2015, 8:17am UTC](https://discuss.elastic.co/t/return-matched-nested-object-instead-of-root-parent-document/29464/2 "2015-09-18T08:17:07Z")

</div>

This can be achieved via aggregations. You can use the nested with the  
top\_hits aggregation to only return the matched nested inner objects.  
Something like this:

{  
"size": 0,  
"aggs": {  
"my-query": {  
"filter": {  
"query": {  
...  
}  
},  
"aggs": {  
"to-nested": {  
"nested": {  
"path": "level1"  
},  
"aggs": {  
"nested-docs": {  
"top\_hits": {  
"size": 10  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![jaikit](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jaikit](https://discuss.elastic.co/u/jaikit)\
**Post date:** [September 20, 2015, 11:47pm UTC](https://discuss.elastic.co/t/return-matched-nested-object-instead-of-root-parent-document/29464/3 "2015-09-20T23:47:12Z")

</div>

You need to add filter in your aggregation which will be equal to your main query. Here is the post with similar use case and solution to it.

> [@Aggregation over nested documents](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810):
>
> Folks, First all great shout out to Elastic community and team for designing and building clean product. Great job! I am running a query to generate aggregations on nested documents. I have created the gist of all the curl calls (index creation, mapping and query. ) As you see in the mapping and my query, I am trying to generate aggregations on item name. In this case aggregations are generated over query results. That said I see aggregation of other nested documents as well in final result. G…

Let me know if this works for you.

---

<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/return-matched-nested-object-instead-of-root-parent-document/29464/4 "2017-07-05T23:49:10Z")

</div>


