# Problem Using 'must\_not' to Exclude Matches

**URL:** <https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531>\
**Category:** Elasticsearch\
**Created:** [March 13, 2020, 2:56pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531 "2020-03-13T14:56:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephencr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephencr/32/64421_2.png) [@stephencr](https://discuss.elastic.co/u/stephencr)\
**Post date:** [March 13, 2020, 2:56pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/1 "2020-03-13T14:56:03Z")

</div>

I'm trying to add a feature to my search where I can exclude users that contain certain hashtags from the result .

My code for including users that contain all of the given hashtags works, and is the same as the code below except it uses 'must' instead of 'must\_not'.

When I change 'must' to 'must\_not', it appears to have no effect whatsoever, instead of the intended effect of excluding users that contain those hashtags. I'm not sure what else to try or if I misunderstood the purpose of must\_not.

I'm using ES 6.8 in PHP.

```
foreach ($this->hashtags as $hashtag) {
	$query[] = [
		'nested' => [
			'path' => 'hashtags',
			'query' => [
				'bool' => [
					'must_not' => [
						[
							'match' => [
								'hashtags.hashtagId' => $hashtag
							]
						]
					]
				]
			]
		]
	];
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![itizir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itizir/32/63552_2.png) [@itizir](https://discuss.elastic.co/u/itizir)\
**Post date:** [March 13, 2020, 3:17pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/2 "2020-03-13T15:17:07Z")

</div>

I think nested queries mean "give me documents where any of the nested objects matches the query", so this is indeed not doing what you want... Try moving the `must_not` negation _outside_ of `nested`.

---

<div class="post-metadata">

**Author:** ![stephencr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephencr/32/64421_2.png) [@stephencr](https://discuss.elastic.co/u/stephencr)\
**Post date:** [March 13, 2020, 3:34pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/3 "2020-03-13T15:34:36Z")

</div>

That causes the following error:

Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root\_cause":[{"type":"parsing\_exception","reason":"no [query] registered for [must\_not]","line":1,"col":95}],"type":"parsing\_exception","reason":"no [query] registered for [must\_not]","line":1,"col":95},"status":400}

---

<div class="post-metadata">

**Author:** ![itizir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itizir/32/63552_2.png) [@itizir](https://discuss.elastic.co/u/itizir)\
**Post date:** [March 13, 2020, 3:40pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/4 "2020-03-13T15:40:06Z")

</div>

`must_not` still needs to be wrapped by a `bool` query. Did you move `bool` together with it?

---

<div class="post-metadata">

**Author:** ![stephencr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephencr/32/64421_2.png) [@stephencr](https://discuss.elastic.co/u/stephencr)\
**Post date:** [March 13, 2020, 3:52pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/5 "2020-03-13T15:52:25Z")

</div>

Moving the bool together with it fixed it, thanks a lot.

---

<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:** [April 10, 2020, 3:52pm UTC](https://discuss.elastic.co/t/problem-using-must-not-to-exclude-matches/223531/6 "2020-04-10T15:52:46Z")

</div>

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