Elasticsearch聚合

 

聚合分词

http://localhost:9200/{index}/{type}/_search 

{
  "aggs": {
  "brands": {
     "terms": { "field": "goods.brandKey" }
   }
 }
}

NativeSearchQuery query =  new NativeSearchQueryBuilder()
                .withIndices("pdm").withTypes("sku")
                .addAggregation(AggregationBuilders.terms("names").field("name"))
                .build();
        Aggregations list = template.query(query, new ResultsExtractor<Aggregations>() {
            @Override
            public Aggregations extract(SearchResponse response) {
                return response.getAggregations();
            }
        });
        for (Aggregation item : list) {

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

            StringTerms terms = (StringTerms)item;
            for (Bucket bucket : terms.getBuckets()) {
                System.out.println(bucket.getKeyAsString());
            }
        }

 

posted @ 2017-07-01 09:21  kingsy.lin  阅读(201)  评论(0编辑  收藏  举报