# Aggregation to count parent documents by field in their children?

**URL:** <https://discuss.elastic.co/t/aggregation-to-count-parent-documents-by-field-in-their-children/239550>\
**Category:** Elasticsearch\
**Created:** [July 1, 2020, 11:03pm UTC](https://discuss.elastic.co/t/aggregation-to-count-parent-documents-by-field-in-their-children/239550 "2020-07-01T23:03:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobbyi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bobbyi/32/27949_2.png) [@bobbyi](https://discuss.elastic.co/u/bobbyi)\
**Post date:** [July 1, 2020, 11:03pm UTC](https://discuss.elastic.co/t/aggregation-to-count-parent-documents-by-field-in-their-children/239550/1 "2020-07-01T23:03:29Z")

</div>

I have an index that contains parent documents and child documents using the join datatype. I have a field on the child documents and for each unique value that appears in that field I would like to answer this question: How many parent documents are there that have at least one child document with this value in the field? So basically like a terms aggregation on the child document field, but rather than a count of how many child documents have each term, I want to know how many parent documents have a child that has the term.

Is there an aggregation that covers this use case? I looked over the aggregations in the documentation and couldn't find a way to handle this. My current plan is to fetch all of the unique values that appear for the child field using a terms aggregation, and then loop over the terms and for each one do a query for parent documents that have a child that have that term value.

---

<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 29, 2020, 11:03pm UTC](https://discuss.elastic.co/t/aggregation-to-count-parent-documents-by-field-in-their-children/239550/2 "2020-07-29T23:03:32Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
