From 0f6c01a716bf550b88beabb6e281f8a5abf2f10c Mon Sep 17 00:00:00 2001 From: Ioana Tagirta Date: Thu, 11 Jul 2024 15:35:31 +0200 Subject: [PATCH] Fix issue with returning incomplete fragment for plain highlighter. (#110707) * Fix issue with noMatchSize for plain highlighter * Update docs/changelog/110707.yaml --- docs/changelog/110707.yaml | 5 +++++ .../fetch/subphase/highlight/HighlighterSearchIT.java | 9 +++++++++ .../fetch/subphase/highlight/PlainHighlighter.java | 3 +++ 3 files changed, 17 insertions(+) create mode 100644 docs/changelog/110707.yaml diff --git a/docs/changelog/110707.yaml b/docs/changelog/110707.yaml new file mode 100644 index 0000000000000..e13688c73c743 --- /dev/null +++ b/docs/changelog/110707.yaml @@ -0,0 +1,5 @@ +pr: 110707 +summary: Fix issue with returning incomplete fragment for plain highlighter +area: Highlighting +type: bug +issues: [] diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java index 0a6fceea9a3f1..d9d6979ffd710 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java @@ -2177,6 +2177,15 @@ public void testHighlightNoMatchSize() throws IOException { field.highlighterType("unified"); assertNotHighlighted(prepareSearch("test").highlighter(new HighlightBuilder().field(field)), 0, "text"); + + // Check when the requested fragment size equals the size of the string + var anotherText = "I am unusual and don't end with your regular )token)"; + indexDoc("test", "1", "text", anotherText); + refresh(); + for (String type : new String[] { "plain", "unified", "fvh" }) { + field.highlighterType(type).noMatchSize(anotherText.length()).numOfFragments(0); + assertHighlight(prepareSearch("test").highlighter(new HighlightBuilder().field(field)), 0, "text", 0, 1, equalTo(anotherText)); + } } public void testHighlightNoMatchSizeWithMultivaluedFields() throws IOException { diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java index e7fa0e67cb453..3d180dd094b18 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java @@ -218,6 +218,9 @@ private static int findGoodEndForNoHighlightExcerpt(int noMatchSize, Analyzer an // Can't split on term boundaries without offsets return -1; } + if (contents.length() <= noMatchSize) { + return contents.length(); + } int end = -1; tokenStream.reset(); while (tokenStream.incrementToken()) {