Apache Lucene open-source search software
LUCENE-1754: BooleanQuery detects up front if it won't match any docs and returns null from its scorer() instead of NonMatchingScorer
Michael McCandless26a2c427d1ae
diff --git a/CHANGES.txt b/CHANGES.txtindex d3372d93cfd..d545a9dc637 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -337,6 +337,16 @@ API Changesmade public as expert, experimental APIs. These APIs may suddenlychange from release to release (Jason Rutherglen via MikeMcCandless).++32. LUCENE-1754: QueryWeight.scorer() can return null if no documents+ are going to be matched by the query. Similarly,+ Filter.getDocIdSet() can return null if no documents are going to+ be accepted by the Filter. Note that these 'can' return null,+ however they don't have to and can return a Scorer/DocIdSet which+ does not match / reject all documents. This is already the+ behavior of some QueryWeight/Filter implementations, and is+ documented here just for emphasis. (Shai Erera via Mike+ McCandless)Bug fixes@@ -663,6 +673,11 @@ OptimizationsRemoves conversions between Set and array.(Simon Willnauer via Mark Miller)+11. LUCENE-1754: BooleanQuery.queryWeight.scorer() will return null if+ it won't match any documents (e.g. if there are no required and+ optional scorers, or not enough optional scorers to satisfy+ minShouldMatch). (Shai Erera via Mike McCandless)+DocumentationBuilddiff --git a/contrib/miscellaneous/src/java/org/apache/lucene/misc/ChainedFilter.java b/contrib/miscellaneous/src/java/org/apache/lucene/misc/ChainedFilter.javaindex 0731c4bb241..a36bdfcdb4c 100644--- a/contrib/miscellaneous/src/java/org/apache/lucene/misc/ChainedFilter.java+++ b/contrib/miscellaneous/src/java/org/apache/lucene/misc/ChainedFilter.java@@ -60,6 +60,7 @@ import org.apache.lucene.search.Filter;import java.io.IOException;import org.apache.lucene.search.DocIdSet;import org.apache.lucene.search.DocIdSetIterator;+import org.apache.lucene.search.EmptyDocIdSetIterator;import org.apache.lucene.util.OpenBitSet;import org.apache.lucene.util.OpenBitSetDISI;import org.apache.lucene.util.SortedVIntList;@@ -146,9 +147,18 @@ public class ChainedFilter extends Filter}private DocIdSetIterator getDISI(Filter filter, IndexReader reader)- throws IOException- {- return filter.getDocIdSet(reader).iterator();+ throws IOException {+ DocIdSet docIdSet = filter.getDocIdSet(reader);+ if (docIdSet == null) {+ return EmptyDocIdSetIterator.getInstance();+ } else {+ DocIdSetIterator iter = docIdSet.iterator();+ if (iter == null) {+ return EmptyDocIdSetIterator.getInstance();+ } else {+ return iter;+ }+ }}private OpenBitSetDISI initialResult(IndexReader reader, int logic, int[] index)@@ -241,13 +251,11 @@ public class ChainedFilter extends Filter}private void doChain(OpenBitSetDISI result, int logic, DocIdSet dis)- throws IOException- {+ throws IOException {if (dis instanceof OpenBitSet) {// optimized case for OpenBitSets- switch (logic)- {+ switch (logic) {case OR:result.or((OpenBitSet) dis);break;@@ -265,9 +273,17 @@ public class ChainedFilter extends Filterbreak;}} else {- DocIdSetIterator disi = dis.iterator();- switch (logic)- {+ DocIdSetIterator disi;+ if (dis == null) {+ disi = EmptyDocIdSetIterator.getInstance();+ } else {+ disi = dis.iterator();+ if (disi == null) {+ disi = EmptyDocIdSetIterator.getInstance();+ }+ }++ switch (logic) {case OR:result.inPlaceOr(disi);break;diff --git a/src/java/org/apache/lucene/analysis/Analyzer.java b/src/java/org/apache/lucene/analysis/Analyzer.javaindex 91605fa15a4..d98438bb86e 100644--- a/src/java/org/apache/lucene/analysis/Analyzer.java+++ b/src/java/org/apache/lucene/analysis/Analyzer.java@@ -133,7 +133,7 @@ public abstract class Analyzer {* fields. This method is only called if the field* produced at least one token for indexing.*- * @param Fieldable the field just indexed+ * @param field the field just indexed* @return offset gap, added to the next token emitted from {@link #tokenStream(String,Reader)}*/public int getOffsetGap(Fieldable field) {diff --git a/src/java/org/apache/lucene/index/DocumentsWriter.java b/src/java/org/apache/lucene/index/DocumentsWriter.javaindex c5a70e1a4cf..11781967b2e 100644--- a/src/java/org/apache/lucene/index/DocumentsWriter.java+++ b/src/java/org/apache/lucene/index/DocumentsWriter.java@@ -1007,12 +1007,14 @@ final class DocumentsWriter {int limit = ((Integer) entry.getValue()).intValue();QueryWeight weight = query.queryWeight(searcher);Scorer scorer = weight.scorer(reader, true, false);- while(true) {- int doc = scorer.nextDoc();- if (((long) docIDStart) + doc >= limit)- break;- reader.deleteDocument(doc);- any = true;+ if (scorer != null) {+ while(true) {+ int doc = scorer.nextDoc();+ if (((long) docIDStart) + doc >= limit)+ break;+ reader.deleteDocument(doc);+ any = true;+ }}}searcher.close();diff --git a/src/java/org/apache/lucene/search/BooleanQuery.java b/src/java/org/apache/lucene/search/BooleanQuery.javaindex 4484602eaf2..b156a7bf62e 100644--- a/src/java/org/apache/lucene/search/BooleanQuery.java+++ b/src/java/org/apache/lucene/search/BooleanQuery.java@@ -227,9 +227,12 @@ public class BooleanQuery extends Query {float sum = 0.0f;boolean fail = false;int shouldMatchCount = 0;- for (int i = 0 ; i < weights.size(); i++) {- BooleanClause c = (BooleanClause)clauses.get(i);- QueryWeight w = (QueryWeight)weights.get(i);+ for (Iterator wIter = weights.iterator(), cIter = clauses.iterator(); wIter.hasNext();) {+ QueryWeight w = (QueryWeight) wIter.next();+ BooleanClause c = (BooleanClause) cIter.next();+ if (w.scorer(reader, true, true) == null) {+ continue;+ }Explanation e = w.explain(reader, doc);if (!c.isProhibited()) maxCoord++;if (e.isMatch()) {@@ -244,7 +247,7 @@ public class BooleanQuery extends Query {sumExpl.addDetail(r);fail = true;}- if (c.getOccur().equals(Occur.SHOULD))+ if (c.getOccur() == Occur.SHOULD)shouldMatchCount++;} else if (c.isRequired()) {Explanation r = new Explanation(0.0f, "no match on required clause (" + c.getQuery().toString() + ")");@@ -312,6 +315,16 @@ public class BooleanQuery extends Query {return new BooleanScorer(similarity, minNrShouldMatch, optional, prohibited);}+ if (required.size() == 0 && optional.size() == 0) {+ // no required and optional clauses.+ return null;+ } else if (optional.size() < minNrShouldMatch) {+ // either >1 req scorer, or there are 0 req scorers and at least 1+ // optional scorer. Therefore if there are not enough optional scorers+ // no documents will be matched by the query+ return null;+ }+// Return a BooleanScorer2return new BooleanScorer2(similarity, minNrShouldMatch, required, prohibited, optional);}diff --git a/src/java/org/apache/lucene/search/BooleanScorer2.java b/src/java/org/apache/lucene/search/BooleanScorer2.javaindex fdadc688423..ce9f78ff37f 100644--- a/src/java/org/apache/lucene/search/BooleanScorer2.java+++ b/src/java/org/apache/lucene/search/BooleanScorer2.java@@ -218,36 +218,26 @@ class BooleanScorer2 extends Scorer {}private Scorer makeCountingSumScorerNoReq() throws IOException { // No required scorers- if (optionalScorers.size() == 0) {- return new NonMatchingScorer(); // no clauses or only prohibited clauses- } else { // No required scorers. At least one optional scorer.- // minNrShouldMatch optional scorers are required, but at least 1- int nrOptRequired = (minNrShouldMatch < 1) ? 1 : minNrShouldMatch;- if (optionalScorers.size() < nrOptRequired) {- return new NonMatchingScorer(); // fewer optional clauses than minimum (at least 1) that should match- } else { // optionalScorers.size() >= nrOptRequired, no required scorers- Scorer requiredCountingSumScorer =- (optionalScorers.size() > nrOptRequired)- ? countingDisjunctionSumScorer(optionalScorers, nrOptRequired)- : // optionalScorers.size() == nrOptRequired (all optional scorers are required), no required scorers- (optionalScorers.size() == 1)- ? new SingleMatchScorer((Scorer) optionalScorers.get(0))- : countingConjunctionSumScorer(optionalScorers);- return addProhibitedScorers(requiredCountingSumScorer);- }- }+ // minNrShouldMatch optional scorers are required, but at least 1+ int nrOptRequired = (minNrShouldMatch < 1) ? 1 : minNrShouldMatch;+ Scorer requiredCountingSumScorer;+ if (optionalScorers.size() > nrOptRequired)+ requiredCountingSumScorer = countingDisjunctionSumScorer(optionalScorers, nrOptRequired);+ else if (optionalScorers.size() == 1)+ requiredCountingSumScorer = new SingleMatchScorer((Scorer) optionalScorers.get(0));+ else+ requiredCountingSumScorer = countingConjunctionSumScorer(optionalScorers);+ return addProhibitedScorers(requiredCountingSumScorer);}private Scorer makeCountingSumScorerSomeReq() throws IOException { // At least one required scorer.- if (optionalScorers.size() < minNrShouldMatch) {- return new NonMatchingScorer(); // fewer optional clauses than minimum that should match- } else if (optionalScorers.size() == minNrShouldMatch) { // all optional scorers also required.+ if (optionalScorers.size() == minNrShouldMatch) { // all optional scorers also required.ArrayList allReq = new ArrayList(requiredScorers);allReq.addAll(optionalScorers);return addProhibitedScorers(countingConjunctionSumScorer(allReq));} else { // optionalScorers.size() > minNrShouldMatch, and at least one required scorerScorer requiredCountingSumScorer =- (requiredScorers.size() == 1)+ requiredScorers.size() == 1? new SingleMatchScorer((Scorer) requiredScorers.get(0)): countingConjunctionSumScorer(requiredScorers);if (minNrShouldMatch > 0) { // use a required disjunction scorer over the optional scorers@@ -260,9 +250,10 @@ class BooleanScorer2 extends Scorer {} else { // minNrShouldMatch == 0return new ReqOptSumScorer(addProhibitedScorers(requiredCountingSumScorer),- ((optionalScorers.size() == 1)+ optionalScorers.size() == 1? new SingleMatchScorer((Scorer) optionalScorers.get(0))- : countingDisjunctionSumScorer(optionalScorers, 1))); // require 1 in combined, optional scorer.+ // require 1 in combined, optional scorer.+ : countingDisjunctionSumScorer(optionalScorers, 1));}}}diff --git a/src/java/org/apache/lucene/search/CachingWrapperFilter.java b/src/java/org/apache/lucene/search/CachingWrapperFilter.javaindex 7c3a2f2a49a..3b01371325b 100644--- a/src/java/org/apache/lucene/search/CachingWrapperFilter.java+++ b/src/java/org/apache/lucene/search/CachingWrapperFilter.java@@ -100,12 +100,13 @@ public class CachingWrapperFilter extends Filter {final DocIdSet docIdSet = docIdSetToCache(filter.getDocIdSet(reader), reader);- synchronized (cache) { // update cache- cache.put(reader, docIdSet);+ if (docIdSet != null) {+ synchronized (cache) { // update cache+ cache.put(reader, docIdSet);+ }}return docIdSet;-}public String toString() {diff --git a/src/java/org/apache/lucene/search/ConstantScoreQuery.java b/src/java/org/apache/lucene/search/ConstantScoreQuery.javaindex 01f5856b49a..25a36965641 100644--- a/src/java/org/apache/lucene/search/ConstantScoreQuery.java+++ b/src/java/org/apache/lucene/search/ConstantScoreQuery.java@@ -113,7 +113,17 @@ public class ConstantScoreQuery extends Query {public ConstantScorer(Similarity similarity, IndexReader reader, QueryWeight w) throws IOException {super(similarity);theScore = w.getValue();- docIdSetIterator = filter.getDocIdSet(reader).iterator();+ DocIdSet docIdSet = filter.getDocIdSet(reader);+ if (docIdSet == null) {+ docIdSetIterator = EmptyDocIdSetIterator.getInstance();+ } else {+ DocIdSetIterator iter = docIdSet.iterator();+ if (iter == null) {+ docIdSetIterator = EmptyDocIdSetIterator.getInstance();+ } else {+ docIdSetIterator = iter;+ }+ }}/** @deprecated use {@link #nextDoc()} instead. */diff --git a/src/java/org/apache/lucene/search/DocIdSet.java b/src/java/org/apache/lucene/search/DocIdSet.javaindex 9272018475b..c8ffa14739f 100644--- a/src/java/org/apache/lucene/search/DocIdSet.java+++ b/src/java/org/apache/lucene/search/DocIdSet.java@@ -30,6 +30,8 @@ public abstract class DocIdSet {* implemented using a {@link SortedVIntList}). */public static final DocIdSet EMPTY_DOCIDSET = new SortedVIntList(new int[0]);- /** Provides a {@link DocIdSetIterator} to access the set. */+ /** Provides a {@link DocIdSetIterator} to access the set.+ * This may (but is not required to) return null if there+ * are no docs that match. */public abstract DocIdSetIterator iterator() throws IOException;}diff --git a/src/java/org/apache/lucene/search/Filter.java b/src/java/org/apache/lucene/search/Filter.javaindex 4e0df738823..523e18be7d0 100644--- a/src/java/org/apache/lucene/search/Filter.java+++ b/src/java/org/apache/lucene/search/Filter.java@@ -39,10 +39,12 @@ public abstract class Filter implements java.io.Serializable {public BitSet bits(IndexReader reader) throws IOException {throw new UnsupportedOperationException();}-+/**- * @return a DocIdSet that provides the documents which should be- * permitted or prohibited in search results.+ * @return a DocIdSet that provides the documents which should be permitted or+ * prohibited in search results. <b>NOTE:</b> null can be returned if+ * no documents will be accepted by this Filter.+ ** @see DocIdBitSet*/public DocIdSet getDocIdSet(IndexReader reader) throws IOException {diff --git a/src/java/org/apache/lucene/search/FilteredQuery.java b/src/java/org/apache/lucene/search/FilteredQuery.javaindex 68869b1b0da..81339c9bbb0 100644--- a/src/java/org/apache/lucene/search/FilteredQuery.java+++ b/src/java/org/apache/lucene/search/FilteredQuery.java@@ -82,7 +82,11 @@ extends Query {inner.addDetail(preBoost);}Filter f = FilteredQuery.this.filter;- DocIdSetIterator docIdSetIterator = f.getDocIdSet(ir).iterator();+ DocIdSet docIdSet = f.getDocIdSet(ir);+ DocIdSetIterator docIdSetIterator = docIdSet == null ? EmptyDocIdSetIterator.getInstance() : docIdSet.iterator();+ if (docIdSetIterator == null) {+ docIdSetIterator = EmptyDocIdSetIterator.getInstance();+ }if (docIdSetIterator.advance(i) == i) {return inner;} else {@@ -100,7 +104,17 @@ extends Query {public Scorer scorer(IndexReader indexReader, boolean scoreDocsInOrder, boolean topScorer)throws IOException {final Scorer scorer = weight.scorer(indexReader, scoreDocsInOrder, false);- final DocIdSetIterator docIdSetIterator = filter.getDocIdSet(indexReader).iterator();+ if (scorer == null) {+ return null;+ }+ DocIdSet docIdSet = filter.getDocIdSet(indexReader);+ if (docIdSet == null) {+ return null;+ }+ final DocIdSetIterator docIdSetIterator = docIdSet.iterator();+ if (docIdSetIterator == null) {+ return null;+ }return new Scorer(similarity) {diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.javaindex 203f524fb24..482760a4a1e 100644--- a/src/java/org/apache/lucene/search/IndexSearcher.java+++ b/src/java/org/apache/lucene/search/IndexSearcher.java@@ -267,8 +267,17 @@ public class IndexSearcher extends Searcher {assert docID == -1 || docID == DocIdSetIterator.NO_MORE_DOCS;// CHECKME: use ConjunctionScorer here?- DocIdSetIterator filterIter = filter.getDocIdSet(reader).iterator();+ DocIdSet filterDocIdSet = filter.getDocIdSet(reader);+ if (filterDocIdSet == null) {+ // this means the filter does not accept any documents.+ return;+ }+ DocIdSetIterator filterIter = filterDocIdSet.iterator();+ if (filterIter == null) {+ // this means the filter does not accept any documents.+ return;+ }int filterDoc = filterIter.nextDoc();int scorerDoc = scorer.advance(filterDoc);diff --git a/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/src/java/org/apache/lucene/search/MultiPhraseQuery.javaindex 01683072cf4..5d523ce2247 100644--- a/src/java/org/apache/lucene/search/MultiPhraseQuery.java+++ b/src/java/org/apache/lucene/search/MultiPhraseQuery.java@@ -217,7 +217,11 @@ public class MultiPhraseQuery extends Query {fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc+"), product of:");- Explanation tfExpl = scorer(reader, true, false).explain(doc);+ Scorer scorer = scorer(reader, true, false);+ if (scorer == null) {+ return new Explanation(0.0f, "no matching docs");+ }+ Explanation tfExpl = scorer.explain(doc);fieldExpl.addDetail(tfExpl);fieldExpl.addDetail(idfExpl);diff --git a/src/java/org/apache/lucene/search/NonMatchingScorer.java b/src/java/org/apache/lucene/search/NonMatchingScorer.javaindex 0260b036516..e69de29bb2d 100644--- a/src/java/org/apache/lucene/search/NonMatchingScorer.java+++ b/src/java/org/apache/lucene/search/NonMatchingScorer.java@@ -1,50 +0,0 @@-package org.apache.lucene.search;--/**- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--import java.io.IOException;--/** A scorer that matches no document at all. */-class NonMatchingScorer extends Scorer {- public NonMatchingScorer() { super(null); } // no similarity used-- /** @deprecated use {@link #docID()} instead. */- public int doc() { throw new UnsupportedOperationException(); }-- public int docID() { return NO_MORE_DOCS; }-- /** @deprecated use {@link #nextDoc()} instead. */- public boolean next() throws IOException { return false; }-- public int nextDoc() throws IOException { return NO_MORE_DOCS; }-- public float score() { throw new UnsupportedOperationException(); }-- /** @deprecated use {@link #advance(int)} instead. */- public boolean skipTo(int target) { return false; }-- public int advance(int target) { return NO_MORE_DOCS; }-- public Explanation explain(int doc) {- Explanation e = new Explanation();- e.setDescription("No document matches.");- return e;- }-}--diff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.javaindex ef76aa2f311..7a0a17457bd 100644--- a/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/src/java/org/apache/lucene/search/PhraseQuery.java@@ -209,7 +209,11 @@ public class PhraseQuery extends Query {fieldExpl.setDescription("fieldWeight("+field+":"+query+" in "+doc+"), product of:");- Explanation tfExpl = scorer(reader, true, false).explain(doc);+ Scorer scorer = scorer(reader, true, false);+ if (scorer == null) {+ return new Explanation(0.0f, "no matching docs");+ }+ Explanation tfExpl = scorer.explain(doc);fieldExpl.addDetail(tfExpl);fieldExpl.addDetail(idfExpl);diff --git a/src/java/org/apache/lucene/search/QueryWeight.java b/src/java/org/apache/lucene/search/QueryWeight.javaindex 69c809e2324..29f55cafe09 100644--- a/src/java/org/apache/lucene/search/QueryWeight.java+++ b/src/java/org/apache/lucene/search/QueryWeight.java@@ -75,8 +75,11 @@ public abstract class QueryWeight implements Weight, Serializable {* <p>* <b>NOTE:</b> even if <code>scoreDocsInOrder</code> is false, it is* recommended to check whether the returned <code>Scorer</code> indeed scores- * documents out of order (i.e., call {@link #scoresDocsOutOfOrder()}), as some- * <code>Scorer</code> implementations will always return documents in-order.+ * documents out of order (i.e., call {@link #scoresDocsOutOfOrder()}), as+ * some <code>Scorer</code> implementations will always return documents+ * in-order.<br>+ * <b>NOTE:</b> null can be returned if no documents will be scored by this+ * query.** @param reader* the {@link IndexReader} for which to return the {@link Scorer}.diff --git a/src/java/org/apache/lucene/search/function/CustomScoreQuery.java b/src/java/org/apache/lucene/search/function/CustomScoreQuery.javaindex a7950d91721..1e5d5476d51 100755--- a/src/java/org/apache/lucene/search/function/CustomScoreQuery.java+++ b/src/java/org/apache/lucene/search/function/CustomScoreQuery.java@@ -331,6 +331,9 @@ public class CustomScoreQuery extends Query {// false for "topScorer" because we will not invoke// score(Collector) on these scorers:Scorer subQueryScorer = subQueryWeight.scorer(reader, true, false);+ if (subQueryScorer == null) {+ return null;+ }Scorer[] valSrcScorers = new Scorer[valSrcWeights.length];for(int i = 0; i < valSrcScorers.length; i++) {valSrcScorers[i] = valSrcWeights[i].scorer(reader, true, false);@@ -339,7 +342,8 @@ public class CustomScoreQuery extends Query {}public Explanation explain(IndexReader reader, int doc) throws IOException {- return scorer(reader, true, false).explain(doc);+ Scorer scorer = scorer(reader, true, false);+ return scorer == null ? new Explanation(0.0f, "no matching docs") : scorer.explain(doc);}public boolean scoresDocsOutOfOrder() {diff --git a/src/test/org/apache/lucene/search/QueryUtils.java b/src/test/org/apache/lucene/search/QueryUtils.javaindex 0e8fdf91d4d..a0d9683c0e9 100644--- a/src/test/org/apache/lucene/search/QueryUtils.java+++ b/src/test/org/apache/lucene/search/QueryUtils.java@@ -152,7 +152,10 @@ public class QueryUtils {final QueryWeight w = q.queryWeight(s);final Scorer scorer = w.scorer(s.getIndexReader(), true, false);-+ if (scorer == null) {+ continue;+ }+// FUTURE: ensure scorer.doc()==-1final int[] sdoc = new int[] {-1};@@ -253,8 +256,10 @@ public class QueryUtils {});QueryWeight w = q.queryWeight(s);Scorer scorer = w.scorer(s.getIndexReader(), true, false);- boolean more = scorer.advance(lastDoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS;- if (more)- Assert.assertFalse("query's last doc was "+lastDoc[0]+" but skipTo("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);+ if (scorer != null) {+ boolean more = scorer.advance(lastDoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS;+ if (more)+ Assert.assertFalse("query's last doc was "+lastDoc[0]+" but skipTo("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);+ }}}diff --git a/src/test/org/apache/lucene/search/TestDocIdSet.java b/src/test/org/apache/lucene/search/TestDocIdSet.javaindex 5767cdc7f10..bc6945d761e 100644--- a/src/test/org/apache/lucene/search/TestDocIdSet.java+++ b/src/test/org/apache/lucene/search/TestDocIdSet.java@@ -22,6 +22,18 @@ import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;+import junit.framework.Assert;++import org.apache.lucene.document.Document;+import org.apache.lucene.document.Field;+import org.apache.lucene.document.Field.Index;+import org.apache.lucene.document.Field.Store;+import org.apache.lucene.analysis.WhitespaceAnalyzer;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.IndexWriter;+import org.apache.lucene.index.IndexWriter.MaxFieldLength;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util._TestUtil;@@ -102,4 +114,30 @@ public class TestDocIdSet extends LuceneTestCase {fail();}}++ public void testNullDocIdSet() throws Exception {+ // Tests that if a Filter produces a null DocIdSet, which is given to+ // IndexSearcher, everything works fine. This came up in LUCENE-1754.+ Directory dir = new RAMDirectory();+ IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), MaxFieldLength.UNLIMITED);+ Document doc = new Document();+ doc.add(new Field("c", "val", Store.NO, Index.NOT_ANALYZED_NO_NORMS));+ writer.addDocument(doc);+ writer.close();++ // First verify the document is searchable.+ IndexSearcher searcher = new IndexSearcher(dir, true);+ Assert.assertEquals(1, searcher.search(new MatchAllDocsQuery(), 10).totalHits);++ // Now search w/ a Filter which returns a null DocIdSet+ Filter f = new Filter() {+ public DocIdSet getDocIdSet(IndexReader reader) throws IOException {+ return null;+ }+ };++ Assert.assertEquals(0, searcher.search(new MatchAllDocsQuery(), f, 10).totalHits);+ searcher.close();+ }+}