This is an automated email from the ASF dual-hosted git repository.

ishan pushed a commit to branch ishan/upgrade-to-lucene-10
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 3f2503b672aa80f2b967f9f4a9d1d851b601c336
Author: Ishan Chattopadhyaya <[email protected]>
AuthorDate: Wed Aug 6 04:15:07 2025 +0530

    SOLR-17631: Fix grouping to properly combine main query with filters for 
scoring
    
    The grouping implementation was only using filter queries for search 
operations,
    ignoring the main query entirely. This caused function queries like 
{\!func}foo2_i
    to be ignored during grouping, resulting in all groups receiving a score of 
1.0
    from MatchAllDocsQuery instead of proper function query scores.
    
    This broke group ordering when Sort.RELEVANCE was used (the default), as 
groups
    were not ordered by their maximum scores as expected.
    
    The fix uses QueryUtils.combineQueryAndFilter() to properly combine the 
main query
    (for scoring) with filter queries (for filtering), matching the behavior of 
normal
    Solr search operations. This ensures function queries and other scoring 
queries
    work correctly with grouping.
    
    Fixes TestGroupingSearch.testGroupAPI and related grouping functionality 
that
    relies on proper query scoring.
---
 solr/core/src/java/org/apache/solr/search/Grouping.java | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/search/Grouping.java 
b/solr/core/src/java/org/apache/solr/search/Grouping.java
index 285e1e961a3..72055ee0f96 100644
--- a/solr/core/src/java/org/apache/solr/search/Grouping.java
+++ b/solr/core/src/java/org/apache/solr/search/Grouping.java
@@ -36,6 +36,7 @@ import org.apache.lucene.search.MultiCollector;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.ScoreDoc;
 import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.TopDocsCollector;
 import org.apache.lucene.search.TopFieldCollector;
@@ -298,7 +299,7 @@ public class Grouping {
 
     SolrIndexSearcher.ProcessedFilter pf = 
searcher.getProcessedFilter(cmd.getFilterList());
 
-    final Query filterQuery = pf.filter == null ? new MatchAllDocsQuery() : 
pf.filter;
+    final Query searchQuery = 
QueryUtils.combineQueryAndFilter(QueryUtils.makeQueryable(cmd.getQuery()), 
pf.filter);
     maxDoc = searcher.maxDoc();
 
     needScores = (cmd.getFlags() & SolrIndexSearcher.GET_SCORES) != 0;
@@ -355,7 +356,7 @@ public class Grouping {
     }
 
     if (allCollectors != null) {
-      searchWithTimeLimiter(filterQuery, allCollectors);
+      searchWithTimeLimiter(searchQuery, allCollectors);
 
       if (allCollectors instanceof DelegatingCollector) {
         ((DelegatingCollector) allCollectors).complete();
@@ -388,14 +389,14 @@ public class Grouping {
                     "The grouping cache is active, but not used because it 
exceeded the max cache limit of %d percent",
                     maxDocsPercentageToCache));
             log.warn("Please increase cache size or disable group caching.");
-            searchWithTimeLimiter(filterQuery, secondPhaseCollectors);
+            searchWithTimeLimiter(searchQuery, secondPhaseCollectors);
           }
         } else {
           if (pf.postFilter != null) {
             pf.postFilter.setLastDelegate(secondPhaseCollectors);
             secondPhaseCollectors = pf.postFilter;
           }
-          searchWithTimeLimiter(filterQuery, secondPhaseCollectors);
+          searchWithTimeLimiter(searchQuery, secondPhaseCollectors);
         }
         if (secondPhaseCollectors instanceof DelegatingCollector) {
           ((DelegatingCollector) secondPhaseCollectors).complete();
@@ -425,9 +426,9 @@ public class Grouping {
    * Invokes search with the specified filter and collector. If a time limit 
has been specified,
    * wrap the collector in a TimeLimitingCollector
    */
-  private void searchWithTimeLimiter(final Query filterQuery, Collector 
collector)
+  private void searchWithTimeLimiter(final Query searchQuery, Collector 
collector)
       throws IOException {
-    searcher.search(filterQuery, collector);
+    searcher.search(searchQuery, collector);
   }
 
   /**
@@ -724,7 +725,7 @@ public class Grouping {
       groupSort = groupSort == null ? Sort.RELEVANCE : groupSort;
       firstPass =
           new FirstPassGroupingCollector<>(
-              new TermGroupSelector(groupBy), groupSort, actualGroupsToFind);
+              new TermGroupSelector(groupBy), searcher.weightSort(groupSort), 
actualGroupsToFind);
       return firstPass;
     }
 

Reply via email to