solr中进行分组统计的应用例子(facet 和 group同时使用):
public class solr {
String url = "";
SolrServer server = server = new HttpSolrServer(url);
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setParam(GroupParams.GROUP, true);
query.setParam(GroupParams.GROUP_FIELD, "brandId");
query.set("facet", "on");
query.set("facet.field","category");
query.set("facet.mincount", "1");
query.set("facet.limit", "15");
QueryResponse response = server.query(query);
FacetField categoryField = response.getFacetField("category");
if (navFirstfacetField != null) {
counts = categoryField.getValues();
if (counts != null) {
for (Count count : counts) {
System.out.println("分类名称:"+count.getName()+" 分类商品数量:"+count.getCount());
}
}
}
GroupResponse groupResponse = response.getGroupResponse();
if(groupResponse != null) {
List<GroupCommand> groupList = groupResponse.getValues();
for(GroupCommand groupCommand : groupList) {
total = groupCommand.getMatches();
List<Group> groups = groupCommand.getValues();
for(Group group : groups) {
String brandId = group.getGroupValue();
int brandNum = group.getResult().getNumFound();
String brandName = null;
SolrDocumentList docList = group.getResult();
for (SolrDocument doc : docList) {
brandName = StringUtil.convertStr(doc.getFieldValue("brandName"));
}
System.out.println("brandId:"+brandId+" brandNum:"+brandNum+" brandName:"+brandName);
}
}
}
}