# Parent/Child: Find children based on parent properties

**URL:** <https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257>\
**Category:** Elasticsearch\
**Created:** [June 29, 2012, 5:03am UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257 "2012-06-29T05:03:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [June 29, 2012, 5:03am UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/1 "2012-06-29T05:03:02Z")

</div>

Hi guys. With a parent/child mapping, is there a way to search for children  
whose parents have certain property values?

For example, in the curl test in this gist, messages belong to users. How  
can I find all messages belonging to a user whose firstname is "Nick"?

> <https://gist.github.com/nickhoffman/3015881>

Thanks,  
Nick

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [June 30, 2012, 7:22pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/2 "2012-06-30T19:22:31Z")

</div>

The way i employ for such problems is as follows

1. Run the query on the parent and get all doc id's of the parent.
2. Take all the matching parent ID's as an array and match it on \_parent  
field (using terms query) over the child type.

Thanks  
Vineeth

On Fri, Jun 29, 2012 at 10:33 AM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hi guys. With a parent/child mapping, is there a way to search for  
> children whose parents have certain property values?
> 
> For example, in the curl test in this gist, messages belong to users. How  
> can I find all messages belonging to a user whose firstname is "Nick"?  
> [ElasticSearch: Parent/Child: Find children based on parent properties · GitHub](https://gist.github.com/3015881)
> 
> Thanks,  
> Nick

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [July 1, 2012, 2:06pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/3 "2012-07-01T14:06:43Z")

</div>

Hey Vineeth. Thanks for the suggestion. That works most of the time.  
However, it prevents you from being able to sort on the parent's fields.  
Also, I'm not sure how well it'd work if a very large number of parent  
documents match...say \>10,000.

Is there no way to find children based on parent properties?

Thanks,  
Nick

On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:

> The way i employ for such problems is as follows
> 
> 1. Run the query on the parent and get all doc id's of the parent.
> 2. Take all the matching parent ID's as an array and match it on \_parent  
> field (using terms query) over the child type.
> 
> Thanks  
> Vineeth

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [July 1, 2012, 3:13pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/4 "2012-07-01T15:13:57Z")

</div>

Hello Nick ,

I dont think you need to worry about the number of parent documents.  
But then sorting based on parents field is really gonna be a problem.  
My suggetion would be that if the parent field you wanna sort is small ,  
duplicate it to the children too.  
But again finding all parent ID and then going through the children is not  
a good idea.

Meanwhile , you can check following links. This problem is pretty actively  
discussed.

Thread -  
[http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html](http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html)  
Issue - [Add a has\_parent query · Issue #792 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/792)

Thanks  
Vineeth

On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hey Vineeth. Thanks for the suggestion. That works most of the time.  
> However, it prevents you from being able to sort on the parent's fields.  
> Also, I'm not sure how well it'd work if a very large number of parent  
> documents match...say \>10,000.
> 
> Is there no way to find children based on parent properties?
> 
> Thanks,  
> Nick
> 
> On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:
> 
> > The way i employ for such problems is as follows
> > 
> > 1. Run the query on the parent and get all doc id's of the parent.
> > 2. Take all the matching parent ID's as an array and match it on \_parent  
> > field (using terms query) over the child type.
> > 
> > Thanks  
> > Vineeth

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [July 4, 2012, 2:20pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/5 "2012-07-04T14:20:55Z")

</div>

Hi Vineeth. Thanks for the links. They're very helpful indeed.

So it wouldn't matter if 20,000 parent docs match, and I use those 20,000  
IDs in the query on the children? I figured the HTTP request for that query  
would be so large that it wouldn't be accepted, ES would take a long time  
to parse it, etc.

You said:  
"finding all parent ID and then going through the children is not a good  
idea"  
There isn't an alternative at the moment though, is there?

Thanks,  
Nick

On Sunday, 1 July 2012 11:13:57 UTC-4, Vineeth Mohan wrote:

> Hello Nick ,
> 
> I dont think you need to worry about the number of parent documents.  
> But then sorting based on parents field is really gonna be a problem.  
> My suggetion would be that if the parent field you wanna sort is small ,  
> duplicate it to the children too.  
> But again finding all parent ID and then going through the children is not  
> a good idea.
> 
> Meanwhile , you can check following links. This problem is pretty actively  
> discussed.
> 
> Thread -  
> [http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html](http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html)  
> Issue - [Add a has\_parent query · Issue #792 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/792)
> 
> Thanks  
> Vineeth
> 
> On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:
> 
> > Hey Vineeth. Thanks for the suggestion. That works most of the time.  
> > However, it prevents you from being able to sort on the parent's fields.  
> > Also, I'm not sure how well it'd work if a very large number of parent  
> > documents match...say \>10,000.
> > 
> > Is there no way to find children based on parent properties?
> > 
> > Thanks,  
> > Nick
> > 
> > On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:
> > 
> > > The way i employ for such problems is as follows
> > > 
> > > 1. Run the query on the parent and get all doc id's of the parent.
> > > 2. Take all the matching parent ID's as an array and match it on \_parent  
> > > field (using terms query) over the child type.
> > > 
> > > Thanks  
> > > Vineeth

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [July 4, 2012, 5:56pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/6 "2012-07-04T17:56:12Z")

</div>

Instances where 20,000 documents matches and going tru the children is not  
actually possible or a good idea.  
I am afraid there is not an alternative at the moment.

We also have a similar situation but then on our search result , each  
search result was the parent document and on request we would show matching  
children too.

Between if you want to have result based on your children and not parent ,  
what is the point in creating parent/child relation ?

Thanks  
Vineeth

On Wed, Jul 4, 2012 at 7:50 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hi Vineeth. Thanks for the links. They're very helpful indeed.
> 
> So it wouldn't matter if 20,000 parent docs match, and I use those 20,000  
> IDs in the query on the children? I figured the HTTP request for that query  
> would be so large that it wouldn't be accepted, ES would take a long time  
> to parse it, etc.
> 
> You said:  
> "finding all parent ID and then going through the children is not a good  
> idea"  
> There isn't an alternative at the moment though, is there?
> 
> Thanks,  
> Nick
> 
> On Sunday, 1 July 2012 11:13:57 UTC-4, Vineeth Mohan wrote:
> 
> > Hello Nick ,
> > 
> > I dont think you need to worry about the number of parent documents.  
> > But then sorting based on parents field is really gonna be a problem.  
> > My suggetion would be that if the parent field you wanna sort is small ,  
> > duplicate it to the children too.  
> > But again finding all parent ID and then going through the children is  
> > not a good idea.
> > 
> > Meanwhile , you can check following links. This problem is pretty  
> > actively discussed.
> > 
> > Thread - [http://elasticsearch-users](http://elasticsearch-users).**[115913.n3.nabble.com/Sorting-](http://115913.n3.nabble.com/Sorting-)**  
> > by-sum-of-matching-children-\*\*td3877730.html[http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html](http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html)  
> > Issue - [https://github.com/\*\*elasticsearch/elasticsearch/\*\*issues/792](https://github.com/ **elasticsearch/elasticsearch/** issues/792)[https://github.com/elasticsearch/elasticsearch/issues/792](https://github.com/elasticsearch/elasticsearch/issues/792)
> > 
> > Thanks  
> > Vineeth
> > 
> > On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:
> > 
> > > Hey Vineeth. Thanks for the suggestion. That works most of the time.  
> > > However, it prevents you from being able to sort on the parent's fields.  
> > > Also, I'm not sure how well it'd work if a very large number of parent  
> > > documents match...say \>10,000.
> > > 
> > > Is there no way to find children based on parent properties?
> > > 
> > > Thanks,  
> > > Nick
> > > 
> > > On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:
> > > 
> > > > The way i employ for such problems is as follows
> > > > 
> > > > 1. Run the query on the parent and get all doc id's of the parent.
> > > > 2. Take all the matching parent ID's as an array and match it on  
> > > > \_parent field (using terms query) over the child type.
> > > > 
> > > > Thanks  
> > > > Vineeth

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [July 4, 2012, 6:38pm UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/7 "2012-07-04T18:38:22Z")

</div>

It's not possible to go through the children of 20,000 parents, eh? Might  
you know what the upper limit is, or is it completely dependent upon the  
resources that're allocated to ES?

My use case involves products and sales, where Product is the parent, and  
Sale is the child. Each product can have zero or more copies for sale. I'm  
trying to query for all sales whose parent matches various criteria.

Since the ratio of products-to-sales is way beyond 1:1, it'd be very  
inefficient to store product fields in each sale.

Thanks for your continued advice, Vineeth. I really appreciate it.  
Nick

On Wednesday, 4 July 2012 13:56:12 UTC-4, Vineeth Mohan wrote:

> Instances where 20,000 documents matches and going tru the children is not  
> actually possible or a good idea.  
> I am afraid there is not an alternative at the moment.
> 
> We also have a similar situation but then on our search result , each  
> search result was the parent document and on request we would show matching  
> children too.
> 
> Between if you want to have result based on your children and not parent ,  
> what is the point in creating parent/child relation ?
> 
> Thanks  
> Vineeth

---

<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, 3:21am UTC](https://discuss.elastic.co/t/parent-child-find-children-based-on-parent-properties/8257/8 "2017-07-06T03:21:29Z")

</div>


