diff --git a/contrib/CHANGES.txt b/contrib/CHANGES.txt index 6f6d4dcc415..ff8693e4eff 100644 --- a/contrib/CHANGES.txt +++ b/contrib/CHANGES.txt @@ -26,6 +26,9 @@ Bug fixes Iterator#hasNext method on exhausted streams. (Patrick Jungermann via Karl Wettin) + * LUCENE-1953: FastVectorHighlighter: small fragCharSize can cause + StringIndexOutOfBoundsException. (Koji Sekiguchi) + New features * LUCENE-1924: Added BalancedSegmentMergePolicy to contrib/misc, diff --git a/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java b/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java index d45ddb10857..089b42e739d 100644 --- a/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java +++ b/contrib/fast-vector-highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java @@ -59,6 +59,8 @@ public class SimpleFragListBuilder implements FragListBuilder { int st = phraseInfo.getStartOffset() - MARGIN < startOffset ? startOffset : phraseInfo.getStartOffset() - MARGIN; int en = st + fragCharSize; + if( phraseInfo.getEndOffset() > en ) + en = phraseInfo.getEndOffset(); startOffset = en; while( true ){ diff --git a/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java b/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java index d5951467506..996dfed2e01 100644 --- a/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java +++ b/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java @@ -37,6 +37,21 @@ public class SimpleFragListBuilderTest extends AbstractTestCase { } } + public void testSmallerFragSizeThanTermQuery() throws Exception { + SimpleFragListBuilder sflb = new SimpleFragListBuilder(); + FieldFragList ffl = sflb.createFieldFragList( fpl( "abcdefghijklmnopqrs", "abcdefghijklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE ); + assertEquals( 1, ffl.fragInfos.size() ); + assertEquals( "subInfos=(abcdefghijklmnopqrs((0,19)))/1.0(0,19)", ffl.fragInfos.get( 0 ).toString() ); + } + + public void testSmallerFragSizeThanPhraseQuery() throws Exception { + SimpleFragListBuilder sflb = new SimpleFragListBuilder(); + FieldFragList ffl = sflb.createFieldFragList( fpl( "\"abcdefgh jklmnopqrs\"", "abcdefgh jklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE ); + assertEquals( 1, ffl.fragInfos.size() ); + System.out.println( ffl.fragInfos.get( 0 ).toString() ); + assertEquals( "subInfos=(abcdefghjklmnopqrs((0,21)))/1.0(0,21)", ffl.fragInfos.get( 0 ).toString() ); + } + public void test1TermIndex() throws Exception { SimpleFragListBuilder sflb = new SimpleFragListBuilder(); FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a" ), 100 );