From bc84eb4df25f76b328cc992c7b20d9817bf97b6f Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Mon, 17 Oct 2011 12:22:19 +0000 Subject: [PATCH] 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 --- .../apache/lucene/queries/TermsFilter.java | 3 ++ .../lucene/queries/TermsFilterTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/modules/queries/src/java/org/apache/lucene/queries/TermsFilter.java b/modules/queries/src/java/org/apache/lucene/queries/TermsFilter.java index cb27f23e032..b950c8b602c 100644 --- a/modules/queries/src/java/org/apache/lucene/queries/TermsFilter.java +++ b/modules/queries/src/java/org/apache/lucene/queries/TermsFilter.java @@ -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(); } diff --git a/modules/queries/src/test/org/apache/lucene/queries/TermsFilterTest.java b/modules/queries/src/test/org/apache/lucene/queries/TermsFilterTest.java index 69e7d2ab20c..4873d5f8d80 100644 --- a/modules/queries/src/test/org/apache/lucene/queries/TermsFilterTest.java +++ b/modules/queries/src/test/org/apache/lucene/queries/TermsFilterTest.java @@ -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(); + } + }