'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

So far I've succeeded in using the following query to grab all documents with only 1 child: (Inspired by this stackoverflow post solution)

GET my_index/_search
{
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "children",
          "query": {
            "exists": {
              "field": "children.name"
            }
          },
          "score_mode": "sum"
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "_score == 1 ? 1 : 0"
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  },
  "min_score": 1
}

And I know that I can use an aggregation to get a list of all the child name properties I'll need for step .2:

"aggs": {
  "child_names": {
    "terms": {
      "field": "name",
      "min_doc_count": 1
    }
  }
}

I stuck on how I can use the aggregation result in a following query that achieves step .2 (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 with query? (1 query is most ideal for my use case) Or perhaps there is a way to do this using a script that is nested inside a query?

Thanks for any help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source