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

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1428161 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-01-03 04:00:32 +00:00
parent ce94072769
commit 07c29ce1a4
2 changed files with 19 additions and 8 deletions

View File

@ -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 {

View File

@ -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();
}