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 f80597d38e6d..20919dc16959 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 Query rewrite(IndexSearcher indexSearcher) throws IOException { // 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 b876fb48963f..89d07f667204 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 void testRemoveMatchAllFilter() throws IOException { 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 Query rewrite(Query original) throws IOException { 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();