LUCENE-5682: NPE in QueryRescorer when Scorer is null

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1595973 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joel Bernstein 2014-05-19 17:40:36 +00:00
parent f039b0f0fb
commit a8804b7867
3 changed files with 57 additions and 9 deletions

View File

@ -180,6 +180,9 @@ Bug fixes
rethrown with more useful information about the problem, omitting the
incorrect OutOfMemoryError. (Robert Muir, Uwe Schindler)
* LUCENE-5682: NPE in QueryRescorer when Scorer is null
(Joel Bernstein, Mike McCandless)
Test Framework
* LUCENE-5622: Fail tests if they print over the given limit of bytes to

View File

@ -85,18 +85,23 @@ public abstract class QueryRescorer extends Rescorer {
scorer = weight.scorer(readerContext, null);
}
int targetDoc = docID - docBase;
int actualDoc = scorer.docID();
if (actualDoc < targetDoc) {
actualDoc = scorer.advance(targetDoc);
}
if(scorer != null) {
int targetDoc = docID - docBase;
int actualDoc = scorer.docID();
if (actualDoc < targetDoc) {
actualDoc = scorer.advance(targetDoc);
}
if (actualDoc == targetDoc) {
// Query did match this doc:
hit.score = combine(hit.score, true, scorer.score());
if (actualDoc == targetDoc) {
// Query did match this doc:
hit.score = combine(hit.score, true, scorer.score());
} else {
// Query did not match this doc:
assert actualDoc > targetDoc;
hit.score = combine(hit.score, false, 0.0f);
}
} else {
// Query did not match this doc:
assert actualDoc > targetDoc;
hit.score = combine(hit.score, false, 0.0f);
}

View File

@ -107,6 +107,46 @@ public class TestQueryRescorer extends LuceneTestCase {
dir.close();
}
// Test LUCENE-5682
public void testNullScorerTermQuery() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(newStringField("id", "0", Field.Store.YES));
doc.add(newTextField("field", "wizard the the the the the oz", Field.Store.NO));
w.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "1", Field.Store.YES));
// 1 extra token, but wizard and oz are close;
doc.add(newTextField("field", "wizard oz the the the the the the", Field.Store.NO));
w.addDocument(doc);
IndexReader r = w.getReader();
w.shutdown();
// Do ordinary BooleanQuery:
BooleanQuery bq = new BooleanQuery();
bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
IndexSearcher searcher = getSearcher(r);
searcher.setSimilarity(new DefaultSimilarity());
TopDocs hits = searcher.search(bq, 10);
assertEquals(2, hits.totalHits);
assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
// Now, resort using TermQuery on term that does not exist.
TermQuery tq = new TermQuery(new Term("field", "gold"));
TopDocs hits2 = QueryRescorer.rescore(searcher, hits, tq, 2.0, 10);
// Just testing that null scorer is handled.
assertEquals(2, hits2.totalHits);
r.close();
dir.close();
}
public void testCustomCombine() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);