Apache Solr open-source search software
SOLR-5634: SolrJ GroupCommand.getNGroups returns null if group.format=simple and group.ngroups=true
Shalin Shekhar Mangar7b7a1b559238
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex e1c0921df92..2e77552572f 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -247,6 +247,9 @@ Bug Fixes* SOLR-5676: SolrCloud updates rejected if talking to secure ZooKeeper.(Greg Chanan via Mark Miller)+* SOLR-5634: SolrJ GroupCommand.getNGroups returns null if group.format=simple+ and group.ngroups=true. (Artem Lukanin via shalin)+Optimizations----------------------diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.javaindex a96b606f3ab..822fd6b1d58 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/response/QueryResponse.java@@ -223,7 +223,13 @@ public class QueryResponse extends SolrResponseBase_groupResponse.add(groupedCommand);} else if (queryCommand != null) {Integer iMatches = (Integer) oMatches;- GroupCommand groupCommand = new GroupCommand(fieldName, iMatches);+ GroupCommand groupCommand;+ if (oNGroups != null) {+ Integer iNGroups = (Integer) oNGroups;+ groupCommand = new GroupCommand(fieldName, iMatches, iNGroups);+ } else {+ groupCommand = new GroupCommand(fieldName, iMatches);+ }SolrDocumentList docList = (SolrDocumentList) queryCommand;groupCommand.add(new Group(fieldName, docList));_groupResponse.add(groupCommand);diff --git a/solr/solrj/src/test-files/solrj/sampleSimpleGroupResponse.xml b/solr/solrj/src/test-files/solrj/sampleSimpleGroupResponse.xmlnew file mode 100644index 00000000000..78a9d38d819--- /dev/null+++ b/solr/solrj/src/test-files/solrj/sampleSimpleGroupResponse.xml@@ -0,0 +1,101 @@+<?xml version="1.0" encoding="UTF-8"?>+<!--+ 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.+ -->++<response>+ <lst name="responseHeader">+ <int name="status">0</int>+ <int name="QTime">3451</int>+ <lst name="params">+ <str name="q">*:*</str>+ <str name="group.limit">1</str>+ <str name="group.field">acco_id</str>+ <str name="group.format">simple</str>+ <str name="group">true</str>+ <str name="group.ngroups">true</str>+ </lst>+ </lst>+ <lst name="grouped">+ <lst name="acco_id">+ <int name="matches">30000000</int>+ <int name="ngroups">5687</int>+ <result name="doclist" numFound="30000000" start="0">+ <doc>+ <int name="id">0</int>+ <str name="country">EG</str>+ <str name="acco_id">116_AR</str>+ <int name="price">417500</int>+ </doc>+ <doc>+ <int name="id">2236</int>+ <str name="country">EG</str>+ <str name="acco_id">116_HI</str>+ <int name="price">475300</int>+ </doc>+ <doc>+ <int name="id">4470</int>+ <str name="country">ES</str>+ <str name="acco_id">953_AR</str>+ <int name="price">559600</int>+ </doc>+ <doc>+ <int name="id">5490</int>+ <str name="country">ES</str>+ <str name="acco_id">953_HI</str>+ <int name="price">594600</int>+ </doc>+ <doc>+ <int name="id">6520</int>+ <str name="country">EG</str>+ <str name="acco_id">954_AR</str>+ <int name="price">425100</int>+ </doc>+ <doc>+ <int name="id">8756</int>+ <str name="country">EG</str>+ <str name="acco_id">954_HI</str>+ <int name="price">444400</int>+ </doc>+ <doc>+ <int name="id">10990</int>+ <str name="country">ES</str>+ <str name="acco_id">546_AR</str>+ <int name="price">1314300</int>+ </doc>+ <doc>+ <int name="id">15974</int>+ <str name="country">ES</str>+ <str name="acco_id">546_HI</str>+ <int name="price">1243500</int>+ </doc>+ <doc>+ <int name="id">20958</int>+ <str name="country">ES</str>+ <str name="acco_id">708_AR</str>+ <int name="price">1515300</int>+ </doc>+ <doc>+ <int name="id">25585</int>+ <str name="country">ES</str>+ <str name="acco_id">708_HI</str>+ <int name="price">1449700</int>+ </doc>+ </result>+ </lst>+ </lst>+</response>+diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.javaindex 53f15c86412..1c1b70a9e90 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseTest.java@@ -20,6 +20,7 @@ package org.apache.solr.client.solrj.response;import junit.framework.Assert;import org.apache.lucene.util.LuceneTestCase;import org.apache.solr.client.solrj.impl.XMLResponseParser;+import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.util.DateUtil;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrResourceLoader;@@ -219,4 +220,45 @@ public class QueryResponseTest extends LuceneTestCase {assertEquals(57074, queryCommandGroups.get(0).getResult().getNumFound());}+ @Test+ public void testSimpleGroupResponse() throws Exception {+ XMLResponseParser parser = new XMLResponseParser();+ InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleSimpleGroupResponse.xml");+ assertNotNull(is);+ Reader in = new InputStreamReader(is, "UTF-8");+ NamedList<Object> response = parser.processResponse(in);+ in.close();++ QueryResponse qr = new QueryResponse(response, null);+ assertNotNull(qr);+ GroupResponse groupResponse = qr.getGroupResponse();+ assertNotNull(groupResponse);+ List<GroupCommand> commands = groupResponse.getValues();+ assertNotNull(commands);+ assertEquals(1, commands.size());++ GroupCommand fieldCommand = commands.get(0);+ assertEquals("acco_id", fieldCommand.getName());+ assertEquals(30000000, fieldCommand.getMatches());+ assertEquals(5687, fieldCommand.getNGroups().intValue());+ List<Group> fieldCommandGroups = fieldCommand.getValues();+ assertEquals(1, fieldCommandGroups.size());++ assertEquals("acco_id", fieldCommandGroups.get(0).getGroupValue());+ SolrDocumentList documents = fieldCommandGroups.get(0).getResult();+ assertNotNull(documents);++ assertEquals(10, documents.size());+ assertEquals("116_AR", documents.get(0).getFieldValue("acco_id"));+ assertEquals("116_HI", documents.get(1).getFieldValue("acco_id"));+ assertEquals("953_AR", documents.get(2).getFieldValue("acco_id"));+ assertEquals("953_HI", documents.get(3).getFieldValue("acco_id"));+ assertEquals("954_AR", documents.get(4).getFieldValue("acco_id"));+ assertEquals("954_HI", documents.get(5).getFieldValue("acco_id"));+ assertEquals("546_AR", documents.get(6).getFieldValue("acco_id"));+ assertEquals("546_HI", documents.get(7).getFieldValue("acco_id"));+ assertEquals("708_AR", documents.get(8).getFieldValue("acco_id"));+ assertEquals("708_HI", documents.get(9).getFieldValue("acco_id"));+ }+}