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

**URL:** <https://discuss.elastic.co/t/retrieve-all-documents-with-a-field-value-that-is-included-a-previous-querys-field-values/300639>\
**Category:** Elasticsearch\
**Created:** [March 24, 2022, 8:53pm UTC](https://discuss.elastic.co/t/retrieve-all-documents-with-a-field-value-that-is-included-a-previous-querys-field-values/300639 "2022-03-24T20:53:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![megancooper](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@megancooper](https://discuss.elastic.co/u/megancooper)\
**Post date:** [March 24, 2022, 8:53pm UTC](https://discuss.elastic.co/t/retrieve-all-documents-with-a-field-value-that-is-included-a-previous-querys-field-values/300639/1 "2022-03-24T20:53:15Z")

</div>

I have a unique query use case that I can't seem to find covered in the documentation. (I'm using [ElasticSearch 7.17](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/index.html))

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

```auto
{
  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

---

<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:** [April 21, 2022, 8:53pm UTC](https://discuss.elastic.co/t/retrieve-all-documents-with-a-field-value-that-is-included-a-previous-querys-field-values/300639/2 "2022-04-21T20:53:28Z")

</div>

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