Skip to content

Apache Lucene open-source search software

LUCENE-4290: also detect attempts to highlight fields w/o any prox

Robert Muir07c29ce1a44c
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java
index c505cc406b1..7d4613721ec 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java
@@ -337,11 +337,10 @@ public final class PostingsHighlighter {
if (!termsEnum.seekExact(terms[i].bytes(), true)) {
continue; // term not found
}
- DocsAndPositionsEnum de2 = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
- if (de2 == null) {
- continue;
- } else {
- de = postings[i] = de2;
+ de = postings[i] = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
+ if (de == null) {
+ // no positions available
+ throw new IllegalArgumentException("field '" + field + "' was indexed without offsets, cannot highlight");
}
pDoc = de.advance(doc);
} else {
diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java
index 62b68710a59..e0740fa23bb 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java
@@ -24,6 +24,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.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexReader;
@@ -232,15 +233,19 @@ public class TestPostingsHighlighter extends LuceneTestCase {
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);
- Field body = new Field("body", "", offsetsType);
+ FieldType positionsType = new FieldType(TextField.TYPE_STORED);
+ positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
+ Field body = new Field("body", "", positionsType);
+ Field title = new StringField("title", "", Field.Store.YES);
Document doc = new Document();
doc.add(body);
+ doc.add(title);
body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
+ title.setStringValue("test");
iw.addDocument(doc);
body.setStringValue("This test is another test. Not a good sentence. Test test test test.");
+ title.setStringValue("test");
iw.addDocument(doc);
IndexReader ir = iw.getReader();
@@ -257,6 +262,13 @@ public class TestPostingsHighlighter extends LuceneTestCase {
} catch (IllegalArgumentException iae) {
// expected
}
+
+ try {
+ highlighter.highlight("title", new TermQuery(new Term("title", "test")), searcher, topDocs, 2);
+ fail("did not hit expected exception");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
ir.close();
dir.close();
}