elasticsearch聚合统计出现次数

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(); } }


    推荐阅读