SolrCloud统计功能小记

因为需要在自己的搜索系统中增加一些统计分析的功能,而当前的数据及所有都是通过solrcloud本身存储,因此,必须使用solrcloud实现一些常见的统计功能。

下面介绍下自己使用solrcloud实现的几个统计小案例:


1、统计每个月存储的邮件数量:

实现代码:

                   QueryResponsequery = null;

                            CloudSolrServersolrcloud = EmailSolrCloudInstance.getInstance(ZkServerConfig.EmailCollection);

                  

                            ModifiableSolrParamsparams = newModifiableSolrParams(); 

                            params.set("shards.tolerant", true);

                            SolrQueryfilterQuery = new SolrQuery("type:email"); 

                       filterQuery.setFacet(true);

                       filterQuery.setParam("facet.date", "timestamp");

                       filterQuery.setParam("facet.date.start", "2013-02-01T00:00:00Z");

                       filterQuery.setParam("facet.date.end", "2014-02-01T00:00:00Z");

                       filterQuery.setParam("facet.date.gap", "+1MONTH");

                            params.add(filterQuery); 

                            query= solrcloud.query(params);

                            System.out.println("查询表达式:" +params.toString());

                            List<FacetField>facets = query.getFacetDates();

                            for (FacetField facet : facets) {

                            System.out.println(facet.getName());

                                     System.out.println("----------------");

                                     List<Count>counts = facet.getValues();

                                     for (Count count : counts) {

                                               System.out.println(count.getName() + ":" + count.getCount());

                                     }

                                     System.out.println();

                            }

2、统计聊天信息各类聊天信息的数量

List<FacetField> facets = null;

                   try {

                            CloudSolrServersolrcloud = EMLSolrCloudInstance.getInstance(ZkServerConfig.IMCollection);

 

                            SolrQueryquery = new SolrQuery();//建立一个新的查询

                            query.setQuery("type:msn OR type:qq OR type:icq OR type:gtalk");

                            query.setFacet(true);//设置facet=on

                            query.addFacetField(new String[] { "type"});//设置需要facet的字段

                            query.setFacetLimit(10);//限制facet返回的数量

                            QueryResponseresponse = solrcloud.query(query);

                             facets = response.getFacetFields();//返回的facet列表

                             System.out.println(facets);

                             for(int i=0;i<facets.size();i++){

                                      

                                      FacetField facetField = facets.get(i);

                                      List<Count> countValue = facetField.getValues();

                                      for(int j=0;j<countValue.size();j++){

                                                

                                                Count count = countValue.get(i);

                                                System.out.println("名称"+count.getName());

                                                System.out.println("统计结果"+count.getCount());

                                      }

                             }

3、统计每个分组中某个字段的和(比如统计淘宝每个月的交易量)

QueryResponse query = null;

                   List<FieldStatsInfo>list = null;

                            try {

                                     CloudSolrServersolrcloud = EMLSolrCloudInstance.getInstance(ZkServerConfig.BTAOBAOCollection);

 

                                     ModifiableSolrParamsparams = newModifiableSolrParams(); 

                                     params.set("shards.tolerant", true);

                                     //SolrQuery filterQuery = newSolrQuery("type:ALIBI");

                                     SolrQueryfilterQuery = newSolrQuery(queryStr);

                                     filterQuery.setFacet(true);

                                     //filterQuery.setParam("fq", "timestamp:[2013-02-01T00:00:00ZTO 2015-02-01T00:00:00Z]");

                                     filterQuery.setParam("fq", fq);

                                     filterQuery.setParam("stats", "on");

                                     filterQuery.setParam("stats.field", "price");

                                     filterQuery.setParam("stats.facet", "sendTime");

                                     params.add(filterQuery); 

                                     query= solrcloud.query(params);                     

                                     Map<String,FieldStatsInfo> info = query.getFieldStatsInfo();

                                     FieldStatsInfoinfo2 = info.get("price");

                                     Map<String,List<FieldStatsInfo>> facets = info2.getFacets();

                                     list= facets.get("sendTime");

                                     for(inti=0;i<list.size();i++){     

                                               FieldStatsInfofieldStatsInfo = list.get(i);

                                               System.out.println("交易结果"+fieldStatsInfo.getName()+":"+fieldStatsInfo.getSum());

                                              

                                     }



你可能感兴趣的:(大数据,搜索,solrCloud,统计功能,聚合计算)