Retrieve all documents with a field value that is included a previous query's field values

I have a unique query use case that I can't seem to find covered in the documentation. (I'm using ElasticSearch 7.17)

I have documents inside an index that are shaped the following way:

{
  parentName: string
  children: [
    { name: string },
    { name: string },
  ]
}

Each document can have an array of children ranging from size 0 to N (any number). Due to a data discrepancy, I need to do the following with a query:

  1. Get a list of parentName values from all documents that only have 1 child
  2. Use this list inside a query to pull all documents that have more than 1 child AND contain a child whose name is equal to some parentName inside the list

Is this possible to do from one query? (Or perhaps there is a way to do this with scripts?)

So far I've succeeded in using an query and aggregation to achieve step one, but I don't understand how I can use the aggregation result in a following query

Thanks for any help

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