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:[1360000 TO 13700000]  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;    
    }

 

posted @ 2014-01-19 23:59  OnTheRoad_Lee  阅读(565)  评论(0编辑  收藏  举报