dsmiley commented on a change in pull request #1123: LUCENE-9093: Unified
highlighter with word separator never gives context to the left
URL: https://github.com/apache/lucene-solr/pull/1123#discussion_r362015879
##########
File path:
lucene/highlighter/src/java/org/apache/lucene/search/uhighlight/LengthGoalBreakIterator.java
##########
@@ -167,68 +146,70 @@ public int previous() {
@Override
public int following(int matchEndIndex) {
- final int targetIdx = (matchEndIndex + 1) + (int)(lengthGoal * (1.f -
fragmentAlignment));
+ return following(matchEndIndex, (matchEndIndex + 1) + (int)(lengthGoal *
(1.f - fragmentAlignment)));
+ }
+
+ private int following(int matchEndIndex, int targetIdx) {
if (targetIdx >= getText().getEndIndex()) {
- return baseIter.last();
+ if (currentCache == baseIter.last()) {
+ return DONE;
+ }
+ return currentCache = baseIter.last();
}
final int afterIdx = baseIter.following(targetIdx - 1);
if (afterIdx == DONE) {
- return baseIter.current();
+ currentCache = baseIter.last();
+ return DONE;
}
if (afterIdx == targetIdx) { // right on the money
- return afterIdx;
+ return currentCache = afterIdx;
}
if (isMinimumLength) { // thus never undershoot
- return afterIdx;
+ return currentCache = afterIdx;
}
// note: it is a shame that we invoke preceding() *one more time*; BI's
are sometimes expensive.
// Find closest break to target
final int beforeIdx = baseIter.preceding(targetIdx);
if (targetIdx - beforeIdx < afterIdx - targetIdx && beforeIdx >
matchEndIndex) {
- return beforeIdx;
+ return currentCache = beforeIdx;
}
- return afterIdx;
- }
-
- private int moveToBreak(int idx) { // precondition: idx is a known break
- // bi.isBoundary(idx) has side-effect of moving the position. Not obvious!
- //boolean moved = baseIter.isBoundary(idx); // probably not particularly
expensive
- //assert moved && current() == idx;
-
- // TODO fix: Would prefer to do "- 1" instead of "- 2" but
CustomSeparatorBreakIterator has a bug.
- int current = baseIter.following(idx - 2);
- assert current == idx : "following() didn't move us to the expected
index.";
- return idx;
+ // moveToBreak is necessary for when getSummaryPassagesNoHighlight calls
next and current() is used
+ return currentCache = afterIdx;
}
// called at start of new Passage given first word start offset
@Override
public int preceding(int matchStartIndex) {
final int targetIdx = (matchStartIndex - 1) - (int)(lengthGoal *
fragmentAlignment);
if (targetIdx <= 0) {
- return 0;
+ if (currentCache == baseIter.first()) {
+ return DONE;
+ }
+ return currentCache = baseIter.first();
}
final int beforeIdx = baseIter.preceding(targetIdx + 1);
if (beforeIdx == DONE) {
- return 0;
+ currentCache = baseIter.first();
+ return DONE;
}
if (beforeIdx == targetIdx) { // right on the money
- return beforeIdx;
+ return currentCache = beforeIdx;
}
if (isMinimumLength) { // thus never undershoot
- return beforeIdx;
+ return currentCache = beforeIdx;
}
// note: it is a shame that we invoke following() *one more time*; BI's
are sometimes expensive.
// Find closest break to target
final int afterIdx = baseIter.following(targetIdx - 1);
if (afterIdx - targetIdx < targetIdx - beforeIdx && afterIdx <
matchStartIndex) {
- return afterIdx;
+ return currentCache = afterIdx;
}
- return beforeIdx;
+ // moveToBreak is for consistency
Review comment:
I'll remove this comment on commit
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]