edcf 发表于 2014-1-20 08:45:01

Solr的学习使用之(八)facet实战

以下为统计media的数量功能,通过solrj,采用了facet方法,类似于sql的分组group by查询;这边的代码只是获取总媒体数量,其实他还有其他功能,去循环solrList的话,可以获取各个媒体的数量


public int getMediaCount(Keyword keyword) {   
      HttpSolrServer solrServer = SolrServer.getInstance().getServer();
      SolrQuery sQuery = new SolrQuery();
      int result = 0;
      try {
            String para = this.initKeywordQueryPara(keyword);//这边的值大概是酱紫的 "* AND publishTime:AND contentStr:\"关键字\"";
            sQuery.setFacet(true);
            sQuery.setFacetMinCount(1);
            sQuery.addFacetField("channelName");
            sQuery.setQuery(para);
            QueryResponse response = solrServer.query(sQuery,SolrRequest.METHOD.POST);            
            List<Count> solrList = response.getFacetField("channelName").getValues();
            result = solrList.size();
            return result;
      } catch (SolrServerException e) {
            log.error("查询solr失败", e);
            e.printStackTrace();
      } finally{
            solrServer.shutdown();
            solrServer = null;
      }

      return result;   
    }


页: [1]
查看完整版本: Solr的学习使用之(八)facet实战