In short: no, the QueryBuilder does not allow that yet. I just created a ticket so that we add this feature in the next version of Hibernate Search.
In the meantime, you can fall back to native queries to achieve what you want:
If you are running Hibernate Search in embedded Lucene mode (the default), you can use new org.apache.lucene.search.BooleanQuery.Builder().should(...).should(...).must(...).setMinimumNumberShouldMatch(...).build() to build the boolean query. Note that the sub-queries you will pass to should/must can still be built using the Hibernate Search QueryBuilder.
If you are using the experimental Elasticsearch integration, you can use org.hibernate.search.elasticsearch.ElasticsearchQueries.fromJson( ... ). You will have to write the whole query as JSON, though, and will not be able to take advantage of the Hibernate Search QueryBuilder at all.
@sant0s We are about to backport the feature to Search 5. Could you confirm that the following syntaxes would cover all of your use cases, please? Thanks.
// Example 1: at least 3 "should" clauses have to match
booleanContext1.minimumShouldMatchNumber( 3 );
// Example 2: at most 2 "should" clauses may not match
booleanContext2.minimumShouldMatchNumber( -2 );
// Example 3: at least 75% of "should" clauses have to match (rounded down)
booleanContext3.minimumShouldMatchPercent( 75 );
// Example 4: at most 25% of "should" clauses may not match (rounded down)
booleanContext4.minimumShouldMatchPercent( -25 );
Be careful, what you’re doing will be ignored by our Elasticsearch integration. There is some conversion logic involved to convert from Lucene Queries to Elasticsearch JSON, and currently it doesn’t take minimumShouldMatch into account. So your code will have the exact same effect as using the DSL and not providing a minimumShouldMatch at all, unfortunately.
I think it should work, though obviously it should only be used as a last resort. With more than 3 conditions, it will get out of control really fast.
Not sure about how that would affect scoring, but I guess some unwanted boost is a small price to pay if the query is at least filtering the results correctly.
I’ll try to release 5.10.2 in the next few days so that you can avoid that though, I really don’t want to force anyone to do such thing.