public List SearchByGroup(String sServer, String sQuery, String sSortFld, boolean bAsc,
String sPage, String sRows, String sFields, String sShards)
{
List _resultList = new ArrayList(); //返回结果列表,每个结果存放到一个hashmap中
String[] _arrsFields = sFields.split(","); //字段列表
int iSeriesNum = 0; //页面显示的自然增长序号
this.pageSize = Integer.parseInt(sRows);
String sStart = (Integer.parseInt(sPage) - 1) * Integer.parseInt(sRows) + "";
SolrQuery _query = new SolrQuery();
_query.setQuery(sQuery);
_query.setParam("start", sStart);
_query.setParam("rows", sRows);
_query.setParam("group", true);
_query.setParam("group.field", "FID"); //按照FID同族专利去重
_query.setParam("group.ngroups", true);
if (sSortFld != null && !"".equalsIgnoreCase(sSortFld))
{
if (bAsc)
{
_query.setSortField(sSortFld, SolrQuery.ORDER.asc);
}
else
{
_query.setSortField(sSortFld, SolrQuery.ORDER.desc);
}
}
if (sShards != null)
{
// _query.setParam("shards", sShards);
}
if (sFields != null)
{
_query.setParam("fl", sFields);
}
CommonsHttpSolrServer commonsHttpSolrServer = null;
try
{
commonsHttpSolrServer = new CommonsHttpSolrServer(sServer);
commonsHttpSolrServer.setRequestWriter(new BinaryRequestWriter());
QueryResponse _solrRes = commonsHttpSolrServer.query(_query);
GroupResponse _solrGroupRes = _solrRes.getGroupResponse();
List<GroupCommand> _listGroup = _solrGroupRes.getValues();
if (_listGroup == null || _listGroup.size() == 0) //检索到group为null 或者size 为0
{
this.lTotal = 0;
}
else
{
System.out.println(_listGroup.size());
Integer _iSize = _listGroup.get(0).getNGroups();
this.lTotal = _iSize.intValue();
System.out.println("hits:" + _iSize.intValue());
List<Group> _groups = _listGroup.get(0).getValues();
//System.out.println("group size:" + _groups.size());
for(int i=0;i<_groups.size();i++) //对返回的每个组进行循环,组的size为Page分页大小
{
Group _g = _groups.get(i);
SolrDocumentList _solrResult = null;
_solrResult = _g.getResult();
// System.out.println("group include hit:" + _solrResult.size());
for(int j=0;j<_solrResult.size();j++) //对每个组内的记录进行循环,默认组内记录数为1,可由group.limit参考控制
{
Map _map = new HashMap(); //存放记录
SolrDocument _doc = _solrResult.get(j);
iSeriesNum = iSeriesNum + 1 ; //页面显示的自然增长序号
_map.put("num", new Integer(iSeriesNum + new Integer(sStart).intValue()).toString());
for(int k=0;k<_arrsFields.length;k++) //对记录的每个字段循环
{
String sValue = _doc.getFieldValue(_arrsFields[k]).toString();
if (sValue != null)
{
_map.put(_arrsFields[k], sValue);
}
else
{
continue;
}
}
_resultList.add(_map);
}
_solrResult = null;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SolrServerException e)
{
e.printStackTrace();
}
return _resultList;
}