https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/_bucket_aggregations.html
AggregationUtil.java中添加下面方法
/**
* 分组聚合
* @param field
* @return
*/
public Terms terms(String field){
AggregationBuilder agg=AggregationBuilders.terms("terms").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("terms");
}
测试程序
package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Terms terms=util.terms("salary");
for(Terms.Bucket entry:terms.getBuckets()){
System.out.println(entry.getKey()+":"+entry.getDocCount());
}
}
}
运行结果
15000:2
20000:2
17000:1
18000:1
/**
* 过滤器聚合
* @param field
* @return
*/
public Filter filter(String field, String key){
QueryBuilder query=QueryBuilders.termQuery(field,key);
AggregationBuilder agg=AggregationBuilders.filter("filter",query);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("filter");
}
package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Filter filter=util.filter("gender","男");
System.out.println(filter.getDocCount());
}
}
4
/**
* 多过滤器聚合
* @return
*/
public Filters filters(String field1, String key1, String field2, String key2){
AggregationBuilder agg=AggregationBuilders.filters("filters",
new FiltersAggregator.KeyedFilter(key1,QueryBuilders.termQuery(field1,key1)),
new FiltersAggregator.KeyedFilter(key2,QueryBuilders.termQuery(field2,key2)));
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("filters");
}
package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.bucket.filter.Filters;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Filters agg=util.filters("gender","男","dep","bigdata");
for(Filters.Bucket entry:agg.getBuckets()){
System.out.println(entry.getKey()+":"+entry.getDocCount());
}
}
}
bigdata:3
男:4
/**
* 区间聚合
* @param field
* @return
*/
public Range range(String field,double to,double from){
AggregationBuilder agg=AggregationBuilders
.range("range")
.field(field)
.addUnboundedTo(to)//第1个范围 ( ,to)
.addRange(to,from)//第2个范围[to,from)
.addUnboundedFrom(from);//第3个范围[from,)
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("range");
}
package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.bucket.range.Range;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Range agg=util.range("salary",10000,20000);
for(Range.Bucket entry:agg.getBuckets()){
System.out.println(entry.getKey()+":"+entry.getDocCount());
}
}
}
*-10000.0:0
10000.0-20000.0:4
20000.0-*:2
/**
* 日期区间聚合
* @param field
* @return
*/
public Range dateRange(String field,String to,String from){
AggregationBuilder agg=AggregationBuilders
.dateRange("dateRange")
.field(field)
.format("yyyy-MM-dd")
.addUnboundedTo(to)
.addUnboundedFrom(from);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("dateRange");
}
package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.bucket.range.Range;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("website");
Range agg=util.dateRange("postdate","now-12M/M","now-12M/M");
for(Range.Bucket entry:agg.getBuckets()){
System.out.println(entry.getKey()+":"+entry.getDocCount());
}
}
}
*-2017-02-01:8
2017-02-01-*:1
/**
* Missing聚合
* @param field
* @return
*/
public Missing missing(String field){
AggregationBuilder agg=AggregationBuilders.missing("missing").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("missing");
}