Elasticsearch Java API 的使用(22)—实现桶聚合

分组聚合
使用 terms 实现分组集合

public class EsTermsAgg throws UnknownHostException{
    public void TermsAgg(TransportClient client){
       AggregationBuilder agg = AggregationBuilders.terms("terms").field("agg");
       
       SearchResponse response = client.prepareSearch("lib3")
							       .addAggregation(agg)
							       .execute()
							       .actionGet();
	   
	   Terms terms = response.getAggregation().get("terms");
	   for(Terms.Bucket entry:terms getBuckets()){
		   System.out.println(entry.getKey()+":"+entry.getDocument());
	   }
    }
}

filter过滤聚合
使用 filter 实现过滤聚合

public class EsFilterAgg throws UnknownHostException{
    public void FilterAgg(TransportClient client){
       QueryBuilder query = QueryBuilders.termQuery("age", 20);
       //把termQuery设的条件作为过滤条件
       AggregationBuilder agg = AggregationBuilders.filter("filter", query);
       
       SearchResponse response = client.prepareSearch("lib3")
							       .addAggregation(agg)
							       .execute()
							       .actionGet();
	   
	   Filter filter = response.getAggregations().get("filter");
	   System.out.println(filter.getDocCount());
    }
}

filters过滤聚合
使用 filters 实现多条件过滤聚合

public class EsFilterAgg throws UnknownHostException{
    public void FilterAgg(TransportClient client){
       AggregationBuilder agg = AggregationBuilders.filters("filters", 
		       new FiltersAggregator.KeyedFilter("changge", QueryBuilders.termQuery()),
		       new FiltersAggregator.KeyedFilter("hejiu", QueryBuilders.termQuery()));
       
       SearchResponse response = client.prepareSearch("lib3")
							       .addAggregation(agg)
							       .execute()
							       .actionGet();
	   
	   Filters filters = response.getAggregations().get("filters");
	   for(Filters.Bucket entity:filters.getBuckets){
		   System.out.println(entry.getKey()+":"+entry.getDocument());
	   }
    }
}

范围聚合
使用 range 实现范围聚合

public class EsRangeAgg throws UnknownHostException{
    public void RangeAgg(TransportClient client){
       AggregationBuilder agg = AggregationBuilders.range("range")
											       .field("age")
											       .addUnboundedTo(50)    //( , to)
											       .addRange(25, 50)      //[from, to) 
											       .addUnboundedFrom(25); //[from,)
       
       SearchResponse response = client.prepareSearch("lib3")
							       .addAggregation(agg)
							       .execute()
							       .actionGet();
	   
	   Range r = response.getAggregations().get("range");
	   for(Range.Bucket entity:filters.getBuckets){
		   System.out.println(entry.getKey()+":"+entry.getDocument());
	   }
    }
}

为空、为null聚合
使用 missing 实现某个字段为空、为 null 的统计聚合

public class EsMissingAgg throws UnknownHostException{
    public void MissingAgg(TransportClient client){
       AggregationBuilder agg = AggregationBuilders.missing("missing")
											       .field("price"); 
       
       SearchResponse response = client.prepareSearch("lib4")
							       .addAggregation(agg)
							       .execute()
							       .actionGet();
	   
	   Aggregation aggregation = response.getAggregations().get("missing");
	   System.out.println(aggregation.toString());
    }
}

你可能感兴趣的:(Elasticsearch,Java,API,桶聚合)