LUCENE-1404: fixed NPE in NearSpansUnordered.isPayloadAvailable

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@698487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-09-24 09:45:25 +00:00
parent 4ca262ce38
commit 0fa718244c
2 changed files with 73 additions and 9 deletions

View File

@ -225,16 +225,16 @@ class NearSpansUnordered implements PayloadSpans {
}
// TODO: Remove warning after API has been finalized
public boolean isPayloadAvailable() {
SpansCell pointer = min();
do {
if(pointer.isPayloadAvailable()) {
return true;
}
pointer = pointer.next;
} while(pointer.next != null);
public boolean isPayloadAvailable() {
SpansCell pointer = min();
while (pointer != null) {
if (pointer.isPayloadAvailable()) {
return true;
}
pointer = pointer.next;
}
return false;
return false;
}
public String toString() {

View File

@ -23,9 +23,14 @@ import org.apache.lucene.search.CheckHits;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MockRAMDirectory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
@ -389,4 +394,63 @@ public class TestSpans extends LuceneTestCase {
assertTrue("first doc score should be zero, " + score, score == 0.0f);
assertTrue("no second doc", ! spanScorer.next());
}
// LUCENE-1404
private void addDoc(IndexWriter writer, String id, String text) throws IOException {
final Document doc = new Document();
doc.add( new Field("id", id, Field.Store.YES, Field.Index.UN_TOKENIZED) );
doc.add( new Field("text", text, Field.Store.YES, Field.Index.TOKENIZED) );
writer.addDocument(doc);
}
// LUCENE-1404
private int hitCount(Searcher searcher, String word) throws Throwable {
return searcher.search(new TermQuery(new Term("text", word)), 10).totalHits;
}
// LUCENE-1404
private SpanQuery createSpan(String value) {
return new SpanTermQuery(new Term("text", value));
}
// LUCENE-1404
private SpanQuery createSpan(int slop, boolean ordered, SpanQuery[] clauses) {
return new SpanNearQuery(clauses, slop, ordered);
}
// LUCENE-1404
private SpanQuery createSpan(int slop, boolean ordered, String term1, String term2) {
return createSpan(slop, ordered, new SpanQuery[] {createSpan(term1), createSpan(term2)});
}
// LUCENE-1404
public void testNPESpanQuery() throws Throwable {
final Directory dir = new MockRAMDirectory();
final IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(new String[0]), IndexWriter.MaxFieldLength.LIMITED);
// Add documents
addDoc(writer, "1", "the big dogs went running to the market");
addDoc(writer, "2", "the cat chased the mouse, then the cat ate the mouse quickly");
// Commit
writer.close();
// Get searcher
final IndexReader reader = IndexReader.open(dir);
final IndexSearcher searcher = new IndexSearcher(reader);
// Control (make sure docs indexed)
assertEquals(2, hitCount(searcher, "the"));
assertEquals(1, hitCount(searcher, "cat"));
assertEquals(1, hitCount(searcher, "dogs"));
assertEquals(0, hitCount(searcher, "rabbit"));
// This throws exception (it shouldn't)
assertEquals(1,
searcher.search(createSpan(0, true,
new SpanQuery[] {createSpan(4, false, "chased", "cat"),
createSpan("ate")}), 10).totalHits);
reader.close();
dir.close();
}
}