solr中facet和group的使用

来源:互联网 发布:折800官网9.9包邮淘宝 编辑:程序博客网 时间:2024/06/06 01:32

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);        }      }  }}




0 0
原创粉丝点击