# Counting the number of children when returning a parent

**URL:** <https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861>\
**Category:** Elasticsearch\
**Created:** [November 28, 2012, 12:48pm UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861 "2012-11-28T12:48:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidconde](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@davidconde](https://discuss.elastic.co/u/davidconde)\
**Post date:** [November 28, 2012, 12:48pm UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/1 "2012-11-28T12:48:20Z")

</div>

I have setup a parent child relationship and I would like to be able to  
return a list of parents but also include the count of children per parent.  
Is there an easy way to do this at the moment?

Thanks,  
Dave

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 28, 2012, 6:06pm UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/2 "2012-11-28T18:06:38Z")

</div>

Hello Dave,

It might help if you use a has\_parent query[0] and do a terms facet[1] on  
the \_parent field.

I've gisted an example here:

> <https://gist.github.com/radu-gheorghe/4162883>

If that works for you, you might get better performance if you replace the  
query with a facet filter[2].

The downside of this approach is that the query itself will return the  
children, not the parents. That said, you can take the parent IDs from the  
facet results and get the contents using the multi get API[3].

[0]

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

[1]

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

[2]

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

[3] [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/api/multi-get.html)

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Nov 28, 2012 at 2:48 PM, davidconde [david@donedeal.ie](mailto:david@donedeal.ie) wrote:

> I have setup a parent child relationship and I would like to be able to  
> return a list of parents but also include the count of children per parent.  
> Is there an easy way to do this at the moment?
> 
> Thanks,  
> Dave
> 
> --

--

---

<div class="post-metadata">

**Author:** ![davidconde](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@davidconde](https://discuss.elastic.co/u/davidconde)\
**Post date:** [December 3, 2012, 11:47am UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/3 "2012-12-03T11:47:49Z")

</div>

Thanks that seems to have done the job 🙂

On Wednesday, 28 November 2012 18:06:38 UTC, Radu Gheorghe wrote:

> Hello Dave,
> 
> It might help if you use a has\_parent query[0] and do a terms facet[1] on  
> the \_parent field.
> 
> I've gisted an example here:  
> [Query parents and count children · GitHub](https://gist.github.com/4162883)
> 
> If that works for you, you might get better performance if you replace the  
> query with a facet filter[2].
> 
> The downside of this approach is that the query itself will return the  
> children, not the parents. That said, you can take the parent IDs from the  
> facet results and get the contents using the multi get API[3].
> 
> [0]  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter.html)  
> [1]  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)  
> [2]  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)  
> [3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/multi-get.html)
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Wed, Nov 28, 2012 at 2:48 PM, davidconde \<da...@donedeal.ie\<javascript:\>
> 
> > wrote:
> 
> > I have setup a parent child relationship and I would like to be able to  
> > return a list of parents but also include the count of children per parent.  
> > Is there an easy way to do this at the moment?
> > 
> > Thanks,  
> > Dave
> > 
> > --

--

---

<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:** [December 3, 2012, 1:22pm UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/4 "2012-12-03T13:22:32Z")

</div>

Another trick that might be interesting to test is to use the  
`top_children` query and then set `score` to `sum` and wrap the child  
query in a `constant_score` query. The score of the parent would then  
tell you how many child documents have matched. This trick obviously  
only is useable if you don't sort by relevance, but by something else  
like a field or you don't care about the ordering of the hits. The  
benefit of this approach is that only one search request needs to be  
executed to get the parent documents with the child count.

Martijn

On 3 December 2012 22:47, davidconde [david@donedeal.ie](mailto:david@donedeal.ie) wrote:

> Thanks that seems to have done the job 🙂
> 
> On Wednesday, 28 November 2012 18:06:38 UTC, Radu Gheorghe wrote:
> 
> > Hello Dave,
> > 
> > It might help if you use a has\_parent query[0] and do a terms facet[1] on  
> > the \_parent field.
> > 
> > I've gisted an example here:  
> > [Query parents and count children · GitHub](https://gist.github.com/4162883)
> > 
> > If that works for you, you might get better performance if you replace the  
> > query with a facet filter[2].
> > 
> > The downside of this approach is that the query itself will return the  
> > children, not the parents. That said, you can take the parent IDs from the  
> > facet results and get the contents using the multi get API[3].
> > 
> > [0]  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter.html)  
> > [1]  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)  
> > [2]  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)  
> > [3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/multi-get.html)
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Wed, Nov 28, 2012 at 2:48 PM, davidconde [da...@donedeal.ie](mailto:da...@donedeal.ie) wrote:
> > 
> > > I have setup a parent child relationship and I would like to be able to  
> > > return a list of parents but also include the count of children per parent.  
> > > Is there an easy way to do this at the moment?
> > > 
> > > Thanks,  
> > > Dave
> > > 
> > > --
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Andrii\_Gakhov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrii_gakhov/32/771_2.png) [@Andrii\_Gakhov](https://discuss.elastic.co/u/Andrii_Gakhov)\
**Post date:** [April 13, 2015, 3:45pm UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/5 "2015-04-13T15:45:29Z")

</div>

Extending Martijn's approch to the current Elasticsearch 1.5, we can  
execute _inner\_hits_ together with _has\_child_ query, and get counts as the  
score for parent documents:

{  
"query": {  
"has\_child": {  
"type": "comment",  
"score\_mode": "sum",  
"query": {  
"match\_all": {}  
},  
"inner\_hits": {}  
}  
}  
}

Monday, 3 december 2012 г., 14:22:32 UTC+1 user Martijn v Groningen wrote:

> Another trick that might be interesting to test is to use the  
> `top_children` query and then set `score` to `sum` and wrap the child  
> query in a `constant_score` query. The score of the parent would then  
> tell you how many child documents have matched. This trick obviously  
> only is useable if you don't sort by relevance, but by something else  
> like a field or you don't care about the ordering of the hits. The  
> benefit of this approach is that only one search request needs to be  
> executed to get the parent documents with the child count.
> 
> Martijn
> 
> On 3 December 2012 22:47, davidconde \<da...@donedeal.ie \<javascript:\>\>  
> wrote:
> 
> > Thanks that seems to have done the job 🙂
> > 
> > On Wednesday, 28 November 2012 18:06:38 UTC, Radu Gheorghe wrote:
> > 
> > > Hello Dave,
> > > 
> > > It might help if you use a has\_parent query[0] and do a terms facet[1]  
> > > on  
> > > the \_parent field.
> > > 
> > > I've gisted an example here:  
> > > [Query parents and count children · GitHub](https://gist.github.com/4162883)
> > > 
> > > If that works for you, you might get better performance if you replace  
> > > the  
> > > query with a facet filter[2].
> > > 
> > > The downside of this approach is that the query itself will return the  
> > > children, not the parents. That said, you can take the parent IDs from  
> > > the  
> > > facet results and get the contents using the multi get API[3].
> > > 
> > > [0]
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter.html)
> 
> > > [1]
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html)
> 
> > > [2]
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> 
> > > [3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/multi-get.html)
> > > 
> > > ## Best regards, Radu
> > > 
> > > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > > 
> > > On Wed, Nov 28, 2012 at 2:48 PM, davidconde [da...@donedeal.ie](mailto:da...@donedeal.ie) wrote:
> > > 
> > > > I have setup a parent child relationship and I would like to be able  
> > > > to  
> > > > return a list of parents but also include the count of children per  
> > > > parent.  
> > > > Is there an easy way to do this at the moment?
> > > > 
> > > > Thanks,  
> > > > Dave
> > > > 
> > > > --
> > 
> > --
> 
> --  
> 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/2899e6be-64d9-4dff-9e55-e4c5dc4d3f1f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2899e6be-64d9-4dff-9e55-e4c5dc4d3f1f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:20am UTC](https://discuss.elastic.co/t/counting-the-number-of-children-when-returning-a-parent/9861/6 "2017-07-06T00:20:03Z")

</div>


