Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with returning incomplete fragment for plain highlighter. #110707

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/110707.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 110707
summary: Fix issue with returning incomplete fragment for plain highlighter
area: Highlighting
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to like 216, even before analyzing contents string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was asking myself the same, but I think the behaviour would not be consistent which could cause confusion - when no offsets are provided we would some return highlights for some document fields, but not for others and the reason why that happens might not be apparent.
If that's not actually a problem I am okay to change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about it, you are right. It is better to have the consistent behaviour. Let's keep your changes as they are.

return contents.length();
}
int end = -1;
tokenStream.reset();
while (tokenStream.incrementToken()) {
Expand Down