| It works as intended. As explained in the Lucene and Elasticsearch documentation, should clauses are completely optional if there is also a must clause in the same boolean junction. If you get a different behavior in Elasticsearch, I would suggest to look into you analyzer definitions, or check that you reindexed since you last changed your mapping. It's likely that the query doesn't match for a reason completely unrelated to minimum_should_match. By the way, you're using BooleanQuery.Builder needlessly, you can just use the query builder (qb.bool(). If you want to make a single should clause mandatory, you obviously could use must instead. But I guess this was just a simplified example. If you have multiple should clauses and you want to make at least one of them mandatory, just separate the should clauses from the must clauses:
Query query = qb.bool()
.must(new TermQuery(new Term("id", "1")))
.filteredBy(qb.bool()
.must(qb.bool()
.should(new TermQuery(new Term("firstName", "non-existing")))
.should()
.should()
.createQuery()
)
.must(new TermQuery(new Term("lastName", "jaric")))
.createQuery()
)
.createQuery();
Or, if you want more fine-grained control, or do not want to change your queries that much, just upgrade to Hibernate Search 5.10.2 or later and use minimum_should_match, either through BooleanQuery.Builder.setMinimumNumberShouldMatch(1) or through qb.bool().minimumShouldMatchNumber(1). |