diff --git a/solr/src/common/org/apache/solr/common/params/GroupParams.java b/solr/src/common/org/apache/solr/common/params/GroupParams.java index 6f2f2ff9fc0..8e75d1beb0b 100755 --- a/solr/src/common/org/apache/solr/common/params/GroupParams.java +++ b/solr/src/common/org/apache/solr/common/params/GroupParams.java @@ -35,5 +35,8 @@ public interface GroupParams { /** treat the first group result as the main result. true/false */ public static final String GROUP_MAIN = GROUP + ".main"; + + /** treat the first group result as the main result. true/false */ + public static final String GROUP_FORMAT = GROUP + ".format"; } diff --git a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/src/java/org/apache/solr/handler/component/QueryComponent.java index e7e137b2e63..00dc0e6f91d 100644 --- a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java +++ b/solr/src/java/org/apache/solr/handler/component/QueryComponent.java @@ -307,6 +307,8 @@ public class QueryComponent extends SearchComponent String[] queries = params.getParams(GroupParams.GROUP_QUERY); String groupSortStr = params.get(GroupParams.GROUP_SORT); boolean main = params.getBool(GroupParams.GROUP_MAIN, false); + String format = params.get(GroupParams.GROUP_FORMAT); + Grouping.Format defaultFormat = "simple".equals(format) ? Grouping.Format.Simple : Grouping.Format.Grouped; // groupSort defaults to sort Sort groupSort = groupSortStr == null ? cmd.getSort() : QueryParsing.parseSort(groupSortStr, req); @@ -345,10 +347,15 @@ public class QueryComponent extends SearchComponent gc.groupOffset = groupOffsetDefault; gc.offset = cmd.getOffset(); gc.sort = cmd.getSort(); + gc.format = defaultFormat; if (main) { gc.main = true; + gc.format = Grouping.Format.Simple; main = false; + } + + if (gc.format == Grouping.Format.Simple) { gc.groupOffset = 0; // doesn't make sense } @@ -372,9 +379,14 @@ public class QueryComponent extends SearchComponent gc.offset = cmd.getOffset(); gc.numGroups = limitDefault; + gc.format = defaultFormat; + if (main) { gc.main = true; + gc.format = Grouping.Format.Simple; main = false; + } + if (gc.format == Grouping.Format.Simple) { gc.docsPerGroup = gc.numGroups; // doesn't make sense to limit to one gc.groupOffset = gc.offset; } diff --git a/solr/src/java/org/apache/solr/search/Grouping.java b/solr/src/java/org/apache/solr/search/Grouping.java index 27291b9adde..a4d119787a8 100755 --- a/solr/src/java/org/apache/solr/search/Grouping.java +++ b/solr/src/java/org/apache/solr/search/Grouping.java @@ -70,7 +70,7 @@ public class Grouping { int max = collector.getTotalHits(); int off = groupOffset; int len = docsPerGroup; - if (main) { + if (format == Format.Simple) { off = offset; len = numGroups; } @@ -181,8 +181,8 @@ public class Grouping { int docsToCollect = getMax(groupOffset, docsPerGroup, maxDoc); docsToCollect = Math.max(docsToCollect, 1); - // if this for the main result, don't skip groups (since we are counting docs, not groups) - int collectorOffset = main ? 0 : offset; + // if the format is simple, don't skip groups (since we are counting docs, not groups) + int collectorOffset = format==Format.Simple ? 0 : offset; if (groupBy instanceof StrFieldSource) { collector2 = new Phase2StringGroupCollector(collector, groupBy, context, groupSort, docsToCollect, needScores, collectorOffset); @@ -195,12 +195,17 @@ public class Grouping { @Override void finish() throws IOException { if (main) { - createMainResponse(); + mainResult = createSimpleResponse(); return; } NamedList groupResult = commonResponse(); + if (format == Format.Simple) { + groupResult.add("doclist", createSimpleResponse()); + return; + } + List groupList = new ArrayList(); groupResult.add("groups", groupList); // grouped={ key={ groups=[ @@ -225,7 +230,7 @@ public class Grouping { } } - private void createMainResponse() { + private DocList createSimpleResponse() { int docCount = numGroups; int docOffset = offset; int docsToGather = getMax(docOffset, docCount, maxDoc); @@ -273,7 +278,7 @@ public class Grouping { idSet.add(iter.nextDoc()); } - mainResult = docs; + return docs; } @Override diff --git a/solr/src/test/org/apache/solr/TestGroupingSearch.java b/solr/src/test/org/apache/solr/TestGroupingSearch.java index d094f990b83..f095d41863e 100644 --- a/solr/src/test/org/apache/solr/TestGroupingSearch.java +++ b/solr/src/test/org/apache/solr/TestGroupingSearch.java @@ -344,6 +344,12 @@ public class TestGroupingSearch extends SolrTestCaseJ4 { assertJQ(req("fq",filt, "q","{!func}"+f2, "group","true", "group.field",f, "fl","id", "rows","3", "start","1", "group.limit","2", "group.main","true") ,"/response=={'numFound':10,'start':1,'docs':[{'id':'10'},{'id':'3'},{'id':'6'}]}" ); + + ///////////////////////// group.format == simple + assertJQ(req("fq",filt, "q","{!func}"+f2, "group","true", "group.field",f, "fl","id", "rows","3", "start","1", "group.limit","2", "group.format","simple") + , "/grouped/foo_i=={'matches':10,'doclist':" + +"{'numFound':10,'start':1,'docs':[{'id':'10'},{'id':'3'},{'id':'6'}]}}" + ); };