Is it a predicate which guarantees no search results without performing any search?
My use case is: building a query from a complex code hierarchy where I can’y guarantee more predicates won’t be added, but I hit a condition for which the search should return none.
In case there is not, what could be the less expensive no-result predicate?
I am currently using a must for a negative id, but I fear that is a costly way to make my search return nothing.
Thanks
This looks like a reasonable use case; I opened [HSEARCH-4489] - Hibernate JIRA to add a matchNone
predicate to Hibernate Search.
In the meantime, you can use this code:
List<MyEntity> hits = searchSession.search( MyEntity.class )
.where( f -> f.bool( b -> {
if ( <the query must not match anything> ) {
b.mustNot( f.matchAll() );
}
else {
b.must( ... ); // Add regular predicates
}
} ) )
.fetchHits( 20 );
As you can see here, Lucene will optimize this and turn the boolean predicate into a predicate that matches no documents, which is what you want. Elasticsearch uses Lucene under the hood, so it should behave similarly.
Awesome! Thank you @yrodiere