Apache Lucene open-source search software
LUCENE-5032: fix IOOBE in PostingsHighlighter when multi-valued field exceeds maxLength
Michael McCandless94472d324ad2
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex d91982952bc..3c3ff7bf9a7 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -126,6 +126,10 @@ Bug Fixes* LUCENE-5028: Remove pointless and confusing doShare option in FST'sPositiveIntOutputs (Han Jiang via Mike McCandless)+* LUCENE-5032: Fix IndexOutOfBoundsExc in PostingsHighlighter when+ multi-valued fields exceed maxLength (Tomás Fernández Löbbe+ via Mike McCandless)+Optimizations* LUCENE-4936: Improve numeric doc values compression in case all values sharediff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.javaindex 850c77a437d..b52fcbe1006 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@@ -669,7 +669,7 @@ public class PostingsHighlighter {public void stringField(FieldInfo fieldInfo, String value) throws IOException {assert currentField >= 0;StringBuilder builder = builders[currentField];- if (builder.length() > 0) {+ if (builder.length() > 0 && builder.length() < maxLength) {builder.append(' '); // for the offset gap, TODO: make this configurable}if (builder.length() + value.length() > maxLength) {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.javaindex 699950e870b..8308bed309f 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@@ -123,6 +123,43 @@ public class TestPostingsHighlighter extends LuceneTestCase {dir.close();}+ // simple test with multiple values that make a result longer than maxLength.+ public void testMaxLengthWithMultivalue() throws Exception {+ Directory dir = newDirectory();+ // use simpleanalyzer for more natural tokenization (else "test." is a token)+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));+ 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);+ Document doc = new Document();++ for(int i = 0; i < 3 ; i++) {+ Field body = new Field("body", "", offsetsType);+ body.setStringValue("This is a multivalued field");+ doc.add(body);+ }++ iw.addDocument(doc);++ IndexReader ir = iw.getReader();+ iw.close();++ IndexSearcher searcher = newSearcher(ir);+ PostingsHighlighter highlighter = new PostingsHighlighter(40);+ Query query = new TermQuery(new Term("body", "field"));+ TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER);+ assertEquals(1, topDocs.totalHits);+ String snippets[] = highlighter.highlight("body", query, searcher, topDocs);+ assertEquals(1, snippets.length);+ assertTrue("Snippet should have maximum 40 characters plus the pre and post tags",+ snippets[0].length() == (40 + "<b></b>".length()));++ ir.close();+ dir.close();+ }+public void testMultipleFields() throws Exception {Directory dir = newDirectory();IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));