Skip to content

Apache Lucene open-source search software

Minor matches highlighter optimizations (#15577)

Dawid Weiss548ae997d123
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java
index b045ba31374..d967fe2fb77 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/MatchRegionRetriever.java
@@ -43,6 +43,7 @@ import org.apache.lucene.search.FilterMatchesIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Matches;
import org.apache.lucene.search.MatchesIterator;
+import org.apache.lucene.search.MatchesUtils;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
@@ -397,6 +398,10 @@ public class MatchRegionRetriever {
ToIntFunction<String> maxHitsPerField,
Map<String, List<OffsetRange>> outputHighlights)
throws IOException {
+ if (queryAffectedHighlightedFields.isEmpty()) {
+ return;
+ }
+
Matches matches = weight.matches(leafReaderContext, contextDocId);
if (matches == null) {
return;
@@ -404,7 +409,7 @@ public class MatchRegionRetriever {
for (String field : queryAffectedHighlightedFields) {
MatchesIterator matchesIterator = matches.getMatches(field);
- if (matchesIterator == null) {
+ if (matchesIterator == null || matchesIterator == MatchesUtils.MATCH_WITH_NO_TERMS) {
// No matches on this field, even though the field was part of the query. This may be
// possible
// with complex queries that source non-text fields (have no "hit regions" in any textual
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/OffsetsFromPositions.java b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/OffsetsFromPositions.java
index a8d33a52a25..27fa17cefcc 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/OffsetsFromPositions.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/matchhighlight/OffsetsFromPositions.java
@@ -66,6 +66,11 @@ public final class OffsetsFromPositions implements OffsetsRetrievalStrategy {
return positionRanges;
}
+ // If values are not stored, there is nothing to highlight...
+ if (values.isEmpty()) {
+ return List.of();
+ }
+
class PositionSpan extends OffsetRange {
int leftOffset = Integer.MAX_VALUE;
int rightOffset = Integer.MIN_VALUE;