# Query: Parents with at least x children of type y

**URL:** <https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080>\
**Category:** Elasticsearch\
**Created:** [January 2, 2014, 2:22pm UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080 "2014-01-02T14:22:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Stautner](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Alexander\_Stautner](https://discuss.elastic.co/u/Alexander_Stautner)\
**Post date:** [January 2, 2014, 2:22pm UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/1 "2014-01-02T14:22:32Z")

</div>

Hello,  
after some research without any results I have a question about  
parent/child relations.

The case:

I have a parent of type "parent\_type" which has children of different types  
e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".

My Question is:

Is there any possibility to get only the parents which have at least x  
children of type "child\_type\_2" with an specific value in an attribute.

e.g

parent\_type: family  
child\_type\_1: girl attribute:name  
child\_type\_2: boy attribute:name  
child\_type\_3: cat attribute:name

And i want to have all families which have at least three girls with name  
"Jane".

Thank you for your help,  
Alex

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/15c715f7-4414-424d-ae04-54bb77b7ddcf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/15c715f7-4414-424d-ae04-54bb77b7ddcf%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Alexander\_Stautner](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Alexander\_Stautner](https://discuss.elastic.co/u/Alexander_Stautner)\
**Post date:** [January 7, 2014, 7:15am UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/2 "2014-01-07T07:15:42Z")

</div>

Sorry for bumping, but i need an answer, if it's posible to answer the  
question above with elasticsearch

Am Donnerstag, 2. Januar 2014 15:22:32 UTC+1 schrieb Alexander Stautner:

> Hello,  
> after some research without any results I have a question about  
> parent/child relations.
> 
> The case:
> 
> I have a parent of type "parent\_type" which has children of different  
> types e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".
> 
> My Question is:
> 
> Is there any possibility to get only the parents which have at least x  
> children of type "child\_type\_2" with an specific value in an attribute.
> 
> e.g
> 
> parent\_type: family  
> child\_type\_1: girl attribute:name  
> child\_type\_2: boy attribute:name  
> child\_type\_3: cat attribute:name
> 
> And i want to have all families which have at least three girls with name  
> "Jane".
> 
> Thank you for your help,  
> Alex

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 7, 2014, 9:39am UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/3 "2014-01-07T09:39:41Z")

</div>

I would probably add a num\_of\_children field in parent doc and update it when a new child is added or removed.

But I guess it depends on your actual use case!

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 7 janv. 2014 à 08:15, Alexander Stautner [alexander.stautner@gmail.com](mailto:alexander.stautner@gmail.com) a écrit :

> Sorry for bumping, but i need an answer, if it's posible to answer the question above with elasticsearch
> 
> Am Donnerstag, 2. Januar 2014 15:22:32 UTC+1 schrieb Alexander Stautner:
> 
> > Hello,  
> > after some research without any results I have a question about parent/child relations.
> > 
> > The case:
> > 
> > I have a parent of type "parent\_type" which has children of different types e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".
> > 
> > My Question is:
> > 
> > Is there any possibility to get only the parents which have at least x children of type "child\_type\_2" with an specific value in an attribute.
> > 
> > e.g
> > 
> > parent\_type: family  
> > child\_type\_1: girl attribute:name  
> > child\_type\_2: boy attribute:name  
> > child\_type\_3: cat attribute:name
> > 
> > And i want to have all families which have at least three girls with name "Jane".
> > 
> > Thank you for your help,  
> > Alex
> 
> --  
> 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).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/85E9435C-F658-45A3-9F84-C9CE0EA697DB%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/85E9435C-F658-45A3-9F84-C9CE0EA697DB%40pilato.fr).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Alexander\_Stautner](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Alexander\_Stautner](https://discuss.elastic.co/u/Alexander_Stautner)\
**Post date:** [January 7, 2014, 10:21am UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/4 "2014-01-07T10:21:38Z")

</div>

Changing the parent doc should be prevented, because there may be new  
child\_types added or old child\_types may be removed and we want the  
child\_types be independed from the parent\_type.

So it seems, that there is at the moment no way for doing suchs queries  
with elasticsearch?

Thanks for helping.

Am Dienstag, 7. Januar 2014 10:39:41 UTC+1 schrieb David Pilato:

> I would probably add a num\_of\_children field in parent doc and update it  
> when a new child is added or removed.
> 
> But I guess it depends on your actual use case!
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 7 janv. 2014 à 08:15, Alexander Stautner \<[alexander...@gmail.com](mailto:alexander...@gmail.com)\<javascript:\>\>  
> a écrit :
> 
> Sorry for bumping, but i need an answer, if it's posible to answer the  
> question above with elasticsearch
> 
> Am Donnerstag, 2. Januar 2014 15:22:32 UTC+1 schrieb Alexander Stautner:
> 
> > Hello,  
> > after some research without any results I have a question about  
> > parent/child relations.
> > 
> > The case:
> > 
> > I have a parent of type "parent\_type" which has children of different  
> > types e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".
> > 
> > My Question is:
> > 
> > Is there any possibility to get only the parents which have at least x  
> > children of type "child\_type\_2" with an specific value in an attribute.
> > 
> > e.g
> > 
> > parent\_type: family  
> > child\_type\_1: girl attribute:name  
> > child\_type\_2: boy attribute:name  
> > child\_type\_3: cat attribute:name
> > 
> > And i want to have all families which have at least three girls with name  
> > "Jane".
> > 
> > Thank you for your help,  
> > Alex
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [January 19, 2014, 10:29pm UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/5 "2014-01-19T22:29:03Z")

</div>

Sorry for responding so late... something like this is possible:

> <https://gist.github.com/martijnvg/8511859>

By using `score_type=max` and wrapping the inner query in a `constant_score query`, the score represents the number of child docs that match with a  
parent. Then by using the `min_score` feature you can filter out parent  
hits that have less then X child doc matches.

On 7 January 2014 11:21, Alexander Stautner [alexander.stautner@gmail.com](mailto:alexander.stautner@gmail.com)wrote:

> Changing the parent doc should be prevented, because there may be new  
> child\_types added or old child\_types may be removed and we want the  
> child\_types be independed from the parent\_type.
> 
> So it seems, that there is at the moment no way for doing suchs queries  
> with elasticsearch?
> 
> Thanks for helping.
> 
> Am Dienstag, 7. Januar 2014 10:39:41 UTC+1 schrieb David Pilato:
> 
> > I would probably add a num\_of\_children field in parent doc and update it  
> > when a new child is added or removed.
> > 
> > But I guess it depends on your actual use case!
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 7 janv. 2014 à 08:15, Alexander Stautner [alexander...@gmail.com](mailto:alexander...@gmail.com) a  
> > écrit :
> > 
> > Sorry for bumping, but i need an answer, if it's posible to answer the  
> > question above with elasticsearch
> > 
> > Am Donnerstag, 2. Januar 2014 15:22:32 UTC+1 schrieb Alexander Stautner:
> > 
> > > Hello,  
> > > after some research without any results I have a question about  
> > > parent/child relations.
> > > 
> > > The case:
> > > 
> > > I have a parent of type "parent\_type" which has children of different  
> > > types e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".
> > > 
> > > My Question is:
> > > 
> > > Is there any possibility to get only the parents which have at least x  
> > > children of type "child\_type\_2" with an specific value in an attribute.
> > > 
> > > e.g
> > > 
> > > parent\_type: family  
> > > child\_type\_1: girl attribute:name  
> > > child\_type\_2: boy attribute:name  
> > > child\_type\_3: cat attribute:name
> > > 
> > > And i want to have all families which have at least three girls with  
> > > name "Jane".
> > > 
> > > Thank you for your help,  
> > > Alex
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > 
> > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%  
> > [40googlegroups.com](http://40googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com)  
> > .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tye%3DheeEHfWhFjvty%3DMeRfy0oMxd9V0nSQvF%2BbUE-JiDQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tye%3DheeEHfWhFjvty%3DMeRfy0oMxd9V0nSQvF%2BbUE-JiDQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 20, 2014, 4:21am UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/6 "2014-01-20T04:21:34Z")

</div>

Awesome!

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 19 janv. 2014 à 23:29, Martijn v Groningen [martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com) a écrit :

Sorry for responding so late... something like this is possible: [example.sh · GitHub](https://gist.github.com/martijnvg/8511859)

By using `score_type=max` and wrapping the inner query in a `constant_score query`, the score represents the number of child docs that match with a parent. Then by using the `min_score` feature you can filter out parent hits that have less then X child doc matches.

> On 7 January 2014 11:21, Alexander Stautner [alexander.stautner@gmail.com](mailto:alexander.stautner@gmail.com) wrote:  
> Changing the parent doc should be prevented, because there may be new child\_types added or old child\_types may be removed and we want the child\_types be independed from the parent\_type.
> 
> So it seems, that there is at the moment no way for doing suchs queries with elasticsearch?
> 
> Thanks for helping.
> 
> Am Dienstag, 7. Januar 2014 10:39:41 UTC+1 schrieb David Pilato:
> 
> > I would probably add a num\_of\_children field in parent doc and update it when a new child is added or removed.
> > 
> > But I guess it depends on your actual use case!
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > > Le 7 janv. 2014 à 08:15, Alexander Stautner [alexander...@gmail.com](mailto:alexander...@gmail.com) a écrit :
> > > 
> > > Sorry for bumping, but i need an answer, if it's posible to answer the question above with elasticsearch
> > > 
> > > Am Donnerstag, 2. Januar 2014 15:22:32 UTC+1 schrieb Alexander Stautner:
> > > 
> > > > Hello,  
> > > > after some research without any results I have a question about parent/child relations.
> > > > 
> > > > The case:
> > > > 
> > > > I have a parent of type "parent\_type" which has children of different types e.g. "child\_type\_1", "child\_type\_2", "child\_type\_3".
> > > > 
> > > > My Question is:
> > > > 
> > > > Is there any possibility to get only the parents which have at least x children of type "child\_type\_2" with an specific value in an attribute.
> > > > 
> > > > e.g
> > > > 
> > > > parent\_type: family  
> > > > child\_type\_1: girl attribute:name  
> > > > child\_type\_2: boy attribute:name  
> > > > child\_type\_3: cat attribute:name
> > > > 
> > > > And i want to have all families which have at least three girls with name "Jane".
> > > > 
> > > > Thank you for your help,  
> > > > Alex
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6622897e-3e72-4db4-b4a0-4d8555c077e8%40googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ca23c8a-631b-4d3c-879e-69bb389eef06%40googlegroups.com).
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

## Martijn van Groningen

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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tye%3DheeEHfWhFjvty%3DMeRfy0oMxd9V0nSQvF%2BbUE-JiDQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CA%2BA76Tye%3DheeEHfWhFjvty%3DMeRfy0oMxd9V0nSQvF%2BbUE-JiDQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/A810B812-FD31-48B0-905A-AF67B9711969%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/A810B812-FD31-48B0-905A-AF67B9711969%40pilato.fr).  
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, 1:55am UTC](https://discuss.elastic.co/t/query-parents-with-at-least-x-children-of-type-y/15080/7 "2017-07-06T01:55:58Z")

</div>


