diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index c86ec77b26f1..bdb09848e8c8 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -293,6 +293,8 @@ Optimizations * GITHUB#15886: Cache frozen FieldType to skip redundant schema validation. (Tim Brooks) +* GITHUB#15942: Skip max score computation when sorting by relevance in BlockGroupingCollector (Binlong Gao) + Bug Fixes --------------------- * GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java index 1f6a473f0e65..6e87825080ee 100644 --- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java +++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java @@ -277,6 +277,7 @@ public TopGroups getTopGroups( final Score fakeScorer = new Score(); float maxScore = Float.MIN_VALUE; + final boolean groupSortByRelevance = groupSort.equals(Sort.RELEVANCE); @SuppressWarnings({"unchecked", "rawtypes"}) final GroupDocs[] groups = new GroupDocs[groupQueue.size() - groupOffset]; @@ -286,7 +287,8 @@ public TopGroups getTopGroups( // At this point we hold all docs w/ in each group, // unsorted; we now sort them: final TopDocsCollector collector; - if (withinGroupSort.equals(Sort.RELEVANCE)) { + final boolean withinGroupSortByRelevance = withinGroupSort.equals(Sort.RELEVANCE); + if (withinGroupSortByRelevance) { // Sort by score if (!needsScores) { throw new IllegalArgumentException( @@ -309,7 +311,9 @@ public TopGroups getTopGroups( final int doc = og.docs[docIDX]; if (needsScores) { fakeScorer.score = og.scores[docIDX]; - groupMaxScore = Math.max(groupMaxScore, fakeScorer.score); + if (!withinGroupSortByRelevance) { + groupMaxScore = Math.max(groupMaxScore, fakeScorer.score); + } } leafCollector.collect(doc); } @@ -323,6 +327,9 @@ public TopGroups getTopGroups( } final TopDocs topDocs = collector.topDocs(withinGroupOffset, maxDocsPerGroup); + if (withinGroupSortByRelevance && topDocs.scoreDocs.length > 0) { + groupMaxScore = topDocs.scoreDocs[0].score; + } // TODO: we could aggregate scores across children // by Sum/Avg instead of passing NaN: @@ -334,7 +341,13 @@ public TopGroups getTopGroups( topDocs.scoreDocs, null, groupSortValues); - maxScore = Math.max(maxScore, groupMaxScore); + if (!groupSortByRelevance) { + maxScore = Math.max(maxScore, groupMaxScore); + } + } + + if (groupSortByRelevance) { + maxScore = groups[0].maxScore(); } /*