# Creating a NOT EQUAL filter in Java Fluent API

**URL:** https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859
**Category:** Elasticsearch
**Created:** [July 18, 2022, 8:32am UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859 "2022-07-18T08:32:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bweston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bweston/32/108437_2.png) [@bweston](https://discuss.elastic.co/u/bweston)
#### Post date: [July 18, 2022, 8:32am UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/1 "2022-07-18T08:32:16Z")

</div>

Hi,

I'm trying to compose a NOT EQUAL filter with the new Java Fluent API, querying Elastic 8.2 and have drawn a blank. The query below returns all documents that have expired according to our company retention policy, together with a few other criteria.

For efficiency, I want to turn the mustNot(beInTrash) part of the bool query into a filter query, essentially stating that the document shouldn't be in a folder starting with a certain prefix, however I'm not sure of the syntax. The other filters are pretty straightforward as they operate on boolean or discrete values.

Thanks in advance.

Blake

```auto
...
var expiredItemsQuery = BoolQuery.of(b -> b
                    .filter(isAnExpiredItem())
                    .filter(isNotDeleted())
                    .filter(isNotSuspended())
                    .filter(isAFile())
                    .mustNot(beInTrash())
            )._toQuery();
...

protected Query beInTrash() {
        return PrefixQuery.of(p -> p
                .field("path.keyword")
                .value("Trash/")
        )._toQuery();
}

protected Query isNotDeleted() {
        return TermQuery.of(t -> t
                .field("is_deleted")
                .value(false)
        )._toQuery();
    }

```

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [July 18, 2022, 12:44pm UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/2 "2022-07-18T12:44:02Z")

</div>

Hi @bweston

how you use the "path.keyword" will exclude only the docs that have this path "Trash/". Docs with path "Trash/folderXpto" would remain in your results.  
That's the problem?

---

<div class="post-metadata">

### Author: ![bweston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bweston/32/108437_2.png) [@bweston](https://discuss.elastic.co/u/bweston)
#### Post date: [July 19, 2022, 6:44am UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/3 "2022-07-19T06:44:33Z")

</div>

Not quite. It's a PrefixQuery. Therefore any documents with a prefix of Trash/ for the path will be excluded. That works just fine. It excludes what I need it to exclude, including folders Trash/123 and Trash/xyz. The issue isn't with the logic, it's more of a performance thing.

I am using the results of this query in an aggregation and therefore I don't care about scoring and as per the Elastic recommendations, I want to turn it into a filter, rather than the must not query it is at the moment.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [July 19, 2022, 12:13pm UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/4 "2022-07-19T12:13:39Z")

</div>

De acordo com a documentação vc já esta usando o filtro.

> `must_not` The clause (query) must not appear in the matching documents. Clauses are executed in [filter context](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html) meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of `0` for all documents is returned.

---

<div class="post-metadata">

### Author: ![bweston](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bweston/32/108437_2.png) [@bweston](https://discuss.elastic.co/u/bweston)
#### Post date: [July 19, 2022, 12:29pm UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/5 "2022-07-19T12:29:37Z")

</div>

You learn something new every day. I didn't realise must\_not was executed in the filter context. I had assumed that it was only executed as a filter if it was wrapped by a filter construct. Therefore my code can stay as-is.

Thanks for your help.

---

<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: [August 16, 2022, 12:29pm UTC](https://discuss.elastic.co/t/creating-a-not-equal-filter-in-java-fluent-api/309859/6 "2022-08-16T12:29:54Z")

</div>

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