Skip to content

Apache Lucene open-source search software

LUCENE-1321: Highlight fragment does not extend to maxDocCharsToAnalyze

Mark Robert Miller943bf37e66b3
diff --git a/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java b/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
index 8d13af2b57a..7d615d4b9b8 100644
--- a/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
+++ b/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
@@ -285,7 +285,7 @@ public class Highlighter
(lastEndOffset < text.length())
&&
// and that text is not too large...
- (text.length()< maxDocCharsToAnalyze)
+ (text.length()<= maxDocCharsToAnalyze)
)
{
//append it to the last fragment
diff --git a/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java b/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
index e8d0323381f..cc0d402b5a3 100644
--- a/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
+++ b/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
@@ -876,6 +876,27 @@ public class HighlighterTest extends TestCase implements Formatter {
helper.start();
}
+
+ public void testMaxSizeEndHighlight() throws Exception {
+ TestHighlightRunner helper = new TestHighlightRunner() {
+ public void run() throws Exception {
+ String stopWords[] = { "in", "it" };
+ TermQuery query = new TermQuery(new Term("text", "searchterm"));
+
+ String text = "this is a text with searchterm in it";
+ SimpleHTMLFormatter fm = new SimpleHTMLFormatter();
+ Highlighter hg = getHighlighter(query, "text", new StandardAnalyzer(
+ stopWords).tokenStream("text", new StringReader(text)), fm);
+ hg.setTextFragmenter(new NullFragmenter());
+ hg.setMaxDocCharsToAnalyze(36);
+ String match = hg.getBestFragment(new StandardAnalyzer(stopWords), "text", text);
+ assertTrue(
+ "Matched text should contain remainder of text after highlighted query ",
+ match.endsWith("in it"));
+ }
+ };
+ helper.start();
+ }
public void testUnRewrittenQuery() throws Exception {
TestHighlightRunner helper = new TestHighlightRunner() {