diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java index f80597d38e6..20919dc1695 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java @@ -604,23 +604,20 @@ public class BooleanQuery extends Query implements Iterable { // Important(this can only be processed after nested clauses have been flattened) { final Collection shoulds = clauseSets.get(Occur.SHOULD); - if (shoulds.size() > 0) { - if (shoulds.size() < minimumNumberShouldMatch) { - return new MatchNoDocsQuery("SHOULD clause count less than minimumNumberShouldMatch"); - } - - if (shoulds.size() == minimumNumberShouldMatch) { - BooleanQuery.Builder builder = new BooleanQuery.Builder(); - for (BooleanClause clause : clauses) { - if (clause.occur() == Occur.SHOULD) { - builder.add(clause.query(), Occur.MUST); - } else { - builder.add(clause); - } + if (shoulds.size() < minimumNumberShouldMatch) { + return new MatchNoDocsQuery("SHOULD clause count less than minimumNumberShouldMatch"); + } + if (shoulds.size() > 0 && shoulds.size() == minimumNumberShouldMatch) { + BooleanQuery.Builder builder = new BooleanQuery.Builder(); + for (BooleanClause clause : clauses) { + if (clause.occur() == Occur.SHOULD) { + builder.add(clause.query(), Occur.MUST); + } else { + builder.add(clause); } - - return builder.build(); } + + return builder.build(); } } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java index b876fb48963..89d07f66720 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java @@ -401,14 +401,12 @@ public class TestBooleanRewrites extends LuceneTestCase { bq = new BooleanQuery.Builder() - .setMinimumNumberShouldMatch(random().nextInt(5)) .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new TermQuery(new Term("foo", "baz")), Occur.MUST) .add(new MatchAllDocsQuery(), Occur.FILTER) .build(); Query expected = new BooleanQuery.Builder() - .setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch()) .add(new TermQuery(new Term("foo", "bar")), Occur.MUST) .add(new TermQuery(new Term("foo", "baz")), Occur.MUST) .build(); @@ -476,7 +474,22 @@ public class TestBooleanRewrites extends LuceneTestCase { Query query = randomBooleanQuery(random()); final TopDocs td1 = searcher1.search(query, 100); final TopDocs td2 = searcher2.search(query, 100); - assertEquals(td1, td2); + try { + assertEquals(td1, td2); + } catch (AssertionError e) { + System.out.println(query); + Query rewritten = query; + do { + query = rewritten; + rewritten = query.rewrite(searcher1); + System.out.println(rewritten); + TopDocs tdx = searcher2.search(rewritten, 100); + if (td2.totalHits.value() != tdx.totalHits.value()) { + System.out.println("Bad"); + } + } while (query != rewritten); + throw e; + } } searcher1.getIndexReader().close();