# Get parents whose none of children matches

**URL:** https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222
**Category:** Elasticsearch
**Created:** [June 29, 2016, 1:06am UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222 "2016-06-29T01:06:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jondeandres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondeandres/32/10612_2.png) [@jondeandres](https://discuss.elastic.co/u/jondeandres)
#### Post date: [June 29, 2016, 1:06am UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/1 "2016-06-29T01:06:17Z")

</div>

Hi,

having a parent doc\_type A and children doc\_type B. Is there a way to query "all the A documents where NONE/ALL of their Bs children matches some filters"?

Having:

A1  
B1.foo = 200  
B2.foo = 100  
A2  
B3. foo = 100

And this query:

`{ "query": { "has_child": { "type": "B", "query": { "filtered": { "filter": { "bool": { "must_not": { "match": { "user_id": 200 }} } } } } } } }`

I get both A1 and A2, since A1 has a child with foo != 200.

Thanks.

---

<div class="post-metadata">

### Author: ![bw99](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bw99](https://discuss.elastic.co/u/bw99)
#### Post date: [July 11, 2016, 2:49pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/2 "2016-07-11T14:49:20Z")

</div>

I accomplish this by going down another level of bool.

So in English, I say "Give me As that have children that do not have parents that have children that have Bs"

So you have to use the has\_parent as well as the has\_child.

It's a bit fiddly but it works.

---

<div class="post-metadata">

### Author: ![jondeandres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondeandres/32/10612_2.png) [@jondeandres](https://discuss.elastic.co/u/jondeandres)
#### Post date: [July 11, 2016, 3:02pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/3 "2016-07-11T15:02:13Z")

</div>

Hi bw99, thanks for replying. Could you explain this with an example?

Thank you very much.

---

<div class="post-metadata">

### Author: ![bw99](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bw99](https://discuss.elastic.co/u/bw99)
#### Post date: [July 12, 2016, 3:12pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/4 "2016-07-12T15:12:19Z")

</div>

Oh, now I look at it, I think maybe you don't need what I said.

You just need to say (in pseudo code!)

query {  
must not {  
has child {  
must {  
{ "match": { "user\_id": 200 }}  
}  
}  
}  
}

I think you're saying "Give me parents that have a child that doesn't have 200", whereas you need to be saying "Give me parents that do NOT have a child that DOES (must) have 200"

---

<div class="post-metadata">

### Author: ![jondeandres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondeandres/32/10612_2.png) [@jondeandres](https://discuss.elastic.co/u/jondeandres)
#### Post date: [July 12, 2016, 3:28pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/5 "2016-07-12T15:28:36Z")

</div>

bw99, I thought has\_child needed to work outside the must\_not clause.

I need a query to get parents whose NONE of their children match a certain query. Also, a query to get parents whose ALL of their children match a certain query.

Finally we've modeled the data in order to not depend on this query, but it'd be interesting know if there is a way in ES to do this.

Thanks bw99.

---

<div class="post-metadata">

### Author: ![bw99](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@bw99](https://discuss.elastic.co/u/bw99)
#### Post date: [July 12, 2016, 3:32pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/6 "2016-07-12T15:32:12Z")

</div>

> bw99, I thought has\_child needed to work outside the must\_not clause.

I'm not sure what you mean. You can use boolean clauses nested several levels deep. So in pseudo code you're saying:

```
Give me all parents who
  must not have
    a child that
      must have 
        200

```

Not sure how you would do all (haven't needed to do that yet) - but I think the above should work for your first use case.

---

<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: [July 5, 2017, 10:36pm UTC](https://discuss.elastic.co/t/get-parents-whose-none-of-children-matches/54222/7 "2017-07-05T22:36:17Z")

</div>


