# How to get childs child in a single Query

**URL:** <https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018>\
**Category:** Elasticsearch\
**Created:** [May 18, 2013, 1:07am UTC](https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018 "2013-05-18T01:07:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hari/32/1582_2.png) [@hari](https://discuss.elastic.co/u/hari)\
**Post date:** [May 18, 2013, 1:07am UTC](https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018/1 "2013-05-18T01:07:47Z")

</div>

I'm not sure if this is something that is doable in a single Query  
Here is the gist

> <https://gist.github.com/hariinfo/5602824>

Essentially I have following relationship

Parent (catentry) - one to many child (attributes) - one to many child  
(attrvalues)

For a given parent entry I want to get all associated childs (attributes)  
and all child documents of parent attributes? is is doable in a single  
Query?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Hannes\_Korte](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hannes_korte/32/909_2.png) [@Hannes\_Korte](https://discuss.elastic.co/u/Hannes_Korte)\
**Post date:** [May 18, 2013, 9:56am UTC](https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018/2 "2013-05-18T09:56:27Z")

</div>

On 18.05.2013 03:07, Hariharan Vadivelu wrote:

> For a given parent entry I want to get all associated childs (attributes)  
> and all child documents of parent attributes? is is doable in a single  
> Query?

Hi Hariharan,

as far as I know, it's not. I think you'll need two round trips to get  
parents and children. And for their children again one more.

If you only need the child documents for a limited number of parents,  
you can use a multi search request to gather the corresponding children:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

This is how I solved a simpler problem with just one additional level  
(parent/child), where I needed the child docs only for the top-n parent  
docs to be displayed. First search on parents, then take the top-n  
parents and build a multi search over the child docs with a term query  
on the "\_parent" field for each parent doc. The multi search response  
contains the list of child docs in the same order, so it's easy to map  
them back to the list of parent docs.

In my scenario it's fast enough, though it takes two round trips instead  
of just one. Also, if you need to get the child docs of a large list of  
parent docs, this is probably not the way to go, I guess. Maybe somebody  
comes up with a more elegant solution for that.

Best regards,  
Hannes

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![hari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hari/32/1582_2.png) [@hari](https://discuss.elastic.co/u/hari)\
**Post date:** [May 18, 2013, 4:30pm UTC](https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018/3 "2013-05-18T16:30:34Z")

</div>

Actually after I included routing to both child and child's child and run following Query I get everything associated to top level parent, is this an option to get all the sub documents associated to a top level parent?

curl -XPOST '[http://localhost:9200/products/\_search?pretty=true](http://localhost:9200/products/_search?pretty=true)' -d '{  
"query": {  
"filtered": {  
"query": {  
"term": {"catentry\_id" :"EI01Y" }  
}  
}  
}  
}'

On Saturday, May 18, 2013 4:56:33 AM UTC-5, Hannes Korte wrote:

> On 18.05.2013 03:07, Hariharan Vadivelu wrote:
> 
> > For a given parent entry I want to get all associated childs  
> > (attributes)  
> > and all child documents of parent attributes? is is doable in a single  
> > Query?
> 
> Hi Hariharan,
> 
> as far as I know, it's not. I think you'll need two round trips to get  
> parents and children. And for their children again one more.
> 
> If you only need the child documents for a limited number of parents,  
> you can use a multi search request to gather the corresponding children:  
> [http://www.elasticsearch.org/guide/reference/api/multi-search/](http://www.elasticsearch.org/guide/reference/api/multi-search/)
> 
> This is how I solved a simpler problem with just one additional level  
> (parent/child), where I needed the child docs only for the top-n parent  
> docs to be displayed. First search on parents, then take the top-n  
> parents and build a multi search over the child docs with a term query  
> on the "\_parent" field for each parent doc. The multi search response  
> contains the list of child docs in the same order, so it's easy to map  
> them back to the list of parent docs.
> 
> In my scenario it's fast enough, though it takes two round trips instead  
> of just one. Also, if you need to get the child docs of a large list of  
> parent docs, this is probably not the way to go, I guess. Maybe somebody  
> comes up with a more elegant solution for that.
> 
> Best regards,  
> Hannes

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:35am UTC](https://discuss.elastic.co/t/how-to-get-childs-child-in-a-single-query/12018/4 "2017-07-06T02:35:52Z")

</div>


