Match all children items in an array

I have the below structure

Doc1:
        {
            "key1": "value1",
            "key2": [
                {
                    "subId": "id1",
                    "subKey1": "PASS"
                },
                {
                    "subId": "id2",
                    "subKey1": "PASS"
                }
            ]
        },
         
        Doc2:
        {
            "key1": "value2",
            "key2": [
                {
                    "subId": "id1",
                    "subKey1": "PASS"
                },
                {
                    "subId": "id2",
                    "subKey1": "FAIL"
                }
            ]
        }

I want to search for doc in which inside "key2.subKey": "PASS" for all children inside the array. So the query in this case should just return Doc1

I dont know if I'm right, but this should meet your req

GET <your_index>/_search
{
"query": {
"bool": {
"must": [
{"match": {
"key2.subKey1": "PASS"
}}
],
"must_not": [
{"match": {
"key2.subKey1": "FAIL"
}}
]
}
}
}

Key2 has nested mapping. The above query won't work in this scenario.

bump

Hi @akshaymaniyar.

Please try this.

Elastic docs

Just give you an idea. No actual syntax honored.

"bool" : { 
     "must_not" : { 
          "nested" : {
                   "path" : "something",
                   "query" : {
                               "bool" : { 
                                    // here you also might need to match all child docs as well
                                   "must_not" : { 
                                          "term" : {  "subKey1": "PASS"}
                                     }
                                }
                    }
           }
      }
 }

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