From 9130ba9190e2b1d77eb830d373b4b0a35eb0db92 Mon Sep 17 00:00:00 2001 From: Binlong Gao Date: Wed, 8 Apr 2026 14:48:19 +0800 Subject: [PATCH 1/2] Skip max score computation when sorting by relevance in BlockGroupingCollector Signed-off-by: Binlong Gao --- lucene/CHANGES.txt | 2 ++ .../grouping/BlockGroupingCollector.java | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index c86ec77b26f1..3276c929062b 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#15886: 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(); } /* From 3e9e31da1bd47cf3eac13729aa23388ff8462001 Mon Sep 17 00:00:00 2001 From: Binlong Gao Date: Wed, 8 Apr 2026 17:24:54 +0800 Subject: [PATCH 2/2] Modify change log Signed-off-by: Binlong Gao --- lucene/CHANGES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 3276c929062b..bdb09848e8c8 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -293,7 +293,7 @@ Optimizations * GITHUB#15886: Cache frozen FieldType to skip redundant schema validation. (Tim Brooks) -* GITHUB#15886: Skip max score computation when sorting by relevance in BlockGroupingCollector (Binlong Gao) +* GITHUB#15942: Skip max score computation when sorting by relevance in BlockGroupingCollector (Binlong Gao) Bug Fixes ---------------------