Index: lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/package.html =================================================================== --- lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/package.html (revision 1103147) +++ lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/package.html (revision ) @@ -88,6 +88,13 @@ boolean fillFields = true; SecondPassGroupingCollector c2 = new SecondPassGroupingCollector("author", topGroups, groupSort, docSort, docOffset+docsPerGroup, getScores, getMaxScores, fillFields); + //Optionally compute total group count + AllGroupsCollector allGroupsCollector = null; + if (requiredTotalGroupCount) { + allGroupsCollector = new AllGroupsCollector("author"); + c2 = MultiCollector.wrap(c2, allGroupsCollector); + } + if (cachedCollector.isCached()) { // Cache fit within maxCacheRAMMB, so we can replay it: cachedCollector.replay(c2); @@ -95,8 +102,11 @@ // Cache was too large; must re-execute query: s.search(new TermQuery(new Term("content", searchTerm)), c2); } - + TopGroups groupsResult = c2.getTopGroups(docOffset); + if (requiredTotalGroupCount) { + groupResult = new TopGroups(groupsResult, allGroupsCollector.getGroupCount()); + } // Render groupsResult... Index: lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/AllGroupsCollector.java =================================================================== --- lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/AllGroupsCollector.java (revision ) +++ lucene/contrib/grouping/src/java/org/apache/lucene/search/grouping/AllGroupsCollector.java (revision ) @@ -0,0 +1,123 @@ +package org.apache.lucene.search.grouping; + +/* + * 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. + */ + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.search.Collector; +import org.apache.lucene.search.FieldCache; +import org.apache.lucene.search.Scorer; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * A collector that collects all groups that match the query. Only the group value is collected. It can't determine + * the most relevant document of a group. + *
+ * Internally the {@link SentinelIntSet} is responsible for detecting if a group is already added to the total count. + * For each segment the {@link SentinelIntSet} is cleared and filled with previous counted groups that occur in + * the new segment. + * + * @lucene.experimental + */ +public class AllGroupsCollector extends Collector { + + private static final int DEFAULT_INITIAL_SIZE = 128; + + private final String groupField; + private final SentinelIntSet ordSet; + private final ListgetGroups().size()+ * + * @return The total number of groups for the executed search + */ + public int getGroupCount() { + return groups.size(); + } + + /** + * Returns the group values + * + * This is an unordered collections of group values. For each group that matched the query there is a {@link String} + * representing a group value. + * + * @return the group values + */ + public Collection
null this value is not computed. */
+ public final Integer totalGroupCount;
+
/** Group results in groupSort order */
public final GroupDocs[] groups;
@@ -47,5 +47,16 @@
this.totalHitCount = totalHitCount;
this.totalGroupedHitCount = totalGroupedHitCount;
this.groups = groups;
+ this.totalGroupCount = null;
}
+
+ public TopGroups(TopGroups oldTopGroups, Integer totalGroupCount) {
+ this.groupSort = oldTopGroups.groupSort;
+ this.withinGroupSort = oldTopGroups.withinGroupSort;
+ this.totalHitCount = oldTopGroups.totalHitCount;
+ this.totalGroupedHitCount = oldTopGroups.totalGroupedHitCount;
+ this.groups = oldTopGroups.groups;
+ this.totalGroupCount = totalGroupCount;
-}
+ }
+
+}
Index: lucene/contrib/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java
===================================================================
--- lucene/contrib/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java (revision 1103038)
+++ lucene/contrib/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java (revision )
@@ -17,13 +17,7 @@
package org.apache.lucene.search.grouping;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
@@ -32,14 +26,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
-import org.apache.lucene.search.Collector;
-import org.apache.lucene.search.FieldCache;
-import org.apache.lucene.search.FieldDoc;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.ScoreDoc;
-import org.apache.lucene.search.Sort;
-import org.apache.lucene.search.SortField;
-import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
@@ -241,6 +228,7 @@
boolean fillFields,
boolean getScores,
boolean getMaxScores,
+ boolean doTotalGroupCount,
Sort groupSort,
Sort docSort,
int topNGroups,
@@ -256,6 +244,7 @@
final List