elasticsearch 집합 통계 출현 횟수

2145 단어
public void getResult() {
        String beanName = "ppcOnlineEs";
        int size = 1000000;
        EsIndexClientBean esIndexClientBean = (EsIndexClientBean) context.getBean(beanName);
        TransportClient transportClient = esIndexClientBean.getTransportClient();
        org.elasticsearch.index.query.RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("createTime");
        rangeQueryBuilder.includeUpper(true).includeLower(true).from("2017-06-06 00:00:00").to("2017-11-06 00:00:00");
        SearchResponse sr = transportClient.prepareSearch("videonew").setTypes("videonew").setQuery(rangeQueryBuilder).setSize(0)
                .addAggregation(
                        // displayName.displayName , 
                        AggregationBuilders.terms("by_displayName").field("displayName.displayName").order(Terms.Order.count(false)).size(size)).execute().actionGet();
        Map aggregationMap = sr.getAggregations().getAsMap();
        // 
        Aggregation agg = aggregationMap.get("by_displayName");
        String outfileName = "C:\\ .txt";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outfileName))));
            int count = 0;
            for (Terms.Bucket entry : ((StringTerms) agg).getBuckets()) {
                count++;
                String key = entry.getKeyAsString();            // bucket key
                long docCount = entry.getDocCount();            // Doc count
                if (docCount < 2) {
                    break;
                }
                StringBuilder sb = new StringBuilder();
                sb.append( key + "\t" + docCount);
                bw.write(sb.toString());
                bw.newLine();
                if(count%1000==0)
                {
                    bw.flush();
                }

            }
            bw.flush();
            bw.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

좋은 웹페이지 즐겨찾기