LUCENE-1630: fix NPEs

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@788802 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-06-26 18:14:48 +00:00
parent 890d53acfb
commit 8a641eb4f7
4 changed files with 44 additions and 7 deletions

View File

@ -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()) {

View File

@ -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;
}
}

View File

@ -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

View File

@ -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();
}
}