Creating a NOT EQUAL filter in Java Fluent API


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.


var expiredItemsQuery = BoolQuery.of(b -> b

protected Query beInTrash() {
        return PrefixQuery.of(p -> p

protected Query isNotDeleted() {
        return TermQuery.of(t -> t

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?

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.

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 meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.

1 Like

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.

1 Like

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