Terms Vs. bool queries

I am trying to figure out (and failing) the relationship/advantages of term searches compared to boolean must/should searches.

Could anybody comment in which case should one be preferred over the other?


Those are simply different things. It’s like comparing the == operator to the || operator in Java; that wouldn’t make much sense, they simply have different purposes.

If you have a particular problem that you tried to solve with both predicates, please show your code, maybe I’ll understand what you mean.

Also, the reference documentation explains the purpose of each predicate:

Thanks for your reply @yrodiere

I read the documentation however, I still can’t see the specific use case difference for terms and boolean searches.

For example, aren’t the following queries equivalent?

List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.terms().field( "genre" )
                .matchingAny( Genre.CRIME_FICTION, Genre.SCIENCE_FICTION ) )
        .fetchHits( 20 );
List<Book> hits = searchSession.search( Book.class )
        .where( f -> f.bool()
                .should( f.match().field( "genre" )
                        .matching( Genre.CRIME_FICTION ) ) 
                .should( f.match().field( "genre" )
                        .matching( Genre.SCIENCE_FICTION ) ) 
        .fetchHits( 20 );

If they are equivalent, even when the second looks like just a more complex way to do it, it could have advantages when building more complex queries programmatically.

The explanation is right here in the documentation :

Functionally, this is somewhat similar to a boolean OR with one match predicate per term, but the syntax for a single terms predicate is more concise.

So. The terms predicate is a more specialized predicate. It’s more concise, and may perform better.

But unlike the boolean predicate it’s not able to combine any other query: it can only handle terms.