Solr的学习使用之(九)facet.pivot实战

facet.pivot自己的理解,就是按照多个维度进行分组查询,以下是自己的实战代码,按照newsType,property两个维度统计:

public List<ReportNewsTypeDTO> queryNewsType(
            ReportQuery reportQuery) {    
        HttpSolrServer solrServer = SolrServer.getInstance().getServer();
        SolrQuery sQuery = new SolrQuery();
        List<ReportNewsTypeDTO> list = new ArrayList<ReportNewsTypeDTO>();
        try {
            String para = this.initReportQueryPara(reportQuery, 0);
            sQuery.setFacet(true);
            sQuery.add("facet.pivot", "newsType,property");//根据这两维度来分组查询
            sQuery.setQuery(para);
            QueryResponse response = solrServer.query(sQuery,SolrRequest.METHOD.POST);     
            NamedList<List<PivotField>> namedList = response.getFacetPivot();
            System.out.println(namedList);//底下为啥要这样判断,把这个值打印出来,你就明白了
            if(namedList != null){
                List<PivotField> pivotList = null;
                for(int i=0;i<namedList.size();i++){
                    pivotList = namedList.getVal(i);
                    if(pivotList != null){
                        ReportNewsTypeDTO dto = null;
                        for(PivotField pivot:pivotList){
                            dto = new ReportNewsTypeDTO();
                            dto.setNewsTypeId((Integer)pivot.getValue());
                            dto.setNewsTypeName(News.newsTypeMap.get((Integer)pivot.getValue()));
                            int pos = 0;
                            int neg = 0;
                            List<PivotField> fieldList = pivot.getPivot();
                            if(fieldList != null){
                                for(PivotField field:fieldList){
                                    int proValue = (Integer) field.getValue();
                                    int count = field.getCount();
                                    if(proValue == 1){
                                        pos = count;
                                    }else{
                                        neg = count;
                                    }
                                }
                            }
                            dto.setPositiveCount(pos);
                            dto.setNegativeCount(neg);
                            list.add(dto);
                        }
                    }
                }
            }

            return list;
        } catch (SolrServerException e) {
            log.error("查询solr失败", e);
            e.printStackTrace();
        } finally{
            solrServer.shutdown();
            solrServer = null;
        }
        return list;    
    }
namedList打印结果:
{newsType,property=
[
newsType:8 [4260] [property:1 [3698] null, property:0 [562] null],
newsType:1 [1507] [property:1 [1389] null, property:0 [118] null],
newsType:2 [1054] [property:1 [909] null, property:0 [145] null],
newsType:6 [715] [property:1 [581] null, property:0 [134] null],
newsType:4 [675] [property:1 [466] null, property:0 [209] null],
newsType:3 [486] [property:1 [397] null, property:0 [89] null],
newsType:7 [458] [property:1 [395] null, property:0 [63] null],
newsType:5 [289] [property:1 [263] null, property:0 [26] null],
newsType:9 [143] [property:1 [138] null, property:0 [5] null]
]
}
这下应该明白了。写到这里,突然想到一个,所有的分组查询统计,不管是一个维度两个维度都可以使用face.pivot来统计,不错的东东。

  好了,Solr的学习使用系列差不多就这些了,其实也没啥东西,根本没有深入进去,如果深入的话,估计得花几个月时间,目前先了解下基本原理和基本使用方法,能够应付目前项目中的问题就行,当然,要真正掌握还需时间,冰冻三尺非一日之寒嘛,需要时日,有时间再来玩一玩。
  在路上……
posted @ 2014-01-20 00:24  OnTheRoad_Lee  阅读(2439)  评论(0编辑  收藏  举报