# Counting parents in a parent child relationship

**URL:** https://discuss.elastic.co/t/counting-parents-in-a-parent-child-relationship/9046
**Category:** Elasticsearch
**Created:** [September 17, 2012, 8:58pm UTC](https://discuss.elastic.co/t/counting-parents-in-a-parent-child-relationship/9046 "2012-09-17T20:58:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Todd\_Gruben](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@Todd\_Gruben](https://discuss.elastic.co/u/Todd_Gruben)
#### Post date: [September 17, 2012, 8:58pm UTC](https://discuss.elastic.co/t/counting-parents-in-a-parent-child-relationship/9046/1 "2012-09-17T20:58:41Z")

</div>

I am attempting to count profiles based on the has\_child filter but I am  
not getting the desired results. If I execute the query against the search  
api, I get the correct list of documents, but If i execute against the  
count api, I get 0.

My end goal is to count profiles which have certain attributes(like male)  
and has a child document that matches a certain tag. That should be  
possible correct?

> <https://gist.github.com/tgruben/3739692>

Thanks for a really cool product

--

---

<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, 2012, 9:14am UTC](https://discuss.elastic.co/t/counting-parents-in-a-parent-child-relationship/9046/2 "2012-09-18T09:14:37Z")

</div>

Hi Todd,

Executing the has\_child query (and also top\_children query) on the  
count api has known problems.

The workaround for this problem is the use the search api with the  
search type 'count':

> **[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.

In general it is recommended to use the search api with search type  
count over the count api for counting hits.

Martijn

On 17 September 2012 22:58, Todd Gruben [tgruben@gmail.com](mailto:tgruben@gmail.com) wrote:

> I am attempting to count profiles based on the has\_child filter but I am not  
> getting the desired results. If I execute the query against the search api,  
> I get the correct list of documents, but If i execute against the count api,  
> I get 0.
> 
> My end goal is to count profiles which have certain attributes(like male)  
> and has a child document that matches a certain tag. That should be  
> possible correct?
> 
> [Issue with Parent Child counting · GitHub](https://gist.github.com/3739692)
> 
> Thanks for a really cool product
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<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:12am UTC](https://discuss.elastic.co/t/counting-parents-in-a-parent-child-relationship/9046/3 "2017-07-06T03:12:31Z")

</div>


