javanna commented on code in PR #15574:
URL: https://github.com/apache/lucene/pull/15574#discussion_r3110569106


##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 FirstPassGroupingCollector. */
+public class FirstPassGroupingCollectorManager<T>
+    implements CollectorManager<FirstPassGroupingCollector<T>, 
Collection<SearchGroup<T>>> {
+
+  private final Supplier<GroupSelector<T>> groupSelectorFactory;
+  private final Sort groupSort;
+  private final int topNGroups;
+  private final boolean ignoreDocsWithoutGroupField;
+  private final List<FirstPassGroupingCollector<T>> collectors;
+
+  /**
+   * Creates a new FirstPassGroupingCollectorManager.
+   *
+   * @param groupSelectorFactory factory to create group selectors for each 
collector
+   * @param groupSort the sort to use for groups
+   * @param topNGroups the number of top groups to collect
+   */
+  public FirstPassGroupingCollectorManager(
+      Supplier<GroupSelector<T>> groupSelectorFactory, Sort groupSort, int 
topNGroups) {
+    this(groupSelectorFactory, groupSort, 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 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 topNGroups,
+      boolean ignoreDocsWithoutGroupField) {
+    this.groupSelectorFactory = groupSelectorFactory;
+    this.groupSort = groupSort;
+    this.topNGroups = topNGroups;
+    this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField;
+    this.collectors = new ArrayList<>();
+  }
+
+  @Override
+  public FirstPassGroupingCollector<T> newCollector() throws IOException {
+    FirstPassGroupingCollector<T> collector =
+        new FirstPassGroupingCollector<>(
+            groupSelectorFactory.get(), groupSort, topNGroups, 
ignoreDocsWithoutGroupField);
+    collectors.add(collector);
+    return collector;
+  }
+
+  @Override
+  public Collection<SearchGroup<T>> 
reduce(Collection<FirstPassGroupingCollector<T>> collectors)
+      throws IOException {
+    if (collectors.isEmpty()) {
+      return null;
+    }
+
+    if (collectors.size() == 1) {
+      return collectors.iterator().next().getTopGroups(0);
+    }

Review Comment:
   I have a slight preference to remove these two above conditionals. I wonder 
how much value they bring.



##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 FirstPassGroupingCollector. */
+public class FirstPassGroupingCollectorManager<T>

Review Comment:
   given the corresponding collector is experimental, should this be too?



##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java:
##########
@@ -277,11 +279,13 @@ private FirstPassGroupingCollector<?> 
createRandomFirstPassCollector(
       String groupField, Sort groupSort, int topDocs) throws IOException {
     if (random().nextBoolean()) {
       ValueSource vs = new BytesRefFieldSource(groupField);
-      return new FirstPassGroupingCollector<>(
-          new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, 
topDocs);
+      return new FirstPassGroupingCollectorManager<>(
+              () -> new ValueSourceGroupSelector(vs, new HashMap<>()), 
groupSort, topDocs)
+          .newCollector();
     } else {
-      return new FirstPassGroupingCollector<>(
-          new TermGroupSelector(groupField), groupSort, topDocs);
+      return new FirstPassGroupingCollectorManager<>(
+              () -> new TermGroupSelector(groupField), groupSort, topDocs)
+          .newCollector();

Review Comment:
   can usages like these also be replaced? Hopefully we will do a search in the 
test using the collector retrieved via the collector manager? Can we do the 
search providing the collector manager instead?



##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 FirstPassGroupingCollector. */
+public class FirstPassGroupingCollectorManager<T>
+    implements CollectorManager<FirstPassGroupingCollector<T>, 
Collection<SearchGroup<T>>> {
+
+  private final Supplier<GroupSelector<T>> groupSelectorFactory;
+  private final Sort groupSort;
+  private final int topNGroups;
+  private final boolean ignoreDocsWithoutGroupField;
+  private final List<FirstPassGroupingCollector<T>> collectors;
+
+  /**
+   * Creates a new FirstPassGroupingCollectorManager.
+   *
+   * @param groupSelectorFactory factory to create group selectors for each 
collector
+   * @param groupSort the sort to use for groups
+   * @param topNGroups the number of top groups to collect
+   */
+  public FirstPassGroupingCollectorManager(
+      Supplier<GroupSelector<T>> groupSelectorFactory, Sort groupSort, int 
topNGroups) {
+    this(groupSelectorFactory, groupSort, 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 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 topNGroups,
+      boolean ignoreDocsWithoutGroupField) {
+    this.groupSelectorFactory = groupSelectorFactory;
+    this.groupSort = groupSort;
+    this.topNGroups = topNGroups;
+    this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField;
+    this.collectors = new ArrayList<>();
+  }
+
+  @Override
+  public FirstPassGroupingCollector<T> newCollector() throws IOException {
+    FirstPassGroupingCollector<T> collector =
+        new FirstPassGroupingCollector<>(
+            groupSelectorFactory.get(), groupSort, topNGroups, 
ignoreDocsWithoutGroupField);
+    collectors.add(collector);
+    return collector;
+  }
+
+  @Override
+  public Collection<SearchGroup<T>> 
reduce(Collection<FirstPassGroupingCollector<T>> collectors)
+      throws IOException {
+    if (collectors.isEmpty()) {
+      return null;
+    }
+
+    if (collectors.size() == 1) {
+      return collectors.iterator().next().getTopGroups(0);
+    }
+
+    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, 0, topNGroups, groupSort);
+  }
+
+  public List<FirstPassGroupingCollector<T>> getCollectors() {

Review Comment:
   this looks unused, can we remove it?



##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java:
##########
@@ -277,11 +279,13 @@ private FirstPassGroupingCollector<?> 
createRandomFirstPassCollector(
       String groupField, Sort groupSort, int topDocs) throws IOException {
     if (random().nextBoolean()) {
       ValueSource vs = new BytesRefFieldSource(groupField);
-      return new FirstPassGroupingCollector<>(
-          new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, 
topDocs);
+      return new FirstPassGroupingCollectorManager<>(
+              () -> new ValueSourceGroupSelector(vs, new HashMap<>()), 
groupSort, topDocs)

Review Comment:
   should the hashmap be created for each supplier or shared across them?



##########
lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java:
##########
@@ -294,11 +298,13 @@ private FirstPassGroupingCollector<?> 
createFirstPassCollector(
     GroupSelector<?> selector = firstPassGroupingCollector.getGroupSelector();
     if (TermGroupSelector.class.isAssignableFrom(selector.getClass())) {
       ValueSource vs = new BytesRefFieldSource(groupField);
-      return new FirstPassGroupingCollector<>(
-          new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, 
topDocs);
+      return new FirstPassGroupingCollectorManager<>(
+              () -> new ValueSourceGroupSelector(vs, new HashMap<>()), 
groupSort, topDocs)

Review Comment:
   same as above regarding the map



##########
lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollectorManager.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 FirstPassGroupingCollector. */
+public class FirstPassGroupingCollectorManager<T>
+    implements CollectorManager<FirstPassGroupingCollector<T>, 
Collection<SearchGroup<T>>> {
+
+  private final Supplier<GroupSelector<T>> groupSelectorFactory;
+  private final Sort groupSort;
+  private final int topNGroups;
+  private final boolean ignoreDocsWithoutGroupField;
+  private final List<FirstPassGroupingCollector<T>> collectors;
+
+  /**
+   * Creates a new FirstPassGroupingCollectorManager.
+   *
+   * @param groupSelectorFactory factory to create group selectors for each 
collector
+   * @param groupSort the sort to use for groups
+   * @param topNGroups the number of top groups to collect
+   */
+  public FirstPassGroupingCollectorManager(
+      Supplier<GroupSelector<T>> groupSelectorFactory, Sort groupSort, int 
topNGroups) {
+    this(groupSelectorFactory, groupSort, 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 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 topNGroups,
+      boolean ignoreDocsWithoutGroupField) {
+    this.groupSelectorFactory = groupSelectorFactory;
+    this.groupSort = groupSort;
+    this.topNGroups = topNGroups;
+    this.ignoreDocsWithoutGroupField = ignoreDocsWithoutGroupField;
+    this.collectors = new ArrayList<>();
+  }
+
+  @Override
+  public FirstPassGroupingCollector<T> newCollector() throws IOException {
+    FirstPassGroupingCollector<T> collector =
+        new FirstPassGroupingCollector<>(
+            groupSelectorFactory.get(), groupSort, topNGroups, 
ignoreDocsWithoutGroupField);
+    collectors.add(collector);
+    return collector;
+  }
+
+  @Override
+  public Collection<SearchGroup<T>> 
reduce(Collection<FirstPassGroupingCollector<T>> collectors)
+      throws IOException {
+    if (collectors.isEmpty()) {
+      return null;

Review Comment:
   Returning null here could cause issues downstream.



-- 
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]

Reply via email to