You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2024/10/04 01:22:23 UTC

(lucene) branch branch_10x updated: Fix UnifiedHighlighter DefaultPassageFormatter for non-offset order passages (#13832)

This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new a29d12f417c Fix UnifiedHighlighter DefaultPassageFormatter for non-offset order passages (#13832)
a29d12f417c is described below

commit a29d12f417c5d41cb6c5f882581907a87afcb7b9
Author: 정승한(Seunghan Jung)/Search Platform <aj...@gmail.com>
AuthorDate: Thu Oct 3 15:00:09 2024 +0900

    Fix UnifiedHighlighter DefaultPassageFormatter for non-offset order passages (#13832)
    
    The ellipsis should have been inserted in more scenarios.
    
    (cherry picked from commit e3e3328a550a8f8760cc3aa213ebf7e7140399fb)
---
 lucene/CHANGES.txt                                 |  3 ++-
 .../search/uhighlight/DefaultPassageFormatter.java |  2 +-
 .../uhighlight/TestDefaultPassageFormatter.java    | 26 ++++++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6eb82b14c3b..5c22049f7aa 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -23,7 +23,8 @@ Optimizations
 
 Bug Fixes
 ---------------------
-(No changes)
+* GITHUB#13832: Fixed an issue where the DefaultPassageFormatter.format method did not format passages as intended
+  when they were not sorted by startOffset. (Seunghan Jung)
 
 Other
 ---------------------
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 27281a91be7..4cd2b07fc1c 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
@@ -64,7 +64,7 @@ public class DefaultPassageFormatter extends PassageFormatter {
     int pos = 0;
     for (Passage passage : passages) {
       // don't add ellipsis if its the first one, or if its connected.
-      if (passage.getStartOffset() > pos && pos > 0) {
+      if (!sb.isEmpty() && passage.getStartOffset() != pos) {
         sb.append(ellipsis);
       }
       pos = passage.getStartOffset();
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestDefaultPassageFormatter.java b/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestDefaultPassageFormatter.java
index b59fea47453..617077c987c 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestDefaultPassageFormatter.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/uhighlight/TestDefaultPassageFormatter.java
@@ -75,4 +75,30 @@ public class TestDefaultPassageFormatter extends LuceneTestCase {
         "<b>Yin yang loooooooooong</b>, <b>yin</b> gap <b>yang</b> yong",
         formatter.format(passages, content));
   }
+
+  public void testReversedStartOffsetOrder() {
+    String content =
+        "When indexing data in Solr, each document is composed of various fields. "
+            + "A document essentially represents a single record, and each document typically contains a unique ID field.";
+
+    Passage[] passages = new Passage[2];
+    passages[0] = new Passage();
+    passages[0].setStartOffset(73);
+    passages[0].setEndOffset(179);
+    passages[0].setScore(1.8846991f);
+    passages[0].addMatch(75, 83, new BytesRef("document"), 1);
+    passages[0].addMatch(133, 141, new BytesRef("document"), 1);
+
+    passages[1] = new Passage();
+    passages[1].setStartOffset(0);
+    passages[1].setEndOffset(73);
+    passages[1].setScore(1.5923802f);
+    passages[1].addMatch(33, 41, new BytesRef("document"), 1);
+
+    DefaultPassageFormatter formatter = new DefaultPassageFormatter("<b>", "</b>", "\n", false);
+    assertEquals(
+        "A <b>document</b> essentially represents a single record, and each <b>document</b> typically contains a unique ID field.\n"
+            + "When indexing data in Solr, each <b>document</b> is composed of various fields. ",
+        formatter.format(passages, content));
+  }
 }