Skip to content

Apache Lucene open-source search software

LUCENE-8446: DefaultPassageFormatter: merge overlapping matches

David Smiley8d3f59a47f2a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1b0926043ca..5ae0111d0e1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -243,6 +243,9 @@ Improvements
* LUCENE-8414: Make segmentInfos private in IndexWriter (Simon Willnauer, Nhat Nguyen)
+* LUCENE-8446: The UnifiedHighlighter's DefaultPassageFormatter now treats overlapping matches in
+ the passage as merged (as if one larger match). (David Smiley)
+
Other:
* LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11's
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/DefaultPassageFormatter.java b/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/DefaultPassageFormatter.java
index 62d58df70f0..8d2a424d100 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/DefaultPassageFormatter.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/DefaultPassageFormatter.java
@@ -70,17 +70,24 @@ public class DefaultPassageFormatter extends PassageFormatter {
pos = passage.getStartOffset();
for (int i = 0; i < passage.getNumMatches(); i++) {
int start = passage.getMatchStarts()[i];
+ assert start >= pos && start < passage.getEndOffset();
+ //append content before this start
+ append(sb, content, pos, start);
+
int end = passage.getMatchEnds()[i];
- // its possible to have overlapping terms
- if (start > pos) {
- append(sb, content, pos, start);
- }
- if (end > pos) {
- sb.append(preTag);
- append(sb, content, Math.max(pos, start), end);
- sb.append(postTag);
- pos = end;
+ assert end > start;
+ // its possible to have overlapping terms.
+ // Look ahead to expand 'end' past all overlapping:
+ while (i + 1 < passage.getNumMatches() && passage.getMatchStarts()[i+1] < end) {
+ end = passage.getMatchEnds()[++i];
}
+ end = Math.min(end, passage.getEndOffset()); // in case match straddles past passage
+
+ sb.append(preTag);
+ append(sb, content, start, end);
+ sb.append(postTag);
+
+ pos = end;
}
// its possible a "term" from the analyzer could span a sentence boundary.
append(sb, content, pos, Math.max(pos, passage.getEndOffset()));
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/OffsetsEnum.java b/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/OffsetsEnum.java
index 55f3c37d493..bdabcc1f59b 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/OffsetsEnum.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/OffsetsEnum.java
@@ -46,6 +46,10 @@ public abstract class OffsetsEnum implements Comparable<OffsetsEnum>, Closeable
if (cmp != 0) {
return cmp; // vast majority of the time we return here.
}
+ cmp = Integer.compare(endOffset(), other.endOffset());
+ if (cmp != 0) {
+ return cmp;
+ }
final BytesRef thisTerm = this.getTerm();
final BytesRef otherTerm = other.getTerm();
if (thisTerm == null || otherTerm == null) {