# Must\_not nested with should query not working (ES version 7.10.2) -- not countryiso OR not branch\_id

**URL:** https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737
**Category:** Elasticsearch
**Created:** [May 2, 2022, 10:12am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737 "2022-05-02T10:12:30Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 10:12am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/1 "2022-05-02T10:12:30Z")

</div>

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "is_reachable": {
              "value": "YES",
              "boost": 1
            }
          }
        },
        {
          "exists": {
            "field": "user_id",
            "boost": 1
          }
        },
        {
          "terms": {
            "account_type": [
              "LEAD"
            ],
            "boost": 1
          }
        }
      ],
      "must_not": [
        {
          "exists": {
            "field": "customer_on",
            "boost": 1
          }
        },
        {
          "nested": {
            "query": {
              "bool": {
                "filter": [
                  {
                    "bool": {
                      "should": [
                        {
                          "terms": {
                            "customer_business_relations.country_iso": [
                              "AE"
                            ],
                            "boost": 1
                          }
                        },
                        {
                          "terms": {
                            "customer_business_relations.branch_id": [
                              11385
                            ],
                            "boost": 1
                          }
                        }
                      ],
                      "adjust_pure_negative": true,
                      "boost": 1
                    }
                  },
                  {
                    "terms": {
                      "customer_business_relations.account_type": [
                        "LEAD"
                      ],
                      "boost": 1
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            "path": "customer_business_relations",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "ACCOUNT_TYPE": {
      "terms": {
        "field": "account_type",
        "size": 5,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "PHONE": {
      "filter": {
        "term": {
          "has_phone": {
            "value": "true",
            "boost": 1
          }
        }
      }
    },
    "REACHABLE_PHONES": {
      "filter": {
        "term": {
          "is_phone_reachable": {
            "value": "true",
            "boost": 1
          }
        }
      }
    },
    "EMAIL": {
      "filter": {
        "term": {
          "has_email": {
            "value": "true",
            "boost": 1
          }
        }
      }
    },
    "REACHABLE_EMAILS": {
      "filter": {
        "term": {
          "is_email_reachable": {
            "value": "true",
            "boost": 1
          }
        }
      }
    },
    "UNDELIVERED_NESTED_MAPPINGS": {
      "nested": {
        "path": "undelivered_communications"
      },
      "aggregations": {
        "UNDELIVERED_COMMUNICATION": {
          "terms": {
            "field": "undelivered_communications.channel",
            "size": 5,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        }
      }
    }
  }
}

```

in the must\_not part i have two condition inside should . if any one condition true count must be increased .but count is not getting increased

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [May 2, 2022, 10:48am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/2 "2022-05-02T10:48:22Z")

</div>

You have a big query, so I am posting the relavant part, I hope you are talking about following part of your query.

```auto
         {
                                                    "terms":
                                                    {
                                                        "customer_business_relations.country_iso":
                                                        [
                                                            "AE"
                                                        ],
                                                        "boost": 1
                                                    }
                                                },
                                                {
                                                    "terms":
                                                    {
                                                        "customer_business_relations.branch_id":
                                                        [
                                                            11385
                                                        ],
                                                        "boost": 1
                                                    }
                                                }

```

If yes, can you explain what do you mean by count not increasing? do you mean the no of search hit, returned by Elasticsearch ?

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 11:03am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/3 "2022-05-02T11:03:22Z")

</div>

yes,this is the part,count means the aggregation will return doc\_count .example  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/8/8880ee731f74ac4d9100765b5322e129021fad27.png)

if any one condition true need to increase the count

this is the condition (must\_not country\_iso) OR (must\_not branch\_id)

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [May 2, 2022, 11:06am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/4 "2022-05-02T11:06:49Z")

</div>

> [@Udayaprakash\_S\_10d](#):
>
> y one condition true need to increase the count

Thanks @Udayaprakash_S_10d , but you are writing these two conditions, which are enclosed by the `must_not` clause, so instead of increasing the count, it will decrease the count, if it matches the condition. Hope this helps.

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 11:16am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/5 "2022-05-02T11:16:38Z")

</div>

yes correct .i put that two condition inside must\_not,if i create one user with "AE" ="country\_iso" ( condition false) and different "branchid" (condition true) so the second condition is true so,i need to increase the count. but the count remain same

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 11:25am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/6 "2022-05-02T11:25:25Z")

</div>

my query condition must\_not( (country\_iso = "AE) OR (branch\_id = 11385) )

so ,if i create one user with country\_iso = "AE" (false) ,  
branch\_id = 11200 (true)

so the second condition is true ,thats why i need to increase the count

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [May 2, 2022, 12:05pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/7 "2022-05-02T12:05:18Z")

</div>

Thanks for example, it makes sense, can you also provide the mapping of your index, I see you are using the nested query but missing the `path` param in the query, can you add it in the request as explained in the [document](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) , and test again?

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 12:10pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/8 "2022-05-02T12:10:30Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/c/2/c27c75b03078ad3732667982a9367daca290145f.png)  
mapping

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 12:12pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/9 "2022-05-02T12:12:08Z")

</div>

also tested with path param  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/2/c2362ec48842d2dbc14126dbe6cb2caa21e7cf6e.png)  
getting error  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/7/67630d81c0a682813ecb0973d02d5312bec7238a.png)

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [May 2, 2022, 12:15pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/10 "2022-05-02T12:15:43Z")

</div>

Sorry, my bad, I see you already defined that in your old query, and error message is clearly indicating it, you can remove it.

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 1:22pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/11 "2022-05-02T13:22:50Z")

</div>

any help regarding this condition

my query condition must\_not( (country\_iso = "AE) OR (branch\_id = 11385) )

so ,if i create one user with country\_iso = "AE" (false) ,  
branch\_id = 11200 (true)

so the second condition is true , need to increase the count

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 2, 2022, 5:50pm UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/12 "2022-05-02T17:50:58Z")

</div>

i have two condition in must\_not with OR condition .need to compare that two condition in every document if any one condition true need that response in doc\_count

but now ,what happens----\> if the user country\_iso = "AE" the must\_not removed the doc related the country condition , so the next condition is not getting compared, because the country condition is removed the doc in response

the main logic which I want is , both the condition are need to be compared simultaneously in the doc

please help

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [May 3, 2022, 5:07am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/13 "2022-05-03T05:07:52Z")

</div>

Welcome to our community @Udayaprakash_S_10d! 😃

Please format your code/logs/config using the `</>` button, or markdown style back ticks. It helps to make things easy to read which helps us help you. Also don't post pictures of text, logs or code. They are difficult to read, impossible to search and replicate (if it's code), and some people may not be even able to see them 🙂

---

<div class="post-metadata">

### Author: ![Udayaprakash\_S\_10d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/udayaprakash_s_10d/32/105116_2.png) [@Udayaprakash\_S\_10d](https://discuss.elastic.co/u/Udayaprakash_S_10d)
#### Post date: [May 3, 2022, 8:59am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/14 "2022-05-03T08:59:54Z")

</div>

sure

---

<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: [May 31, 2022, 9:00am UTC](https://discuss.elastic.co/t/must-not-nested-with-should-query-not-working-es-version-7-10-2-not-countryiso-or-not-branch-id/303737/15 "2022-05-31T09:00:22Z")

</div>

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