# Negating filter (RestHighLevelClient)

**URL:** <https://discuss.elastic.co/t/negating-filter-resthighlevelclient/239061>\
**Category:** Elasticsearch\
**Created:** [June 29, 2020, 5:47am UTC](https://discuss.elastic.co/t/negating-filter-resthighlevelclient/239061 "2020-06-29T05:47:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_E](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Thomas\_E](https://discuss.elastic.co/u/Thomas_E)\
**Post date:** [June 29, 2020, 5:47am UTC](https://discuss.elastic.co/t/negating-filter-resthighlevelclient/239061/1 "2020-06-29T05:47:30Z")

</div>

I have

```
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.filter(termQuery(...))

```

and now want to add a filter with a negated condition.

With

```
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.filter(termQuery(...))
queryBuilder.mustNot(termQuery(...))

```

I didn't get the expected result.

Instead

```
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(...);
queryBuilder.mustNot(...)

```

works for me.

Can someone explain this behavior? Is must/mustNot the way to go or is it with filter possible?

PS: I don't need any scoring.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 29, 2020, 6:51am UTC](https://discuss.elastic.co/t/negating-filter-resthighlevelclient/239061/2 "2020-06-29T06:51:59Z")

</div>

Your code looks good.

There's probably something else but we cannot tell without more details.

Ideally reproduce the problem with a pure Elasticsearch script which can be run in Kibana dev console and we can work from that.

---

<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 27, 2020, 6:52am UTC](https://discuss.elastic.co/t/negating-filter-resthighlevelclient/239061/3 "2020-07-27T06:52:01Z")

</div>

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