diff --git a/src/java/org/apache/lucene/search/BooleanQuery.java b/src/java/org/apache/lucene/search/BooleanQuery.java index 932db8cd735..6f6673cb760 100644 --- a/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/src/java/org/apache/lucene/search/BooleanQuery.java @@ -294,7 +294,9 @@ public class BooleanQuery extends Query { BooleanClause c = (BooleanClause) cIter.next(); Scorer subScorer = w.scorer(reader, true, false); if (subScorer == null) { - return null; + if (c.isRequired()) { + return null; + } } else if (c.isRequired()) { required.add(subScorer); } else if (c.isProhibited()) { diff --git a/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java index a2edd224861..3a0969e3652 100644 --- a/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java +++ b/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java @@ -134,9 +134,7 @@ public class DisjunctionMaxQuery extends Query { for (Iterator iter = weights.iterator(); iter.hasNext();) { QueryWeight w = (QueryWeight) iter.next(); Scorer subScorer = w.scorer(reader, true, false); - if (subScorer == null) { - return null; - } else if (subScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { + if (subScorer != null && subScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { scorers[idx++] = subScorer; } } diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.java index ace8989d7d7..203f524fb24 100644 --- a/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/src/java/org/apache/lucene/search/IndexSearcher.java @@ -219,7 +219,9 @@ public class IndexSearcher extends Searcher { hcw.setNextReader(reader, 0); if (filter == null) { Scorer scorer = weight.scorer(reader, true, true); - scorer.score(hcw); + if (scorer != null) { + scorer.score(hcw); + } } else { searchWithFilter(reader, weight, filter, hcw); } @@ -239,7 +241,9 @@ public class IndexSearcher extends Searcher { for (int i = 0; i < subReaders.length; i++) { // search each subreader collector.setNextReader(subReaders[i], docStarts[i]); Scorer scorer = weight.scorer(subReaders[i], !collector.acceptsDocsOutOfOrder(), true); - scorer.score(collector); + if (scorer != null) { + scorer.score(collector); + } } } else { for (int i = 0; i < subReaders.length; i++) { // search each subreader diff --git a/src/test/org/apache/lucene/search/TestBooleanQuery.java b/src/test/org/apache/lucene/search/TestBooleanQuery.java index 13b06210ac6..e452a75af1f 100644 --- a/src/test/org/apache/lucene/search/TestBooleanQuery.java +++ b/src/test/org/apache/lucene/search/TestBooleanQuery.java @@ -16,7 +16,13 @@ package org.apache.lucene.search; * See the License for the specific language governing permissions and * limitations under the License. */ - +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.store.MockRAMDirectory; +import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.index.Term; @@ -50,5 +56,32 @@ public class TestBooleanQuery extends LuceneTestCase { // okay } } + + // LUCENE-1630 + public void testNullOrSubScorer() throws Throwable { + Directory dir = new MockRAMDirectory(); + IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); + Document doc = new Document(); + doc.add(new Field("field", "a b c d", Field.Store.NO, Field.Index.ANALYZED)); + w.addDocument(doc); + IndexReader r = w.getReader(); + IndexSearcher s = new IndexSearcher(r); + BooleanQuery q = new BooleanQuery(); + q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD); + + // PhraseQuery w/ no terms added returns a null scorer + PhraseQuery pq = new PhraseQuery(); + q.add(pq, BooleanClause.Occur.SHOULD); + assertEquals(1, s.search(q, 10).totalHits); + + DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f); + dmq.add(new TermQuery(new Term("field", "a"))); + dmq.add(pq); + assertEquals(1, s.search(dmq, 10).totalHits); + + r.close(); + w.close(); + dir.close(); + } }