Skip to content

Commit 01b1770

Browse files
ioanatiaalbertzaharovits
authored andcommitted
Fix issue with returning incomplete fragment for plain highlighter. (#110707)
* Fix issue with noMatchSize for plain highlighter * Update docs/changelog/110707.yaml
1 parent 8a61676 commit 01b1770

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

docs/changelog/110707.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 110707
2+
summary: Fix issue with returning incomplete fragment for plain highlighter
3+
area: Highlighting
4+
type: bug
5+
issues: []

server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,15 @@ public void testHighlightNoMatchSize() throws IOException {
21772177

21782178
field.highlighterType("unified");
21792179
assertNotHighlighted(prepareSearch("test").highlighter(new HighlightBuilder().field(field)), 0, "text");
2180+
2181+
// Check when the requested fragment size equals the size of the string
2182+
var anotherText = "I am unusual and don't end with your regular )token)";
2183+
indexDoc("test", "1", "text", anotherText);
2184+
refresh();
2185+
for (String type : new String[] { "plain", "unified", "fvh" }) {
2186+
field.highlighterType(type).noMatchSize(anotherText.length()).numOfFragments(0);
2187+
assertHighlight(prepareSearch("test").highlighter(new HighlightBuilder().field(field)), 0, "text", 0, 1, equalTo(anotherText));
2188+
}
21802189
}
21812190

21822191
public void testHighlightNoMatchSizeWithMultivaluedFields() throws IOException {

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ private static int findGoodEndForNoHighlightExcerpt(int noMatchSize, Analyzer an
218218
// Can't split on term boundaries without offsets
219219
return -1;
220220
}
221+
if (contents.length() <= noMatchSize) {
222+
return contents.length();
223+
}
221224
int end = -1;
222225
tokenStream.reset();
223226
while (tokenStream.incrementToken()) {

0 commit comments

Comments
 (0)