LUCENE-3522: fix NPE in TermsFilter when field is missing

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1185120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2011-10-17 12:22:19 +00:00
parent 4bfa64bcb9
commit bc84eb4df2
2 changed files with 40 additions and 0 deletions

View File

@ -72,6 +72,9 @@ public class TermsFilter extends Filter {
for (Term term : terms) {
if (!term.field().equals(lastField)) {
termsC = fields.terms(term.field());
if (termsC == null) {
return result;
}
termsEnum = termsC.iterator();
lastField = term.field();
}

View File

@ -23,6 +23,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.SlowMultiReaderWrapper;
import org.apache.lucene.index.Term;
@ -30,6 +31,7 @@ import org.apache.lucene.search.Filter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.ReaderUtil;
public class TermsFilterTest extends LuceneTestCase {
@ -86,4 +88,39 @@ public class TermsFilterTest extends LuceneTestCase {
reader.close();
rd.close();
}
public void testMissingField() throws Exception {
String fieldName = "field1";
Directory rd1 = newDirectory();
RandomIndexWriter w1 = new RandomIndexWriter(random, rd1);
Document doc = new Document();
doc.add(newField(fieldName, "content1", StringField.TYPE_STORED));
w1.addDocument(doc);
IndexReader reader1 = w1.getReader();
w1.close();
fieldName = "field2";
Directory rd2 = newDirectory();
RandomIndexWriter w2 = new RandomIndexWriter(random, rd2);
doc = new Document();
doc.add(newField(fieldName, "content2", StringField.TYPE_STORED));
w2.addDocument(doc);
IndexReader reader2 = w2.getReader();
w2.close();
TermsFilter tf = new TermsFilter();
tf.addTerm(new Term(fieldName, "content1"));
MultiReader multi = new MultiReader(reader1, reader2);
for (IndexReader.AtomicReaderContext context : ReaderUtil.leaves(multi.getTopReaderContext())) {
FixedBitSet bits = (FixedBitSet) tf.getDocIdSet(context);
assertTrue("Must be >= 0", bits.cardinality() >= 0);
}
multi.close();
reader1.close();
reader2.close();
rd1.close();
rd2.close();
}
}