javanna commented on code in PR #15574: URL: https://github.com/apache/lucene/pull/15574#discussion_r3272258667
########## lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java: ########## @@ -0,0 +1,121 @@ +/* + * 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 java.util.function.Supplier; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.Sort; + +/** + * A CollectorManager implementation for {@link FirstPassGroupingCollector} that supports parallel + * collection and merges results across segments. + * + * <p>Example usage: + * + * <pre class="prettyprint"> + * IndexSearcher searcher = new IndexSearcher(reader); + * Sort groupSort = Sort.RELEVANCE; + * int topNGroups = 10; + * + * FirstPassGroupingCollectorManager<BytesRef> manager = + * new FirstPassGroupingCollectorManager<>( + * () -> new TermGroupSelector("category"), + * groupSort, + * 0, + * topNGroups); + * + * Collection<SearchGroup<BytesRef>> topGroups = searcher.search(query, manager); + * + * // topGroups can then be passed to SecondPassGroupingCollector for full group results + * </pre> + * + * @lucene.experimental + */ +public class FirstPassGroupingCollectorManager<T> + implements CollectorManager<FirstPassGroupingCollector<T>, Collection<SearchGroup<T>>> { + + private final Supplier<GroupSelector<T>> groupSelectorFactory; + private final Sort groupSort; + private final int groupOffset; + private final int topNGroups; + private final boolean ignoreDocsWithoutGroupField; + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups) { + this(groupSelectorFactory, groupSort, groupOffset, topNGroups, false); + } + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + * @param ignoreDocsWithoutGroupField whether to ignore documents without a group field + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups, + boolean ignoreDocsWithoutGroupField) { + this.groupSelectorFactory = groupSelectorFactory; + this.groupSort = groupSort; + this.groupOffset = groupOffset; + this.topNGroups = topNGroups; + this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField; + } + + @Override + public FirstPassGroupingCollector<T> newCollector() throws IOException { + return new FirstPassGroupingCollector<>( + groupSelectorFactory.get(), + groupSort, + groupOffset + topNGroups, + ignoreDocsWithoutGroupField); + } + + @Override + public Collection<SearchGroup<T>> reduce(Collection<FirstPassGroupingCollector<T>> collectors) + throws IOException { + final List<Collection<SearchGroup<T>>> allGroups = new ArrayList<>(); + for (FirstPassGroupingCollector<T> collector : collectors) { + Collection<SearchGroup<T>> groups = collector.getTopGroups(0); + if (groups != null) { + allGroups.add(groups); + } + } + + return SearchGroup.merge(allGroups, groupOffset, topNGroups, groupSort); Review Comment: small thing I noticed: merge can return null, which is going to make the search method return null. That seems like a potential problem? We should add a test to cover that scenario as well. ########## lucene/CHANGES.txt: ########## @@ -335,6 +335,8 @@ Improvements * GITHUB#16000: Clarify that Accountable#ramBytesUsed() reports JVM heap memory only. (Luca Cavanna) +* GITHUB#15574: Introduce FirstPassGroupingCollectorManager to parallelize search when using FirstPassGroupingCollector. (Binlong Gao) Review Comment: I see another mention of 15574 above, could you correct that other entry please? ########## lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java: ########## @@ -0,0 +1,121 @@ +/* + * 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 java.util.function.Supplier; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.Sort; + +/** + * A CollectorManager implementation for {@link FirstPassGroupingCollector} that supports parallel + * collection and merges results across segments. + * + * <p>Example usage: + * + * <pre class="prettyprint"> + * IndexSearcher searcher = new IndexSearcher(reader); + * Sort groupSort = Sort.RELEVANCE; + * int topNGroups = 10; + * + * FirstPassGroupingCollectorManager<BytesRef> manager = + * new FirstPassGroupingCollectorManager<>( + * () -> new TermGroupSelector("category"), + * groupSort, + * 0, + * topNGroups); + * + * Collection<SearchGroup<BytesRef>> topGroups = searcher.search(query, manager); + * + * // topGroups can then be passed to SecondPassGroupingCollector for full group results + * </pre> + * + * @lucene.experimental + */ +public class FirstPassGroupingCollectorManager<T> + implements CollectorManager<FirstPassGroupingCollector<T>, Collection<SearchGroup<T>>> { + + private final Supplier<GroupSelector<T>> groupSelectorFactory; + private final Sort groupSort; + private final int groupOffset; + private final int topNGroups; + private final boolean ignoreDocsWithoutGroupField; + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups) { + this(groupSelectorFactory, groupSort, groupOffset, topNGroups, false); + } + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + * @param ignoreDocsWithoutGroupField whether to ignore documents without a group field + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups, + boolean ignoreDocsWithoutGroupField) { + this.groupSelectorFactory = groupSelectorFactory; + this.groupSort = groupSort; + this.groupOffset = groupOffset; + this.topNGroups = topNGroups; + this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField; Review Comment: Shall we add some basic validation to the constructor to make sure we don't accept values that are going to cause issues later on? ########## lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java: ########## @@ -0,0 +1,121 @@ +/* + * 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 java.util.function.Supplier; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.Sort; + +/** + * A CollectorManager implementation for {@link FirstPassGroupingCollector} that supports parallel + * collection and merges results across segments. + * + * <p>Example usage: + * + * <pre class="prettyprint"> + * IndexSearcher searcher = new IndexSearcher(reader); + * Sort groupSort = Sort.RELEVANCE; + * int topNGroups = 10; + * + * FirstPassGroupingCollectorManager<BytesRef> manager = + * new FirstPassGroupingCollectorManager<>( + * () -> new TermGroupSelector("category"), + * groupSort, + * 0, + * topNGroups); + * + * Collection<SearchGroup<BytesRef>> topGroups = searcher.search(query, manager); + * + * // topGroups can then be passed to SecondPassGroupingCollector for full group results + * </pre> + * + * @lucene.experimental + */ +public class FirstPassGroupingCollectorManager<T> + implements CollectorManager<FirstPassGroupingCollector<T>, Collection<SearchGroup<T>>> { + + private final Supplier<GroupSelector<T>> groupSelectorFactory; + private final Sort groupSort; + private final int groupOffset; + private final int topNGroups; + private final boolean ignoreDocsWithoutGroupField; + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups) { + this(groupSelectorFactory, groupSort, groupOffset, topNGroups, false); + } + + /** + * Creates a new FirstPassGroupingCollectorManager. + * + * @param groupSelectorFactory factory to create group selectors for each collector + * @param groupSort the sort to use for groups + * @param groupOffset The offset in the collected groups + * @param topNGroups the number of top groups to collect + * @param ignoreDocsWithoutGroupField whether to ignore documents without a group field + */ + public FirstPassGroupingCollectorManager( + Supplier<GroupSelector<T>> groupSelectorFactory, + Sort groupSort, + int groupOffset, + int topNGroups, + boolean ignoreDocsWithoutGroupField) { + this.groupSelectorFactory = groupSelectorFactory; + this.groupSort = groupSort; + this.groupOffset = groupOffset; + this.topNGroups = topNGroups; + this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField; + } + + @Override + public FirstPassGroupingCollector<T> newCollector() throws IOException { + return new FirstPassGroupingCollector<>( + groupSelectorFactory.get(), + groupSort, + groupOffset + topNGroups, + ignoreDocsWithoutGroupField); + } + + @Override + public Collection<SearchGroup<T>> reduce(Collection<FirstPassGroupingCollector<T>> collectors) + throws IOException { + final List<Collection<SearchGroup<T>>> allGroups = new ArrayList<>(); + for (FirstPassGroupingCollector<T> collector : collectors) { + Collection<SearchGroup<T>> groups = collector.getTopGroups(0); + if (groups != null) { + allGroups.add(groups); + } + } + + return SearchGroup.merge(allGroups, groupOffset, topNGroups, groupSort); Review Comment: I opened #16088 to fix this upstream, would still be good to add a small test for this scenario. ########## lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java: ########## @@ -0,0 +1,121 @@ +/* + * 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 java.util.function.Supplier; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.Sort; + +/** + * A CollectorManager implementation for {@link FirstPassGroupingCollector} that supports parallel + * collection and merges results across segments. + * + * <p>Example usage: + * + * <pre class="prettyprint"> + * IndexSearcher searcher = new IndexSearcher(reader); + * Sort groupSort = Sort.RELEVANCE; + * int topNGroups = 10; + * + * FirstPassGroupingCollectorManager<BytesRef> manager = + * new FirstPassGroupingCollectorManager<>( + * () -> new TermGroupSelector("category"), + * groupSort, + * 0, + * topNGroups); + * + * Collection<SearchGroup<BytesRef>> topGroups = searcher.search(query, manager); + * + * // topGroups can then be passed to SecondPassGroupingCollector for full group results Review Comment: Is this comment accurate? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
