# Bool filter and must - must not Query with nested not right response

**URL:** <https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481>\
**Category:** Elasticsearch\
**Created:** [August 23, 2019, 9:37am UTC](https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481 "2019-08-23T09:37:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![z-Dapor](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@z-Dapor](https://discuss.elastic.co/u/z-Dapor)\
**Post date:** [August 23, 2019, 9:37am UTC](https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481/1 "2019-08-23T09:37:40Z")

</div>

This is my mappings:  
"mappings": {  
"\_doc":{  
"properties": {  
"channels": {  
"type": "nested",  
"properties":{  
"name":{  
"type":"keyword"  
},  
"value":{  
"type":"long"  
}  
}  
}

This is my DSL:  
GET my\_index2/\_search  
{  
"\_source": "channels.name",  
"query": {  
"bool": {  
"filter": {  
"nested": {  
"path": "channels",  
"query": {"bool": {  
"must": [  
{"match": {  
"channels.name": "cXl0zgxy1e6"  
}}  
],  
"must\_not": [  
{"match": {  
"channels.name": "SwZ0PLh0gxD"  
}}  
]  
}}  
}  
}  
}  
}  
}

This is result：  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/1/91720b9f9aed93de2cd976a3bb440acc61c098f2.png)  
The result did not follow my expectations；  
The channels is a array contains nested type;  
I dont have any idea...  
And I want get doc that nested objet equal “cXl0zgxy1e6“，but not ”SwZ0PLh0gxD“；  
Can you help me？ Thanks ！！

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 26, 2019, 9:47am UTC](https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481/2 "2019-08-26T09:47:30Z")

</div>

There is a problem with the boolean logic of your `bool` query. 🙂 Which is not uncommon, because when you combine `bool` queries with `nested` queries, things become a bit complicated.

Your query basically asks Elasticsearch "find me a document that has at least one nested object that does not have a `channel.name` equal to `SwZ0PLh0gxD`." Which your document matches, because there is a nested object that has a `channel.name` set to `cXl0zgxy1e6`, which is not `SwZ0PLh0gxD`.

What you want to do instead is take the `bool` query out of the `nested` query. This way you ask Elasticsearch "find me a document that does not have any nested object that has a `channel name` equal to `SwZ0PLh0gxD`. That query would look like this:

```auto
GET my_index2/_search
{
  "_source": "channels.name",
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "channels",
          "query": {
            "match": {
              "channels.name": "cXl0zgxy1e6"
            }
          }
        }
      },
      "must_not": [
        {
          "nested": {
            "path": "channels",
            "query": {
              "match": {
                "channels.name": "SwZ0PLh0gxD"
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![z-Dapor](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@z-Dapor](https://discuss.elastic.co/u/z-Dapor)\
**Post date:** [August 26, 2019, 10:03am UTC](https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481/3 "2019-08-26T10:03:37Z")

</div>

ye This is useful; Thank you for your kind reply,

---

<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:** [September 23, 2019, 10:03am UTC](https://discuss.elastic.co/t/bool-filter-and-must-must-not-query-with-nested-not-right-response/196481/4 "2019-09-23T10:03:45Z")

</div>

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