You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "gaobinlong (via GitHub)" <gi...@apache.org> on 2026/04/04 12:56:02 UTC
[PR] Introduce BlockGroupingCollectorManager [lucene]
gaobinlong opened a new pull request, #15936:
URL: https://github.com/apache/lucene/pull/15936
### Description
BlockGroupingCollector in grouping package did not have until now a corresponding collector manager, this pr introduces one and switches GroupingSearch to use search concurrency and move away from the deprecated search(Query, Collector) method.
This PR also implements a new TopGroups.mergeBlockGroup() method to merge blocking TopGroups() collected from different shards.
Relates to https://github.com/apache/lucene/issues/12892.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3295944942
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -280,4 +287,123 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
+ * * must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
+ * @param groupOffset Which group to start from.
+ * @param topNGroups How many top groups to keep.
+ * @return TopGroups instance or null if there are no groups.
+ */
+ public static TopGroups<?> mergeBlockGroups(
+ List<TopGroups<?>> shardGroups, Sort groupSort, int groupOffset, int topNGroups) {
+ if (shardGroups.isEmpty()) {
+ return null;
+ }
+
+ int totalGroupCount = 0;
+ int totalHitCount = 0;
+ int totalGroupedHitCount = 0;
+ for (TopGroups<?> sg : shardGroups) {
+ totalGroupCount += sg.totalGroupCount;
+ totalHitCount += sg.totalHitCount;
+ }
+
+ // k-way merge
+ GroupComparator groupComp = new GroupComparator(groupSort);
+ NavigableSet<MergedBlockGroup> queue = new TreeSet<>(groupComp);
+
+ // init queue
+ for (int idx = 0; idx < shardGroups.size(); idx++) {
+ GroupDocs<?> firstGroupDocs = shardGroups.get(idx).groups[0];
+ queue.add(new MergedBlockGroup(firstGroupDocs.groupSortValues(), idx, 0));
+ }
+
+ float maxScore = shardGroups.get(queue.first().shardIndex).groups[0].maxScore();
Review Comment:
This is really a bug, we need to compute maxScore by iterating on all TopGroups, only for the case of sort by relevance, we can get the maxScore directly from the head of the queue after initialized.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3353123018
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
Review Comment:
Changed that, thanks!
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +77,64 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
Review Comment:
Created a sub-IndexSearcher with passing an ExecutorService and overriding the slices() method so that the collector manager can work with intra-segment concurrency. Use this sub-IndexSearcher in all of the test method in this file.
I found this testShardedGrouping() is not needed, since we can only compare the totalHitCount of the final result between a single collector manager and one collector manager per shard, that's because when sorting by relevance, the final top-5 groups reduced by collector manager may differ from the one collector manager per shard's top-5 due to different score distributions, so finally I removed this test.
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -290,4 +296,146 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
+ * * must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
+ * @param groupOffset Which group to start from.
+ * @param topNGroups How many top groups to keep.
+ * @param docSort The sort to use within each group
+ * @return TopGroups instance or null if there are no groups.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> TopGroups<T> mergeBlockGroups(
+ List<TopGroups<T>> shardGroups,
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ Sort docSort) {
+ if (shardGroups.isEmpty()) {
+ return new TopGroups<>(
+ groupSort.getSort(),
+ docSort.getSort(),
+ 0,
+ 0,
+ (GroupDocs<T>[]) new GroupDocs<?>[0],
+ Float.NaN);
+ }
+
+ int totalGroupCount = 0;
+ int totalHitCount = 0;
+ int totalGroupedHitCount = 0;
+ for (TopGroups<T> sg : shardGroups) {
+ totalGroupCount += sg.totalGroupCount;
+ totalHitCount += sg.totalHitCount;
+ }
+
+ // k-way merge
+ GroupComparator groupComp = new GroupComparator(groupSort);
+ NavigableSet<MergedBlockGroup> queue = new TreeSet<>(groupComp);
+
+ float totalMaxScore = Float.NaN;
+ final boolean groupSortByRelevance = groupSort.equals(Sort.RELEVANCE);
+ // init queue
+ for (int idx = 0; idx < shardGroups.size(); idx++) {
+ TopGroups<T> topGroups = shardGroups.get(idx);
+ if (topGroups.groups.length == 0) {
+ continue;
+ }
+ if (!groupSortByRelevance) {
+ totalMaxScore = nonNANmax(totalMaxScore, topGroups.maxScore);
+ }
+ GroupDocs<T> firstGroupDocs = topGroups.groups[0];
+ queue.add(new MergedBlockGroup(firstGroupDocs.groupSortValues(), idx, 0));
+ }
+
+ if (groupSortByRelevance) {
+ totalMaxScore = shardGroups.get(queue.first().shardIndex).maxScore;
Review Comment:
Added a check for the queue.
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = new IndexSearcher(reader);
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.groupOffset = groupOffset;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.withinGroupSort = withinGroupSort;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ return new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public TopGroups<T> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ // Merge results from multiple collectors
Review Comment:
Removed yet.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "javanna (via GitHub)" <gi...@apache.org>.
javanna merged PR #15936:
URL: https://github.com/apache/lucene/pull/15936
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3379787561
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -58,7 +59,6 @@ public void testSimple() throws IOException {
// score of the top document from the same query with no grouping
TopDocs topDoc = searcher.search(topLevel, 1);
assertEquals(topDoc.scoreDocs[0].score, tg.groups[0].scoreDocs()[0].score, 0);
- assertEquals(topDoc.scoreDocs[0].doc, tg.groups[0].scoreDocs()[0].doc);
Review Comment:
We couldn't compare doc because in concurrent search mode, which doc with same highest score will in the final result is not guaranteed, the following tests already omitted this assert.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "javanna (via GitHub)" <gi...@apache.org>.
javanna commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3371738091
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -290,4 +296,151 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
Review Comment:
could you fix indentation here and the the javadoc format?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -290,4 +296,151 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
Review Comment:
do these fields need to be public?
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/AbstractGroupingTestCase.java:
##########
@@ -16,22 +16,141 @@
*/
package org.apache.lucene.search.grouping;
+import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import java.io.Closeable;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexReaderContext;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Weight;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.NamedThreadFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
/** Base class for grouping related tests. */
// TODO (MvG) : The grouping tests contain a lot of code duplication. Try to move the common code to
// this class..
public abstract class AbstractGroupingTestCase extends LuceneTestCase {
+ protected static ExecutorService testExecutor;
+
+ @BeforeClass
+ public static void setUpExecutor() {
+ testExecutor = Executors.newFixedThreadPool(2, new NamedThreadFactory("GroupingTest"));
+ }
+
+ @AfterClass
+ public static void tearDownExecutor() {
+ TestUtil.shutdownExecutorService(testExecutor);
+ testExecutor = null;
+ }
+
+ /**
+ * Creates an IndexSearcher with random concurrency. For intra-segment concurrency, slices are
+ * aligned on block boundaries using the provided lastDocPerGroup query so that no doc block is
+ * ever split across slices.
+ */
+ protected IndexSearcher newIndexSearcher(IndexReader reader, Query lastDocPerGroupQuery)
Review Comment:
I think I see why you had to add the following code to support intra segment concurrency. I wonder if this is necessary as part of this PR. I am not sure we should have this copy of the slices method and custom randomizations around concurrency applied. Could we instead use the `newSearcher` method and provide `INTER_SEGMENT` to it as concurrency mode, so that intra-segment is not tested, and add a comment that intra segment requires more work? We have this in a few others places already
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each slice is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p><b>NOTE</b>: All documents in a group block must be processed by the same {@link
+ * BlockGroupingCollector} instance. This means that the {@link
+ * org.apache.lucene.search.IndexSearcher}'s slices must not split a segment in a way that places
+ * documents from the same block into different slices. The default {@link
+ * org.apache.lucene.search.IndexSearcher#slices} implementation (inter-segment only) satisfies this
+ * constraint. If intra-segment concurrency is desired, the caller must override {@link
+ * org.apache.lucene.search.IndexSearcher#slices} to ensure each doc block falls entirely within one
+ * slice.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = ...; // your IndexSearcher
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ if (groupOffset < 0) {
+ throw new IllegalArgumentException("groupOffset must be >= 0 (got " + groupOffset + ")");
+ }
+
+ if (topNGroups < 1) {
+ throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
+ }
+
+ if (withinGroupOffset < 0) {
+ throw new IllegalArgumentException(
+ "withinGroupOffset must be >= 0 (got " + withinGroupOffset + ")");
+ }
+
+ if (maxDocsPerGroup < 1) {
+ throw new IllegalArgumentException(
+ "maxDocsPerGroup must be >= 1 (got " + maxDocsPerGroup + ")");
+ }
+
+ if (withinGroupSort.getSort().length == 0) {
Review Comment:
Sorry, my bad: I think this type of validation for the two Sort instances is redundant. `Sort` already does this in its constructor. We can remove it then.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "javanna (via GitHub)" <gi...@apache.org>.
javanna commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3355194690
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each slice is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p><b>NOTE</b>: All documents in a group block must be processed by the same {@link
+ * BlockGroupingCollector} instance. This means that the {@link
+ * org.apache.lucene.search.IndexSearcher}'s slices must not split a segment in a way that places
+ * documents from the same block into different slices. The default {@link
+ * org.apache.lucene.search.IndexSearcher#slices} implementation (inter-segment only) satisfies this
+ * constraint. If intra-segment concurrency is desired, the caller must override {@link
+ * org.apache.lucene.search.IndexSearcher#slices} to ensure each doc block falls entirely within one
+ * slice.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = ...; // your IndexSearcher
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ if (groupOffset < 0) {
+ throw new IllegalArgumentException("groupOffset must be >= 0 (got " + groupOffset + ")");
+ }
+
+ if (topNGroups < 1) {
+ throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
+ }
+
+ if (withinGroupOffset < 0) {
+ throw new IllegalArgumentException(
+ "withinGroupOffset must be >= 0 (got " + withinGroupOffset + ")");
+ }
+
+ if (maxDocsPerGroup < 1) {
+ throw new IllegalArgumentException(
+ "maxDocsPerGroup must be >= 1 (got " + maxDocsPerGroup + ")");
+ }
+
+ if (withinGroupSort.getSort().length == 0) {
+ throw new IllegalArgumentException("Sort must contain at least one field");
+ }
+
+ this.groupSort = groupSort;
+ this.groupOffset = groupOffset;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.withinGroupSort = withinGroupSort;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ return new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
Review Comment:
should this also use groupOffset?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each slice is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p><b>NOTE</b>: All documents in a group block must be processed by the same {@link
+ * BlockGroupingCollector} instance. This means that the {@link
+ * org.apache.lucene.search.IndexSearcher}'s slices must not split a segment in a way that places
+ * documents from the same block into different slices. The default {@link
+ * org.apache.lucene.search.IndexSearcher#slices} implementation (inter-segment only) satisfies this
+ * constraint. If intra-segment concurrency is desired, the caller must override {@link
+ * org.apache.lucene.search.IndexSearcher#slices} to ensure each doc block falls entirely within one
+ * slice.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = ...; // your IndexSearcher
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ if (groupOffset < 0) {
+ throw new IllegalArgumentException("groupOffset must be >= 0 (got " + groupOffset + ")");
+ }
+
+ if (topNGroups < 1) {
+ throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
+ }
+
+ if (withinGroupOffset < 0) {
+ throw new IllegalArgumentException(
+ "withinGroupOffset must be >= 0 (got " + withinGroupOffset + ")");
+ }
+
+ if (maxDocsPerGroup < 1) {
+ throw new IllegalArgumentException(
+ "maxDocsPerGroup must be >= 1 (got " + maxDocsPerGroup + ")");
+ }
+
+ if (withinGroupSort.getSort().length == 0) {
Review Comment:
should we validate groupSort in the same way just to make sure?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3367592987
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each slice is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p><b>NOTE</b>: All documents in a group block must be processed by the same {@link
+ * BlockGroupingCollector} instance. This means that the {@link
+ * org.apache.lucene.search.IndexSearcher}'s slices must not split a segment in a way that places
+ * documents from the same block into different slices. The default {@link
+ * org.apache.lucene.search.IndexSearcher#slices} implementation (inter-segment only) satisfies this
+ * constraint. If intra-segment concurrency is desired, the caller must override {@link
+ * org.apache.lucene.search.IndexSearcher#slices} to ensure each doc block falls entirely within one
+ * slice.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = ...; // your IndexSearcher
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ if (groupOffset < 0) {
+ throw new IllegalArgumentException("groupOffset must be >= 0 (got " + groupOffset + ")");
+ }
+
+ if (topNGroups < 1) {
+ throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
+ }
+
+ if (withinGroupOffset < 0) {
+ throw new IllegalArgumentException(
+ "withinGroupOffset must be >= 0 (got " + withinGroupOffset + ")");
+ }
+
+ if (maxDocsPerGroup < 1) {
+ throw new IllegalArgumentException(
+ "maxDocsPerGroup must be >= 1 (got " + maxDocsPerGroup + ")");
+ }
+
+ if (withinGroupSort.getSort().length == 0) {
+ throw new IllegalArgumentException("Sort must contain at least one field");
+ }
+
+ this.groupSort = groupSort;
+ this.groupOffset = groupOffset;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.withinGroupSort = withinGroupSort;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ return new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
Review Comment:
Yes, we should use groupOffset+topNGroups as topN here, then found that in GroupingSearch, topN is already set to offset+limit: https://github.com/apache/lucene/blob/dbc915f49b874887a9e2e9f5b3e774cdeb503ae2/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupingSearch.java#L195
this is not correct for collector manager, so fixed it.
In addition, TestGroupingSearch should also use the new index searcher we defined in TestBlockGrouping, so move the index searcher to AbstractGroupingTestCase to make it be shared with both classed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on PR #15936:
URL: https://github.com/apache/lucene/pull/15936#issuecomment-4196164173
@dweiss , could you help to review this PR? Thank you~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3295934755
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +77,64 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
Review Comment:
This test verifies that the result is consistent between a single collector manager and one collector manager per shard, it was inspired by https://github.com/apache/lucene/blob/9c957f8a2c6dea79e834b308ec2288d999f5acbd/lucene/grouping/src/test/org/apache/lucene/search/grouping/BaseGroupSelectorTestCase.java#L272.
That's true that we are testing search concurrency on the existing tests because they're using GroupingSearch class and we changed the code from collector to collector manager in that class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on PR #15936:
URL: https://github.com/apache/lucene/pull/15936#issuecomment-4531850453
@javanna all of the above comments are addressed, please help to review again, thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3296267539
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
+ implements CollectorManager<BlockGroupingCollector, TopGroups<?>> {
+
+ private final Sort groupSort;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int groupOffset;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ private final List<BlockGroupingCollector> collectors;
+
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int groupOffset,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.collectors = new ArrayList<>();
+ this.withinGroupSort = withinGroupSort;
+ this.groupOffset = groupOffset;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ BlockGroupingCollector collector =
+ new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ collectors.add(collector);
+ return collector;
+ }
+
+ @Override
+ public TopGroups<?> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ if (collectors.isEmpty()) {
+ return null;
+ }
+
+ if (collectors.size() == 1) {
+ return collectors
+ .iterator()
+ .next()
+ .getTopGroups(withinGroupSort, groupOffset, withinGroupOffset, maxDocsPerGroup);
+ }
+
+ // Merge results from multiple collectors
+ List<TopGroups<?>> shardGroupsList = new ArrayList<>();
+ for (BlockGroupingCollector collector : collectors) {
+ TopGroups<?> topGroups =
+ collector.getTopGroups(withinGroupSort, 0, withinGroupOffset, maxDocsPerGroup);
+ if (topGroups != null) {
Review Comment:
collector.getTopGroups can return null; For empty groups array case, we need to filter here, and also add a protection in the merge method, please help to review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3367565152
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each slice is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p><b>NOTE</b>: All documents in a group block must be processed by the same {@link
+ * BlockGroupingCollector} instance. This means that the {@link
+ * org.apache.lucene.search.IndexSearcher}'s slices must not split a segment in a way that places
+ * documents from the same block into different slices. The default {@link
+ * org.apache.lucene.search.IndexSearcher#slices} implementation (inter-segment only) satisfies this
+ * constraint. If intra-segment concurrency is desired, the caller must override {@link
+ * org.apache.lucene.search.IndexSearcher#slices} to ensure each doc block falls entirely within one
+ * slice.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = ...; // your IndexSearcher
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ if (groupOffset < 0) {
+ throw new IllegalArgumentException("groupOffset must be >= 0 (got " + groupOffset + ")");
+ }
+
+ if (topNGroups < 1) {
+ throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
+ }
+
+ if (withinGroupOffset < 0) {
+ throw new IllegalArgumentException(
+ "withinGroupOffset must be >= 0 (got " + withinGroupOffset + ")");
+ }
+
+ if (maxDocsPerGroup < 1) {
+ throw new IllegalArgumentException(
+ "maxDocsPerGroup must be >= 1 (got " + maxDocsPerGroup + ")");
+ }
+
+ if (withinGroupSort.getSort().length == 0) {
Review Comment:
Added, thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "javanna (via GitHub)" <gi...@apache.org>.
javanna commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3316222121
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -290,4 +296,146 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
+ * * must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
+ * @param groupOffset Which group to start from.
+ * @param topNGroups How many top groups to keep.
+ * @param docSort The sort to use within each group
+ * @return TopGroups instance or null if there are no groups.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> TopGroups<T> mergeBlockGroups(
+ List<TopGroups<T>> shardGroups,
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ Sort docSort) {
+ if (shardGroups.isEmpty()) {
+ return new TopGroups<>(
+ groupSort.getSort(),
+ docSort.getSort(),
+ 0,
+ 0,
+ (GroupDocs<T>[]) new GroupDocs<?>[0],
+ Float.NaN);
+ }
+
+ int totalGroupCount = 0;
+ int totalHitCount = 0;
+ int totalGroupedHitCount = 0;
+ for (TopGroups<T> sg : shardGroups) {
+ totalGroupCount += sg.totalGroupCount;
+ totalHitCount += sg.totalHitCount;
+ }
+
+ // k-way merge
+ GroupComparator groupComp = new GroupComparator(groupSort);
+ NavigableSet<MergedBlockGroup> queue = new TreeSet<>(groupComp);
+
+ float totalMaxScore = Float.NaN;
+ final boolean groupSortByRelevance = groupSort.equals(Sort.RELEVANCE);
+ // init queue
+ for (int idx = 0; idx < shardGroups.size(); idx++) {
+ TopGroups<T> topGroups = shardGroups.get(idx);
+ if (topGroups.groups.length == 0) {
+ continue;
+ }
+ if (!groupSortByRelevance) {
+ totalMaxScore = nonNANmax(totalMaxScore, topGroups.maxScore);
+ }
+ GroupDocs<T> firstGroupDocs = topGroups.groups[0];
+ queue.add(new MergedBlockGroup(firstGroupDocs.groupSortValues(), idx, 0));
+ }
+
+ if (groupSortByRelevance) {
+ totalMaxScore = shardGroups.get(queue.first().shardIndex).maxScore;
Review Comment:
Could it happen that the queue is empty and this throws exception?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
Review Comment:
each slice is searched by a separate collector. A slice is made of one or more segments. Actually, a slice could be a partition of a segment too.
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +78,66 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
+ Shard shardControl = new Shard();
+ int shardCount = random().nextInt(3) + 2;
+ Shard[] shards = new Shard[shardCount];
+ for (int shardIdx = 0; shardIdx < shardCount; shardIdx++) {
+ shards[shardIdx] = new Shard();
+ // int bookCount = atLeast(20);
+ for (int bookIdx = 0; bookIdx < 5; bookIdx++) {
+ List<Document> block = new ArrayList<>();
+ String bookName = "book" + shardIdx + bookIdx;
+ // int chapterCount = atLeast(10);
+ int chapterCount = 2;
+ for (int j = 0; j < 2; j++) {
+ Document doc = new Document();
+ String chapterName = "chapter" + j;
+ String chapterText = randomText();
+ doc.add(new TextField("book", bookName, Field.Store.YES));
+ doc.add(new TextField("chapter", chapterName, Field.Store.YES));
+ doc.add(new TextField("text", chapterText, Field.Store.NO));
+ doc.add(new NumericDocValuesField("length", chapterText.length()));
+ doc.add(new SortedDocValuesField("book", new BytesRef(bookName)));
+ if (j == chapterCount - 1) {
+ doc.add(new TextField("blockEnd", "true", Field.Store.NO));
+ }
+ block.add(doc);
+ }
+ shards[shardIdx].writer.addDocuments(block);
+ shardControl.writer.addDocuments(block);
+ }
+ }
+
+ IndexSearcher shardControlIndexSearcher = shardControl.getIndexSearcher();
+
+ Query blockEndQuery = new TermQuery(new Term("blockEnd", "true"));
+ GroupingSearch grouper = new GroupingSearch(blockEndQuery);
+ grouper.setGroupDocsLimit(10);
+
+ Query topLevel = new TermQuery(new Term("text", "grandmother"));
+ TopGroups<BytesRef> singleShardTopGroups =
+ grouper.search(shardControlIndexSearcher, topLevel, 0, 5);
+
+ List<TopGroups<BytesRef>> shardTopGroups = new ArrayList<>();
+ for (int shardIdx = 0; shardIdx < shardCount; shardIdx++) {
+ shardTopGroups.add(grouper.search(shards[shardIdx].getIndexSearcher(), topLevel, 0, 5));
+ }
+
+ TopGroups<BytesRef> mergedTopGroups =
+ TopGroups.mergeBlockGroups(shardTopGroups, Sort.RELEVANCE, 0, 5, Sort.RELEVANCE);
+ assertNotNull(mergedTopGroups);
+
+ assertEquals(singleShardTopGroups.totalHitCount, mergedTopGroups.totalHitCount);
+ assertEquals(singleShardTopGroups.totalGroupCount, mergedTopGroups.totalGroupCount);
+ assertEquals(singleShardTopGroups.groups.length, mergedTopGroups.groups.length);
Review Comment:
do we need to also verify ordering and add a bit more assertions here?
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +77,64 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
Review Comment:
ok thanks for clarifying, could you add some javadocs perhaps to this test method to clarify its purpose?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * <p>Example usage:
+ *
+ * <pre class="prettyprint">
+ * IndexSearcher searcher = new IndexSearcher(reader);
+ * Query lastDocPerGroupQuery = new TermQuery(new Term("groupEnd", "true"));
+ * Weight lastDocPerGroup = searcher.createWeight(
+ * searcher.rewrite(lastDocPerGroupQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
+ *
+ * BlockGroupingCollectorManager<BytesRef> manager = new BlockGroupingCollectorManager<>(
+ * Sort.RELEVANCE, // groupSort
+ * 0, // groupOffset
+ * 10, // topNGroups
+ * true, // needsScores
+ * lastDocPerGroup,
+ * Sort.RELEVANCE, // withinGroupSort
+ * 0, // withinGroupOffset
+ * 5); // maxDocsPerGroup
+ *
+ * TopGroups<BytesRef> result = searcher.search(query, manager);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager<T>
+ implements CollectorManager<BlockGroupingCollector, TopGroups<T>> {
+
+ private final Sort groupSort;
+ private final int groupOffset;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ /**
+ * Creates a new BlockGroupingCollectorManager.
+ *
+ * @param groupSort the sort used to rank groups
+ * @param groupOffset the offset into the groups to start returning from
+ * @param topNGroups the number of top groups to collect
+ * @param needsScores whether scores are needed (must be true if groupSort or withinGroupSort uses
+ * scores)
+ * @param lastDocPerGroup a {@link Weight} that matches the last document in each group block
+ * @param withinGroupSort the sort used to rank documents within each group
+ * @param withinGroupOffset the offset into each group's documents to start returning from
+ * @param maxDocsPerGroup the maximum number of documents to return per group
+ */
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int groupOffset,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.groupOffset = groupOffset;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.withinGroupSort = withinGroupSort;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ return new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public TopGroups<T> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ // Merge results from multiple collectors
Review Comment:
I am not sure that this comment adds value, should we remove it?
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +77,64 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
Review Comment:
On exercising concurrency: Ii I understand correctly, the searcher is created via `shard.getIndexSearcher`, which does `searcher = new IndexSearcher(this.writer.getReader());` without passing an executor. This way there is no concurrency, a single collector goes sequentially through all the segments?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on PR #15936:
URL: https://github.com/apache/lucene/pull/15936#issuecomment-4658738686
@javanna , all comments are addressed, please take another look, thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "javanna (via GitHub)" <gi...@apache.org>.
javanna commented on code in PR #15936:
URL: https://github.com/apache/lucene/pull/15936#discussion_r3266083896
##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestBlockGrouping.java:
##########
@@ -77,6 +77,64 @@ public void testSimple() throws IOException {
shard.close();
}
+ public void testShardedBlockGrouping() throws IOException {
Review Comment:
Are we testing with search concurrency on in this test + the existing tests?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
+ implements CollectorManager<BlockGroupingCollector, TopGroups<?>> {
+
+ private final Sort groupSort;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int groupOffset;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ private final List<BlockGroupingCollector> collectors;
+
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int groupOffset,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.collectors = new ArrayList<>();
+ this.withinGroupSort = withinGroupSort;
+ this.groupOffset = groupOffset;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ BlockGroupingCollector collector =
+ new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ collectors.add(collector);
+ return collector;
+ }
+
+ @Override
+ public TopGroups<?> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ if (collectors.isEmpty()) {
+ return null;
+ }
+
+ if (collectors.size() == 1) {
+ return collectors
+ .iterator()
+ .next()
+ .getTopGroups(withinGroupSort, groupOffset, withinGroupOffset, maxDocsPerGroup);
+ }
Review Comment:
Avoid returning null above and remove this conditional? Is it necessary?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -280,4 +287,123 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
+ * * must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
+ * @param groupOffset Which group to start from.
+ * @param topNGroups How many top groups to keep.
+ * @return TopGroups instance or null if there are no groups.
+ */
+ public static TopGroups<?> mergeBlockGroups(
Review Comment:
We could use generics at our advantage here, to enforce consistency
##########
lucene/CHANGES.txt:
##########
@@ -115,6 +115,8 @@ Improvements
* GITHUB#15225: Improve package documentation for org.apache.lucene.util. (Syed Mohammad Saad)
+* GITHUB#15936: Introduce BlockGroupingCollectorManager to parallelize search when using BlockGroupingCollector. (Binlong Gao)
Review Comment:
Move this to 10.5 ? I think it makes sense to backport it
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/TopGroups.java:
##########
@@ -280,4 +287,123 @@ public static <T> TopGroups<T> merge(
totalMaxScore);
}
}
+
+ private record MergedBlockGroup(Object[] topValues, int shardIndex, int groupIndex) {}
+
+ private static class GroupComparator implements Comparator<MergedBlockGroup> {
+ @SuppressWarnings("rawtypes")
+ public final FieldComparator[] comparators;
+
+ public final int[] reversed;
+
+ @SuppressWarnings({"rawtypes"})
+ public GroupComparator(Sort groupSort) {
+ final SortField[] sortFields = groupSort.getSort();
+ comparators = new FieldComparator[sortFields.length];
+ reversed = new int[sortFields.length];
+ for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
+ final SortField sortField = sortFields[compIDX];
+ comparators[compIDX] = sortField.getComparator(1, Pruning.NONE);
+ reversed[compIDX] = sortField.getReverse() ? -1 : 1;
+ }
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked"})
+ public int compare(MergedBlockGroup group, MergedBlockGroup other) {
+ if (group == other) {
+ return 0;
+ }
+ final Object[] groupValues = group.topValues;
+ final Object[] otherValues = other.topValues;
+ for (int compIDX = 0; compIDX < comparators.length; compIDX++) {
+ final int c =
+ reversed[compIDX]
+ * comparators[compIDX].compareValues(groupValues[compIDX], otherValues[compIDX]);
+ if (c != 0) {
+ return c;
+ }
+ }
+
+ assert group.shardIndex != other.shardIndex;
+ return group.shardIndex - other.shardIndex;
+ }
+ }
+
+ /**
+ * Merge TopGroups that are partitioned into blocks per shard. This method assumes that within
+ * each shard, the groups are sorted according to the groupSort.
+ *
+ * @param shardGroups list of TopGroups, one per shard.
+ * @param groupSort The {@link Sort} used to sort the groups. The top sorted document within each
+ * * group according to groupSort, determines how that group sorts against other groups. This
+ * * must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
+ * @param groupOffset Which group to start from.
+ * @param topNGroups How many top groups to keep.
+ * @return TopGroups instance or null if there are no groups.
+ */
+ public static TopGroups<?> mergeBlockGroups(
+ List<TopGroups<?>> shardGroups, Sort groupSort, int groupOffset, int topNGroups) {
+ if (shardGroups.isEmpty()) {
+ return null;
+ }
+
+ int totalGroupCount = 0;
+ int totalHitCount = 0;
+ int totalGroupedHitCount = 0;
+ for (TopGroups<?> sg : shardGroups) {
+ totalGroupCount += sg.totalGroupCount;
+ totalHitCount += sg.totalHitCount;
+ }
+
+ // k-way merge
+ GroupComparator groupComp = new GroupComparator(groupSort);
+ NavigableSet<MergedBlockGroup> queue = new TreeSet<>(groupComp);
+
+ // init queue
+ for (int idx = 0; idx < shardGroups.size(); idx++) {
+ GroupDocs<?> firstGroupDocs = shardGroups.get(idx).groups[0];
+ queue.add(new MergedBlockGroup(firstGroupDocs.groupSortValues(), idx, 0));
+ }
+
+ float maxScore = shardGroups.get(queue.first().shardIndex).groups[0].maxScore();
Review Comment:
is the maxScore only read once from the first group?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
+ implements CollectorManager<BlockGroupingCollector, TopGroups<?>> {
+
+ private final Sort groupSort;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int groupOffset;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ private final List<BlockGroupingCollector> collectors;
Review Comment:
can we remove this list? I don't think that tracking the collectors getting created brings any value.
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
Review Comment:
We could use a T generic type for the manager that's the same as the TopGroups?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
+ implements CollectorManager<BlockGroupingCollector, TopGroups<?>> {
+
+ private final Sort groupSort;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int groupOffset;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ private final List<BlockGroupingCollector> collectors;
+
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int groupOffset,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.collectors = new ArrayList<>();
+ this.withinGroupSort = withinGroupSort;
+ this.groupOffset = groupOffset;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ BlockGroupingCollector collector =
+ new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ collectors.add(collector);
+ return collector;
+ }
+
+ @Override
+ public TopGroups<?> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ if (collectors.isEmpty()) {
+ return null;
+ }
+
+ if (collectors.size() == 1) {
+ return collectors
+ .iterator()
+ .next()
+ .getTopGroups(withinGroupSort, groupOffset, withinGroupOffset, maxDocsPerGroup);
+ }
+
+ // Merge results from multiple collectors
+ List<TopGroups<?>> shardGroupsList = new ArrayList<>();
+ for (BlockGroupingCollector collector : collectors) {
+ TopGroups<?> topGroups =
+ collector.getTopGroups(withinGroupSort, 0, withinGroupOffset, maxDocsPerGroup);
+ if (topGroups != null) {
+ shardGroupsList.add(topGroups);
+ }
+ }
+
+ if (shardGroupsList.isEmpty()) {
+ return null;
Review Comment:
avoid returning null?
##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollectorManager.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search.grouping;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.CollectorManager;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A {@link CollectorManager} for {@link BlockGroupingCollector} that merges results from multiple
+ * collectors into a single {@link TopGroups}. This is intended for use with concurrent search,
+ * where each segment is searched by a separate {@link BlockGroupingCollector}.
+ *
+ * <p>Documents must be indexed as blocks using {@link
+ * org.apache.lucene.index.IndexWriter#addDocuments IndexWriter.addDocuments()} or {@link
+ * org.apache.lucene.index.IndexWriter#updateDocuments IndexWriter.updateDocuments()}.
+ *
+ * <p>See {@link BlockGroupingCollector} for more details.
+ *
+ * @lucene.experimental
+ */
+public class BlockGroupingCollectorManager
+ implements CollectorManager<BlockGroupingCollector, TopGroups<?>> {
+
+ private final Sort groupSort;
+ private final int topNGroups;
+ private final boolean needsScores;
+ private final Weight lastDocPerGroup;
+
+ private final Sort withinGroupSort;
+ private final int groupOffset;
+ private final int withinGroupOffset;
+ private final int maxDocsPerGroup;
+
+ private final List<BlockGroupingCollector> collectors;
+
+ public BlockGroupingCollectorManager(
+ Sort groupSort,
+ int topNGroups,
+ boolean needsScores,
+ Weight lastDocPerGroup,
+ Sort withinGroupSort,
+ int groupOffset,
+ int withinGroupOffset,
+ int maxDocsPerGroup) {
+ this.groupSort = groupSort;
+ this.topNGroups = topNGroups;
+ this.needsScores = needsScores;
+ this.lastDocPerGroup = lastDocPerGroup;
+ this.collectors = new ArrayList<>();
+ this.withinGroupSort = withinGroupSort;
+ this.groupOffset = groupOffset;
+ this.withinGroupOffset = withinGroupOffset;
+ this.maxDocsPerGroup = maxDocsPerGroup;
+ }
+
+ @Override
+ public BlockGroupingCollector newCollector() throws IOException {
+ BlockGroupingCollector collector =
+ new BlockGroupingCollector(groupSort, topNGroups, needsScores, lastDocPerGroup);
+ collectors.add(collector);
+ return collector;
+ }
+
+ @Override
+ public TopGroups<?> reduce(Collection<BlockGroupingCollector> collectors) throws IOException {
+ if (collectors.isEmpty()) {
+ return null;
+ }
+
+ if (collectors.size() == 1) {
+ return collectors
+ .iterator()
+ .next()
+ .getTopGroups(withinGroupSort, groupOffset, withinGroupOffset, maxDocsPerGroup);
+ }
+
+ // Merge results from multiple collectors
+ List<TopGroups<?>> shardGroupsList = new ArrayList<>();
+ for (BlockGroupingCollector collector : collectors) {
+ TopGroups<?> topGroups =
+ collector.getTopGroups(withinGroupSort, 0, withinGroupOffset, maxDocsPerGroup);
+ if (topGroups != null) {
Review Comment:
is this a real-life scenario? And what about top groups with empty groups array?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Introduce BlockGroupingCollectorManager [lucene]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15936:
URL: https://github.com/apache/lucene/pull/15936#issuecomment-4292783604
This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org