# Query for matching and not matching at same level

**URL:** <https://discuss.elastic.co/t/query-for-matching-and-not-matching-at-same-level/251331>\
**Category:** Elasticsearch\
**Created:** [October 7, 2020, 6:11pm UTC](https://discuss.elastic.co/t/query-for-matching-and-not-matching-at-same-level/251331 "2020-10-07T18:11:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopal\_Chauhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_chauhan/32/51033_2.png) [@Gopal\_Chauhan](https://discuss.elastic.co/u/Gopal_Chauhan)\
**Post date:** [October 7, 2020, 6:11pm UTC](https://discuss.elastic.co/t/query-for-matching-and-not-matching-at-same-level/251331/1 "2020-10-07T18:11:45Z")

</div>

Hi ,

I have kind of below data in ES (version 6.2). I am trying to write a query with below condition

Addresses.Type="Home" and Addresses.Suspended \<\> "B" and Addresses.Suspended \<\> "Y". these three condition should be at same level.

'{  
"ID":"1",  
"Addresses":[  
{  
"FirstName": "Sam",  
"Suspended": "N"  
"Type": "Home"  
},  
{  
"FirstName": "Bill",  
"Suspended": "B"  
"Type": "Office"  
}  
]  
},  
{  
"ID":"2",  
"Addresses":[  
{  
"FirstName": "Maddie",  
"Suspended": "B"  
"Type": "Home"  
},  
{  
"FirstName": "John",  
"Suspended": "B"  
"Type": "Office"  
}  
]  
}'

I wrote below query. But the issue is , must and must\_not are working independently. I wanted first document as result. All Document which has type home and suspended \<\> "B" and suspended \<\> "Y".  
In First document, one section of address is matching above condition and should come as part of result. Right now nothing is coming. Because of must\_not filtering out both records.

'GET testindex/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"bool": {  
"must": [  
{  
"Addresses.Type": {  
"query": "Home"  
}

```
          }
        ]
      }
    }
  ],
  "must_not": [
    {
      "bool": {
        "should": [
          {
            "bool": {
              "must": [
                {
                  "terms": {
                    "Addresses.Suspended": ["b","y"]
                  }
                }
              ]
            }
          }
          ]
      }
    }
  ]
}

```

}  
}'

---

<div class="post-metadata">

**Author:** ![Gopal\_Chauhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gopal_chauhan/32/51033_2.png) [@Gopal\_Chauhan](https://discuss.elastic.co/u/Gopal_Chauhan)\
**Post date:** [October 7, 2020, 6:15pm UTC](https://discuss.elastic.co/t/query-for-matching-and-not-matching-at-same-level/251331/2 "2020-10-07T18:15:42Z")

</div>

```auto
GET testindex/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                  "Addresses.Type": {
                    "query": "Home"
                  }
                
              }
            ]
          }
        }
      ],
      "must_not": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "terms": {
                        "Addresses.Suspended": ["b","y"]
                      }
                    }
                  ]
                }
              }
              ]
          }
        }
      ]
    }
  }
}

```

---

<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:** [November 4, 2020, 6:15pm UTC](https://discuss.elastic.co/t/query-for-matching-and-not-matching-at-same-level/251331/3 "2020-11-04T18:15:53Z")

</div>

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