# Best way to count Child object matches?

**URL:** <https://discuss.elastic.co/t/best-way-to-count-child-object-matches/9706>\
**Category:** Elasticsearch\
**Created:** [November 13, 2012, 11:29pm UTC](https://discuss.elastic.co/t/best-way-to-count-child-object-matches/9706 "2012-11-13T23:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [November 13, 2012, 11:29pm UTC](https://discuss.elastic.co/t/best-way-to-count-child-object-matches/9706/1 "2012-11-13T23:29:47Z")

</div>

If I have a parent and child relationship I can write a query to find  
all parents that match criteria within the parent and other criteria  
within the child.  
I can use a has-child query to see if there are any appropriately  
matching parents that have matching children The desired scoring is  
based on relevancy. All is well as long as none of the relevancy  
scoring is based any fields in the child which in my case is true. Such  
a query that also includes a has\_child would work great until the  
application wants to see the count of children. The query hits doesn't  
give the count of children matched.

Count of matching children

Approach 1: has\_child, then facet  
One way that comes to mind to find the count of matching children per  
parent is to do a termS facet on the scope of the matching children  
where the term is the "\_parentId" and the stats are for any numeric  
field in the child. Thus the count statistic from the facet is the  
count of matching children for a particular parent.

The advantage to approach 1, is that the search hits for the query are  
exactly the results, so "From" and "size" and total hits all are just  
what is needed.  
The disadvantage to this approach is that the facet list returned might  
be huge (the facet would include all matching document IDs and some  
stats about them). In my case that might be 10ks of facet values. That  
doesn't sound good at all.

Approach 2: has\_parent  
Invert the whole question. Start looking for the children that match,  
but also ask about has\_parent that match. That sounds good (or at least  
it did to me) until I realized that my ranking is based on a score of  
fields in the parent, so I couldn't actually rank my hits as I wanted.  
It also suffers from being a list of all children and what is wanted is  
a list of parents, so requires post processing everything after the fact.

Approach 3: 2nd Query  
After getting the result set of parents, go back to the index just to  
retrieve all matching children, but only for the page of parents that we  
are trying to return to the application, it certainly ought to be a  
quick query being limited by a list of IDs. Then we can report the  
count of how many children for each parent ID. This does have the  
advantage that if the application needs something else from the  
children, we could take the opportunity at that point to get it.

Does anyone have any comments or suggests on any of these approaches?

-Paul

--

---

<div class="post-metadata">

**Author:** ![lolcat](https://avatars.discourse-cdn.com/v4/letter/l/7993a0/32.png) [@lolcat](https://discuss.elastic.co/u/lolcat)\
**Post date:** [March 3, 2014, 4:03pm UTC](https://discuss.elastic.co/t/best-way-to-count-child-object-matches/9706/2 "2014-03-03T16:03:23Z")

</div>

Hi,

Did you manage to come up with an appropriate solution to manage counting child documents when you want a count of matches when performing the has\_child query ?

Thanks for your time 🙂

---

<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:46am UTC](https://discuss.elastic.co/t/best-way-to-count-child-object-matches/9706/3 "2017-07-06T01:46:10Z")

</div>


