Skip to content

Apache Lucene open-source search software

LUCENE-6449: fix NullPointerException when one segment is missing the highlighted field in its postings

Michael McCandlessfef21df361e4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 214f6f8ea21..e8b71e1ecdf 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -130,6 +130,10 @@ Bug Fixes
of replacing it with a synonym, when expand=true and there is no
explicit mapping (Ian Ribas, Robert Muir, Mike McCandless)
+* LUCENE-6449: Don't throw NullPointerException if some segments are
+ missing the field being highlighted, in PostingsHighlighter (Roman
+ Khmelichek via Mike McCandless)
+
API Changes
* LUCENE-6377: SearcherFactory#newSearcher now accepts the previous reader
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
index 2415d71ef6c..f6fb93cb187 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java
@@ -505,12 +505,16 @@ public class PostingsHighlighter {
// if the segment has changed, we must initialize new enums.
if (leaf != lastLeaf) {
Terms t = r.terms(field);
- if (!t.hasOffsets()) {
- // no offsets available
- throw new IllegalArgumentException("field '" + field + "' was indexed without offsets, cannot highlight");
+ if (t != null) {
+ if (!t.hasOffsets()) {
+ // no offsets available
+ throw new IllegalArgumentException("field '" + field + "' was indexed without offsets, cannot highlight");
+ }
+ termsEnum = t.iterator();
+ postings = new PostingsEnum[terms.length];
+ } else {
+ termsEnum = null;
}
- termsEnum = t.iterator();
- postings = new PostingsEnum[terms.length];
}
if (termsEnum == null) {
continue; // no terms for this field, nothing to do
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
index 8475679883c..fb1051786a2 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestPostingsHighlighter.java
@@ -31,6 +31,7 @@ import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexOptions;
@@ -42,6 +43,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
@@ -1118,4 +1120,39 @@ public class TestPostingsHighlighter extends LuceneTestCase {
ir.close();
dir.close();
}
+
+ public void testFieldSometimesMissingFromSegment() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
+ iwc.setMergePolicy(newLogMergePolicy());
+ RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);
+
+ FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
+ offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
+ Field body = new Field("body", "foo", offsetsType);
+ Document doc = new Document();
+ doc.add(body);
+ iw.addDocument(doc);
+
+ // Make a 2nd segment where body is only stored:
+ iw.commit();
+ doc = new Document();
+ doc.add(new StoredField("body", "foo"));
+ iw.addDocument(doc);
+
+ IndexReader ir = iw.getReader();
+ iw.close();
+
+ IndexSearcher searcher = newSearcher(ir);
+ PostingsHighlighter highlighter = new PostingsHighlighter();
+ Query query = new MatchAllDocsQuery();
+ TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
+ assertEquals(2, topDocs.totalHits);
+ String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
+ assertEquals(2, snippets.length);
+ assertEquals("foo", snippets[0]);
+ assertNull(snippets[1]);
+ ir.close();
+ dir.close();
+ }
}