Aggregation to count parent documents by field in their children?

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.

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