Negating filter (RestHighLevelClient)

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.

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.