zoukankan      html  css  js  c++  java
  • 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的学习使用系列差不多就这些了,其实也没啥东西,根本没有深入进去,如果深入的话,估计得花几个月时间,目前先了解下基本原理和基本使用方法,能够应付目前项目中的问题就行,当然,要真正掌握还需时间,冰冻三尺非一日之寒嘛,需要时日,有时间再来玩一玩。
      在路上……
  • 相关阅读:
    error C4430: 缺少类型说明符
    Fiddler 教程
    make: Nothing to be done for 'first'
    Qt Creator + MinGW 在windows 下的调试GDB停止工作解决
    WIN7成功安装Qt4.8方法,无需VS支持
    深入研究 UCenter API For .NET
    C#在Winform程序中显示QQ在线状态
    VS2010 需要缺少的web组件才能加载该项目
    System.Runtime.InteropServices.COMException: 检索 COM 类工厂中 CLSID 为 {0002E510-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154
    c#while循环注意continue的地方
  • 原文地址:https://www.cnblogs.com/ontheroad_lee/p/3526385.html
Copyright © 2011-2022 走看看