Apache Lucene open-source search software
Fix TopGroupsCollectorManager ignoring withinGroupOffset (#15995)
gaobinlongdc190df0955e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 8ef68343e5f..f0bb778d385 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -309,7 +309,7 @@ Improvements* GITHUB#15976: Store DocValuesSkipper data in their own file. (Alan Woodward)-* GITHUB#15574: Introduce TopGroupsCollectorManager to parallelize search when using TopGroupsCollector. (Binlong Gao)+* GITHUB#15574, GITHUB#15995: Introduce TopGroupsCollectorManager to parallelize search when using TopGroupsCollector. (Binlong Gao)* GITHUB#15989: DocValuesRangeIterator always tries to use skipper-based block iterationas its approximation. (Alan Woodward)diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroupsCollectorManager.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroupsCollectorManager.javaindex 74210871921..cd27475c6c0 100644--- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroupsCollectorManager.java+++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroupsCollectorManager.java@@ -32,6 +32,7 @@ public class TopGroupsCollectorManager<T>private final Collection<SearchGroup<T>> searchGroups;private final Sort groupSort;private final Sort sortWithinGroup;+ private final int withinGroupOffset;private final int maxDocsPerGroup;private final boolean getMaxScores;private final TopGroups.ScoreMergeMode scoreMergeMode;@@ -44,6 +45,7 @@ public class TopGroupsCollectorManager<T>* @param searchGroups the search groups from the first pass* @param groupSort the sort to use for groups* @param sortWithinGroup the sort to use within each group+ * @param withinGroupOffset the offset within each group to start collecting documents* @param maxDocsPerGroup the maximum number of documents per group* @param getMaxScores whether to compute max scores*/@@ -52,6 +54,7 @@ public class TopGroupsCollectorManager<T>Collection<SearchGroup<T>> searchGroups,Sort groupSort,Sort sortWithinGroup,+ int withinGroupOffset,int maxDocsPerGroup,boolean getMaxScores) {this(@@ -59,6 +62,7 @@ public class TopGroupsCollectorManager<T>searchGroups,groupSort,sortWithinGroup,+ withinGroupOffset,maxDocsPerGroup,getMaxScores,TopGroups.ScoreMergeMode.None);@@ -71,6 +75,7 @@ public class TopGroupsCollectorManager<T>* @param searchGroups the search groups from the first pass* @param groupSort the sort to use for groups* @param sortWithinGroup the sort to use within each group+ * @param withinGroupOffset the offset within each group to start collecting documents* @param maxDocsPerGroup the maximum number of documents per group* @param getMaxScores whether to compute max scores* @param scoreMergeMode the mode for merging scores across shards@@ -80,6 +85,7 @@ public class TopGroupsCollectorManager<T>Collection<SearchGroup<T>> searchGroups,Sort groupSort,Sort sortWithinGroup,+ int withinGroupOffset,int maxDocsPerGroup,boolean getMaxScores,TopGroups.ScoreMergeMode scoreMergeMode) {@@ -87,6 +93,7 @@ public class TopGroupsCollectorManager<T>this.searchGroups = searchGroups;this.groupSort = groupSort;this.sortWithinGroup = sortWithinGroup;+ this.withinGroupOffset = withinGroupOffset;this.maxDocsPerGroup = maxDocsPerGroup;this.getMaxScores = getMaxScores;this.scoreMergeMode = scoreMergeMode;@@ -101,7 +108,7 @@ public class TopGroupsCollectorManager<T>searchGroups,groupSort,sortWithinGroup,- maxDocsPerGroup,+ withinGroupOffset + maxDocsPerGroup,getMaxScores);collectors.add(collector);return collector;@@ -109,32 +116,15 @@ public class TopGroupsCollectorManager<T>@Overridepublic TopGroups<T> reduce(Collection<TopGroupsCollector<T>> collectors) throws IOException {- if (collectors.isEmpty()) {- return null;- }-- if (collectors.size() == 1) {- return collectors.iterator().next().getTopGroups(0);- }-// Merge results from multiple collectors- List<TopGroups<T>> shardGroupsList = new ArrayList<>();- for (TopGroupsCollector<T> collector : collectors) {- TopGroups<T> groups = collector.getTopGroups(0);- if (groups != null) {- shardGroupsList.add(groups);- }- }-- if (shardGroupsList.isEmpty()) {- return null;- }+ List<TopGroups<T>> shardGroupsList = collectors.stream().map(c -> c.getTopGroups(0)).toList();return TopGroups.merge(- shardGroupsList, groupSort, sortWithinGroup, 0, maxDocsPerGroup, scoreMergeMode);- }-- public List<TopGroupsCollector<T>> getCollectors() {- return collectors;+ shardGroupsList,+ groupSort,+ sortWithinGroup,+ withinGroupOffset,+ maxDocsPerGroup,+ scoreMergeMode);}}diff --git a/lucene/grouping/src/test/org/apache/lucene/search/grouping/BaseGroupSelectorTestCase.java b/lucene/grouping/src/test/org/apache/lucene/search/grouping/BaseGroupSelectorTestCase.javaindex d936822cb6e..1296c3a1e8c 100644--- a/lucene/grouping/src/test/org/apache/lucene/search/grouping/BaseGroupSelectorTestCase.java+++ b/lucene/grouping/src/test/org/apache/lucene/search/grouping/BaseGroupSelectorTestCase.java@@ -324,9 +324,16 @@ public abstract class BaseGroupSelectorTestCase<T> extends AbstractGroupingTestCCollection<SearchGroup<T>> mergedGroups = SearchGroup.merge(shardGroups, 0, 5, sort);assertEquals(singletonGroups, mergedGroups);+ final int withinGroupOffset = random().nextInt(numDocs);TopGroupsCollectorManager<T> topGroupsCollectorManager =new TopGroupsCollectorManager<>(- this::getGroupSelector, singletonGroups, sort, Sort.RELEVANCE, 5, true);+ this::getGroupSelector,+ singletonGroups,+ sort,+ Sort.RELEVANCE,+ withinGroupOffset,+ 5,+ true);TopGroups<T> singletonTopGroups =control.getIndexSearcher().search(topLevel, topGroupsCollectorManager);@@ -334,11 +341,23 @@ public abstract class BaseGroupSelectorTestCase<T> extends AbstractGroupingTestCfor (Shard shard : shards) {TopGroupsCollectorManager<T> scm =new TopGroupsCollectorManager<>(- this::getGroupSelector, mergedGroups, sort, Sort.RELEVANCE, 5, true);+ this::getGroupSelector,+ mergedGroups,+ sort,+ Sort.RELEVANCE,+ 0,+ withinGroupOffset + 5,+ true);shardTopGroups.add(shard.getIndexSearcher().search(topLevel, scm));}TopGroups<T> mergedTopGroups =- TopGroups.merge(shardTopGroups, sort, Sort.RELEVANCE, 0, 5, TopGroups.ScoreMergeMode.None);+ TopGroups.merge(+ shardTopGroups,+ sort,+ Sort.RELEVANCE,+ withinGroupOffset,+ 5,+ TopGroups.ScoreMergeMode.None);assertNotNull(mergedTopGroups);assertEquals(singletonTopGroups.totalGroupedHitCount, mergedTopGroups.totalGroupedHitCount);