Index: contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java =================================================================== --- contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java (revision 984595) +++ contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java (working copy) @@ -105,7 +105,8 @@ "This piece of text refers to Kennedy at the beginning then has a longer piece of text that is very long in the middle and finally ends with another reference to Kennedy", "JFK has been shot", "John Kennedy has been shot", "This text has a typo in referring to Keneddy", - "wordx wordy wordz wordx wordy wordx worda wordb wordy wordc", "y z x y z a b", "lets is a the lets is a the lets is a the lets" }; + "wordx wordy wordz wordx wordy wordx worda wordb wordy wordc", "y z x y z a b", "lets is a the lets is a the lets is a the lets", + "Attribute instances are reused for all tokens of a document. Thus, a TokenStream/-Filter needs to update the appropriate Attribute(s) in incrementToken(). The consumer, commonly the Lucene indexer, consumes the data in the Attributes and then calls incrementToken() again until it retuns false, which indicates that the end of the stream was reached. This means that in each call of incrementToken() a TokenStream/-Filter can safely overwrite the data in the Attribute instances. "}; /** * Constructor for HighlightExtractorTest. @@ -463,6 +464,21 @@ if (VERBOSE) System.out.println("\t" + result); } + + doSearching("\"all tokens\""); + + maxNumFragmentsRequired = 2; + scorer = new QueryScorer(query, FIELD_NAME); + highlighter = new Highlighter(this, scorer); + for (int i = 0; i < hits.totalHits; i++) { + String text = searcher.doc(hits.scoreDocs[i].doc).get(FIELD_NAME); + TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, + new StringReader(text)); + highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, 20)); + String result = highlighter.getBestFragments(tokenStream, text, + maxNumFragmentsRequired, "..."); + if (VERBOSE) System.out.println("\t" + result); + } } // position sensitive query added after position insensitive query Index: contrib/highlighter/src/java/org/apache/lucene/search/highlight/SimpleSpanFragmenter.java =================================================================== --- contrib/highlighter/src/java/org/apache/lucene/search/highlight/SimpleSpanFragmenter.java (revision 984595) +++ contrib/highlighter/src/java/org/apache/lucene/search/highlight/SimpleSpanFragmenter.java (working copy) @@ -64,7 +64,7 @@ public boolean isNewFragment() { position += posIncAtt.getPositionIncrement(); - if (waitForPos == position) { + if (waitForPos <= position) { waitForPos = -1; } else if (waitForPos != -1) { return false;