SOLR-2637: Added support for group result parsing in SolrJ

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1152568 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Martijn van Groningen 2011-07-31 11:52:59 +00:00
parent 9fd0af75ff
commit 865098ae7e
8 changed files with 833 additions and 24 deletions

View File

@ -342,9 +342,12 @@ New Features
* SOLR-2523: Added support in SolrJ to easily interact with range facets. * SOLR-2523: Added support in SolrJ to easily interact with range facets.
The range facet response can be parsed and is retrievable from the The range facet response can be parsed and is retrievable from the
QueryResponse class. The SolrQuery has convenient methods for using QueryResponse class. The SolrQuery class has convenient methods for using
range facets. (Martijn van Groningen) range facets. (Martijn van Groningen)
* SOLR-2637: Added support for group result parsing in SolrJ.
(Tao Cheng, Martijn van Groningen)
Optimizations Optimizations
---------------------- ----------------------

View File

@ -17,23 +17,7 @@
package org.apache.solr.client.solrj.impl; package org.apache.solr.client.solrj.impl;
import java.io.Reader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.solr.client.solrj.ResponseParser; import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
@ -41,6 +25,19 @@ import org.apache.solr.common.util.DateUtil;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.XMLErrorLogger; import org.apache.solr.common.util.XMLErrorLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/** /**
* *
@ -306,10 +303,10 @@ public class XMLResponseParser extends ResponseParser
if( !type.isLeaf ) { if( !type.isLeaf ) {
switch( type ) { switch( type ) {
case LST: vals.add( readNamedList( parser ) ); continue; case LST: vals.add( readNamedList( parser ) ); depth--; continue;
case ARR: vals.add( readArray( parser ) ); continue; case ARR: vals.add( readArray( parser ) ); depth--; continue;
case RESULT: vals.add( readDocuments( parser ) ); continue; case RESULT: vals.add( readDocuments( parser ) ); depth--; continue;
case DOC: vals.add( readDocument( parser ) ); continue; case DOC: vals.add( readDocument( parser ) ); depth--; continue;
} }
throw new XMLStreamException( "branch element not handled!", parser.getLocation() ); throw new XMLStreamException( "branch element not handled!", parser.getLocation() );
} }

View File

@ -0,0 +1,69 @@
package org.apache.solr.client.solrj.response;
/*
* 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.solr.common.SolrDocumentList;
import java.io.Serializable;
/**
* Represents a group. A group contains a common group value that all documents inside the group share and
* documents that belong to this group.
*
* A group value can be a field value, function result or a query string depending on the {@link GroupCommand}.
* In case of a field value or a function result the value is always a indexed value.
*
* @since solr 3.4
*/
public class Group implements Serializable {
private final String _groupValue;
private final SolrDocumentList _result;
/**
* Creates a Group instance.
*
* @param groupValue The common group value (indexed value) that all documents share.
* @param result The documents to be displayed that belong to this group
*/
public Group(String groupValue, SolrDocumentList result) {
_groupValue = groupValue;
_result = result;
}
/**
* Returns the common group value that all documents share inside this group.
* This is an indexed value, not a stored value.
*
* @return the common group value
*/
public String getGroupValue() {
return _groupValue;
}
/**
* Returns the documents to be displayed that belong to this group.
* How many documents are returned depend on the <code>group.offset</code> and <code>group.limit</code> parameters.
*
* @return the documents to be displayed that belong to this group
*/
public SolrDocumentList getResult() {
return _result;
}
}

View File

@ -0,0 +1,125 @@
package org.apache.solr.client.solrj.response;
/*
* 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 java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents the result of a group command.
* This can be the result of the following parameter:
* <ul>
* <li> group.field
* <li> group.func
* <li> group.query
* </ul>
*
* An instance of this class contains:
* <ul>
* <li> The name of this command. This can be the field, function or query grouped by.
* <li> The total number of documents that have matched.
* <li> The total number of groups that have matched.
* <li> The groups to be displayed. Depending on the start and rows parameter.
* </ul>
*
* In case of <code>group.query</code> only one group is present and ngroups is always <code>null</code>.
*
* @since solr 3.4
*/
public class GroupCommand implements Serializable {
private final String _name;
private final List<Group> _values = new ArrayList<Group>();
private final int _matches;
private final Integer _ngroups;
/**
* Creates a GroupCommand instance
*
* @param name The name of this command
* @param matches The total number of documents found for this command
*/
public GroupCommand(String name, int matches) {
_name = name;
_matches = matches;
_ngroups = null;
}
/**
* Creates a GroupCommand instance.
*
* @param name The name of this command
* @param matches The total number of documents found for this command
* @param nGroups The total number of groups found for this command.
*/
public GroupCommand(String name, int matches, int nGroups) {
_name = name;
_matches = matches;
_ngroups = nGroups;
}
/**
* Returns the name of this command. This can be the field, function or query grouped by.
*
* @return the name of this command
*/
public String getName() {
return _name;
}
/**
* Adds a group to this command.
*
* @param group A group to be added
*/
public void add(Group group) {
_values.add(group);
}
/**
* Returns the groups to be displayed.
* The number of groups returned depend on the <code>start</code> and <code>rows</code> parameters.
*
* @return the groups to be displayed.
*/
public List<Group> getValues() {
return _values;
}
/**
* Returns the total number of documents found for this command.
*
* @return the total number of documents found for this command.
*/
public int getMatches() {
return _matches;
}
/**
* Returns the total number of groups found for this command.
* Returns <code>null</code> if the <code>group.ngroups</code> parameter is unset or <code>false</code> or
* if this is a group command query (parameter = <code>group.query</code>).
*
* @return the total number of groups found for this command.
*/
public Integer getNGroups() {
return _ngroups;
}
}

View File

@ -0,0 +1,56 @@
package org.apache.solr.client.solrj.response;
/*
* 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 java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Overall grouping result. Contains a list of {@link GroupCommand} instances that is the result of
* one the following parameters:
* <ul>
* <li>group.field
* <li>group.func
* <li>group.query
* </ul>
*
* @since solr 3.4
*/
public class GroupResponse implements Serializable {
private final List<GroupCommand> _values = new ArrayList<GroupCommand>();
/**
* Adds a grouping command to the response.
*
* @param command The grouping command to add
*/
public void add(GroupCommand command) {
_values.add(command);
}
/**
* Returns all grouping commands.
*
* @return all grouping commands
*/
public List<GroupCommand> getValues() {
return _values;
}
}

View File

@ -21,6 +21,7 @@ import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder; import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import java.util.*; import java.util.*;
@ -43,6 +44,10 @@ public class QueryResponse extends SolrResponseBase
private NamedList<Object> _statsInfo = null; private NamedList<Object> _statsInfo = null;
private NamedList<NamedList<Number>> _termsInfo = null; private NamedList<NamedList<Number>> _termsInfo = null;
// Grouping response
private NamedList<Object> _groupedInfo = null;
private GroupResponse _groupResponse = null;
// Facet stuff // Facet stuff
private Map<String,Integer> _facetQuery = null; private Map<String,Integer> _facetQuery = null;
private List<FacetField> _facetFields = null; private List<FacetField> _facetFields = null;
@ -107,6 +112,10 @@ public class QueryResponse extends SolrResponseBase
else if( "debug".equals( n ) ) { else if( "debug".equals( n ) ) {
_debugInfo = (NamedList<Object>) res.getVal( i ); _debugInfo = (NamedList<Object>) res.getVal( i );
extractDebugInfo( _debugInfo ); extractDebugInfo( _debugInfo );
}
else if( "grouped".equals( n ) ) {
_groupedInfo = (NamedList<Object>) res.getVal( i );
extractGroupedInfo( _groupedInfo );
} }
else if( "highlighting".equals( n ) ) { else if( "highlighting".equals( n ) ) {
_highlightingInfo = (NamedList<Object>) res.getVal( i ); _highlightingInfo = (NamedList<Object>) res.getVal( i );
@ -170,6 +179,54 @@ public class QueryResponse extends SolrResponseBase
} }
} }
private void extractGroupedInfo( NamedList<Object> info ) {
if ( info != null ) {
_groupResponse = new GroupResponse();
int size = info.size();
for (int i=0; i < size; i++) {
String fieldName = info.getName(i);
Object fieldGroups = info.getVal(i);
SimpleOrderedMap<Object> simpleOrderedMap = (SimpleOrderedMap<Object>) fieldGroups;
Object oMatches = simpleOrderedMap.get("matches");
Object oNGroups = simpleOrderedMap.get("ngroups");
Object oGroups = simpleOrderedMap.get("groups");
Object queryCommand = simpleOrderedMap.get("doclist");
if (oMatches == null) {
continue;
}
if (oGroups != null) {
Integer iMatches = (Integer) oMatches;
ArrayList<Object> groupsArr = (ArrayList<Object>) oGroups;
GroupCommand groupedCommand;
if (oNGroups != null) {
Integer iNGroups = (Integer) oNGroups;
groupedCommand = new GroupCommand(fieldName, iMatches, iNGroups);
} else {
groupedCommand = new GroupCommand(fieldName, iMatches);
}
for (Object oGrp : groupsArr) {
SimpleOrderedMap grpMap = (SimpleOrderedMap) oGrp;
Object sGroupValue = grpMap.get( "groupValue");
SolrDocumentList doclist = (SolrDocumentList) grpMap.get( "doclist");
Group group = new Group(sGroupValue.toString(), doclist) ;
groupedCommand.add(group);
}
_groupResponse.add(groupedCommand);
} else if (queryCommand != null) {
Integer iMatches = (Integer) oMatches;
GroupCommand groupCommand = new GroupCommand(fieldName, iMatches);
SolrDocumentList docList = (SolrDocumentList) queryCommand;
groupCommand.add(new Group(fieldName, docList));
_groupResponse.add(groupCommand);
}
}
}
}
private void extractHighlightingInfo( NamedList<Object> info ) private void extractHighlightingInfo( NamedList<Object> info )
{ {
_highlighting = new HashMap<String,Map<String,List<String>>>(); _highlighting = new HashMap<String,Map<String,List<String>>>();
@ -332,6 +389,21 @@ public class QueryResponse extends SolrResponseBase
return _facetQuery; return _facetQuery;
} }
/**
* Returns the {@link GroupResponse} containing the group commands.
* A group command can be the result of one of the following parameters:
* <ul>
* <li>group.field
* <li>group.func
* <li>group.query
* </ul>
*
* @return the {@link GroupResponse} containing the group commands
*/
public GroupResponse getGroupResponse() {
return _groupResponse;
}
public Map<String, Map<String, List<String>>> getHighlighting() { public Map<String, Map<String, List<String>>> getHighlighting() {
return _highlighting; return _highlighting;
} }

View File

@ -0,0 +1,384 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">2</str>
<str name="group.field">acco_id</str>
<str name="group.query">country:fr</str>
<str name="group">true</str>
</lst>
</lst>
<lst name="grouped">
<lst name="acco_id">
<int name="matches">30000000</int>
<int name="ngroups">5687</int>
<arr name="groups">
<lst>
<str name="groupValue">116_ar</str>
<result name="doclist" numFound="2236" 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">1</int>
<str name="country">EG</str>
<str name="acco_id">116_AR</str>
<int name="price">472500</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">116_hi</str>
<result name="doclist" numFound="2234" start="0">
<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">2237</int>
<str name="country">EG</str>
<str name="acco_id">116_HI</str>
<int name="price">475400</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">953_ar</str>
<result name="doclist" numFound="1020" start="0">
<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">4471</int>
<str name="country">ES</str>
<str name="acco_id">953_AR</str>
<int name="price">593600</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">953_hi</str>
<result name="doclist" numFound="1030" start="0">
<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">5491</int>
<str name="country">ES</str>
<str name="acco_id">953_HI</str>
<int name="price">595600</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">954_ar</str>
<result name="doclist" numFound="2236" start="0">
<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">6521</int>
<str name="country">EG</str>
<str name="acco_id">954_AR</str>
<int name="price">425200</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">954_hi</str>
<result name="doclist" numFound="2234" start="0">
<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">8757</int>
<str name="country">EG</str>
<str name="acco_id">954_HI</str>
<int name="price">444500</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">546_ar</str>
<result name="doclist" numFound="4984" start="0">
<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">10991</int>
<str name="country">ES</str>
<str name="acco_id">546_AR</str>
<int name="price">1314300</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">546_hi</str>
<result name="doclist" numFound="4984" start="0">
<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">15975</int>
<str name="country">ES</str>
<str name="acco_id">546_HI</str>
<int name="price">1242500</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">708_ar</str>
<result name="doclist" numFound="4627" start="0">
<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">20959</int>
<str name="country">ES</str>
<str name="acco_id">708_AR</str>
<int name="price">1515800</int>
</doc>
</result>
</lst>
<lst>
<str name="groupValue">708_hi</str>
<result name="doclist" numFound="4627" start="0">
<doc>
<int name="id">25585</int>
<str name="country">ES</str>
<str name="acco_id">708_HI</str>
<int name="price">1449700</int>
</doc>
<doc>
<int name="id">25586</int>
<str name="country">ES</str>
<str name="acco_id">708_HI</str>
<int name="price">1448700</int>
</doc>
</result>
</lst>
</arr>
</lst>
<lst name="sum(price, price)">
<int name="matches">30000000</int>
<arr name="groups">
<lst>
<float name="groupValue">95000.0</float>
<result name="doclist" numFound="43666" start="0">
<doc>
<int name="id">0</int>
<str name="country">EG</str>
<str name="acco_id">060116_AR</str>
<int name="price">47500</int>
</doc>
<doc>
<int name="id">1</int>
<str name="country">EG</str>
<str name="acco_id">600116_AR</str>
<int name="price">47500</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">91400.0</float>
<result name="doclist" numFound="27120" start="0">
<doc>
<int name="id">86</int>
<str name="country">EG</str>
<str name="acco_id">600116_AR</str>
<int name="price">45700</int>
</doc>
<doc>
<int name="id">87</int>
<str name="country">EG</str>
<str name="acco_id">600116_AR</str>
<int name="price">45700</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">104800.0</float>
<result name="doclist" numFound="34579" start="0">
<doc>
<int name="id">172</int>
<str name="country">EG</str>
<str name="acco_id">600116_AR</str>
<int name="price">52400</int>
</doc>
<doc>
<int name="id">173</int>
<str name="country">EG</str>
<str name="acco_id">001166_AR</str>
<int name="price">52400</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">99400.0</float>
<result name="doclist" numFound="40519" start="0">
<doc>
<int name="id">217</int>
<str name="country">EG</str>
<str name="acco_id">001164_AR</str>
<int name="price">49700</int>
</doc>
<doc>
<int name="id">218</int>
<str name="country">EG</str>
<str name="acco_id">040116_AR</str>
<int name="price">49700</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">109600.0</float>
<result name="doclist" numFound="36203" start="0">
<doc>
<int name="id">262</int>
<str name="country">EG</str>
<str name="acco_id">004116_AR</str>
<int name="price">54800</int>
</doc>
<doc>
<int name="id">263</int>
<str name="country">EG</str>
<str name="acco_id">040116_AR</str>
<int name="price">54800</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">102400.0</float>
<result name="doclist" numFound="37852" start="0">
<doc>
<int name="id">307</int>
<str name="country">EG</str>
<str name="acco_id">004116_AR</str>
<int name="price">51200</int>
</doc>
<doc>
<int name="id">308</int>
<str name="country">EG</str>
<str name="acco_id">001416_AR</str>
<int name="price">51200</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">116800.0</float>
<result name="doclist" numFound="40393" start="0">
<doc>
<int name="id">352</int>
<str name="country">EG</str>
<str name="acco_id">001416_AR</str>
<int name="price">58400</int>
</doc>
<doc>
<int name="id">353</int>
<str name="country">EG</str>
<str name="acco_id">004116_AR</str>
<int name="price">58400</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">107800.0</float>
<result name="doclist" numFound="41639" start="0">
<doc>
<int name="id">438</int>
<str name="country">EG</str>
<str name="acco_id">004116_AR</str>
<int name="price">53900</int>
</doc>
<doc>
<int name="id">439</int>
<str name="country">EG</str>
<str name="acco_id">040116_AR</str>
<int name="price">53900</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">136200.0</float>
<result name="doclist" numFound="25929" start="0">
<doc>
<int name="id">524</int>
<str name="country">EG</str>
<str name="acco_id">004116_AR</str>
<int name="price">68100</int>
</doc>
<doc>
<int name="id">525</int>
<str name="country">EG</str>
<str name="acco_id">0014416_AR</str>
<int name="price">68100</int>
</doc>
</result>
</lst>
<lst>
<float name="groupValue">131400.0</float>
<result name="doclist" numFound="29179" start="0">
<doc>
<int name="id">600</int>
<str name="country">EG</str>
<str name="acco_id">001164_AR</str>
<int name="price">65700</int>
</doc>
<doc>
<int name="id">601</int>
<str name="country">EG</str>
<str name="acco_id">001416_AR</str>
<int name="price">65700</int>
</doc>
</result>
</lst>
</arr>
</lst>
<lst name="country:fr">
<int name="matches">30000000</int>
<result name="doclist" numFound="57074" start="0">
<doc>
<int name="id">68298</int>
<str name="country">FR</str>
<str name="acco_id">0426_OA</str>
<int name="price">1234070</int>
</doc>
<doc>
<int name="id">68299</int>
<str name="country">FR</str>
<str name="acco_id">0426_OA</str>
<int name="price">1234070</int>
</doc>
</result>
</lst>
</lst>
</response>

View File

@ -28,9 +28,10 @@ import org.junit.Test;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.util.List;
/** /**
* Simple test for Date facet support in QueryResponse * A few tests for parsing Solr response in QueryResponse
* *
* @since solr 1.3 * @since solr 1.3
*/ */
@ -113,4 +114,106 @@ public class QueryResponseTest extends LuceneTestCase {
assertEquals(0, manufacturedateDt.getCounts().get(2).getCount()); assertEquals(0, manufacturedateDt.getCounts().get(2).getCount());
} }
@Test
public void testGroupResponse() throws Exception {
XMLResponseParser parser = new XMLResponseParser();
InputStream is = new SolrResourceLoader(null, null).openResource("solrj/sampleGroupResponse.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(3, 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(10, fieldCommandGroups.size());
assertEquals("116_ar", fieldCommandGroups.get(0).getGroupValue());
assertEquals(2, fieldCommandGroups.get(0).getResult().size());
assertEquals(2236, fieldCommandGroups.get(0).getResult().getNumFound());
assertEquals("116_hi", fieldCommandGroups.get(1).getGroupValue());
assertEquals(2, fieldCommandGroups.get(1).getResult().size());
assertEquals(2234, fieldCommandGroups.get(1).getResult().getNumFound());
assertEquals("953_ar", fieldCommandGroups.get(2).getGroupValue());
assertEquals(2, fieldCommandGroups.get(2).getResult().size());
assertEquals(1020, fieldCommandGroups.get(2).getResult().getNumFound());
assertEquals("953_hi", fieldCommandGroups.get(3).getGroupValue());
assertEquals(2, fieldCommandGroups.get(3).getResult().size());
assertEquals(1030, fieldCommandGroups.get(3).getResult().getNumFound());
assertEquals("954_ar", fieldCommandGroups.get(4).getGroupValue());
assertEquals(2, fieldCommandGroups.get(4).getResult().size());
assertEquals(2236, fieldCommandGroups.get(4).getResult().getNumFound());
assertEquals("954_hi", fieldCommandGroups.get(5).getGroupValue());
assertEquals(2, fieldCommandGroups.get(5).getResult().size());
assertEquals(2234, fieldCommandGroups.get(5).getResult().getNumFound());
assertEquals("546_ar", fieldCommandGroups.get(6).getGroupValue());
assertEquals(2, fieldCommandGroups.get(6).getResult().size());
assertEquals(4984, fieldCommandGroups.get(6).getResult().getNumFound());
assertEquals("546_hi", fieldCommandGroups.get(7).getGroupValue());
assertEquals(2, fieldCommandGroups.get(7).getResult().size());
assertEquals(4984, fieldCommandGroups.get(7).getResult().getNumFound());
assertEquals("708_ar", fieldCommandGroups.get(8).getGroupValue());
assertEquals(2, fieldCommandGroups.get(8).getResult().size());
assertEquals(4627, fieldCommandGroups.get(8).getResult().getNumFound());
assertEquals("708_hi", fieldCommandGroups.get(9).getGroupValue());
assertEquals(2, fieldCommandGroups.get(9).getResult().size());
assertEquals(4627, fieldCommandGroups.get(9).getResult().getNumFound());
GroupCommand funcCommand = commands.get(1);
assertEquals("sum(price, price)", funcCommand.getName());
assertEquals(30000000, funcCommand.getMatches());
assertNull(funcCommand.getNGroups());
List<Group> funcCommandGroups = funcCommand.getValues();
assertEquals(10, funcCommandGroups.size());
assertEquals("95000.0", funcCommandGroups.get(0).getGroupValue());
assertEquals(2, funcCommandGroups.get(0).getResult().size());
assertEquals(43666, funcCommandGroups.get(0).getResult().getNumFound());
assertEquals("91400.0", funcCommandGroups.get(1).getGroupValue());
assertEquals(2, funcCommandGroups.get(1).getResult().size());
assertEquals(27120, funcCommandGroups.get(1).getResult().getNumFound());
assertEquals("104800.0", funcCommandGroups.get(2).getGroupValue());
assertEquals(2, funcCommandGroups.get(2).getResult().size());
assertEquals(34579, funcCommandGroups.get(2).getResult().getNumFound());
assertEquals("99400.0", funcCommandGroups.get(3).getGroupValue());
assertEquals(2, funcCommandGroups.get(3).getResult().size());
assertEquals(40519, funcCommandGroups.get(3).getResult().getNumFound());
assertEquals("109600.0", funcCommandGroups.get(4).getGroupValue());
assertEquals(2, funcCommandGroups.get(4).getResult().size());
assertEquals(36203, funcCommandGroups.get(4).getResult().getNumFound());
assertEquals("102400.0", funcCommandGroups.get(5).getGroupValue());
assertEquals(2, funcCommandGroups.get(5).getResult().size());
assertEquals(37852, funcCommandGroups.get(5).getResult().getNumFound());
assertEquals("116800.0", funcCommandGroups.get(6).getGroupValue());
assertEquals(2, funcCommandGroups.get(6).getResult().size());
assertEquals(40393, funcCommandGroups.get(6).getResult().getNumFound());
assertEquals("107800.0", funcCommandGroups.get(7).getGroupValue());
assertEquals(2, funcCommandGroups.get(7).getResult().size());
assertEquals(41639, funcCommandGroups.get(7).getResult().getNumFound());
assertEquals("136200.0", funcCommandGroups.get(8).getGroupValue());
assertEquals(2, funcCommandGroups.get(8).getResult().size());
assertEquals(25929, funcCommandGroups.get(8).getResult().getNumFound());
assertEquals("131400.0", funcCommandGroups.get(9).getGroupValue());
assertEquals(2, funcCommandGroups.get(9).getResult().size());
assertEquals(29179, funcCommandGroups.get(9).getResult().getNumFound());
GroupCommand queryCommand = commands.get(2);
assertEquals("country:fr", queryCommand.getName());
assertNull(queryCommand.getNGroups());
assertEquals(30000000, queryCommand.getMatches());
List<Group> queryCommandGroups = queryCommand.getValues();
assertEquals(1, queryCommandGroups.size());
assertEquals("country:fr", queryCommandGroups.get(0).getGroupValue());
assertEquals(2, queryCommandGroups.get(0).getResult().size());
assertEquals(57074, queryCommandGroups.get(0).getResult().getNumFound());
}
} }