0、自定义工具类
https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-aggs.html To build aggregation requests, use AggregationBuilders helpers. Just import them in your class:
代码语言:javascript复制import org.elasticsearch.search.aggregations.AggregationBuilders;
代码语言:javascript复制package cn.hadron.es;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.min.Min;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
public class AggregationUtil {
private String index="index";
private TransportClient client = ESUtil.getClient();
public AggregationUtil(String index){
this.index=index;
}
...
...
}
https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/_metrics_aggregations.html
1、max统计
代码语言:javascript复制 /**
* 最大值统计
* @param field
* @return
*/
public double max(String field){
AggregationBuilder agg=AggregationBuilders.max("aggMax").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).get();
Max max=response.getAggregations().get("aggMax");
return max.getValue();
}
2、min统计
代码语言:javascript复制 /**
* 最小值统计
* @param field
* @return
*/
public double min(String field){
AggregationBuilder agg=AggregationBuilders.min("aggMin").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).get();
Min min=response.getAggregations().get("aggMin");
return min.getValue();
}
3、sum统计
代码语言:javascript复制 /**
* 合计统计
* @param field
* @return
*/
public double sum(String field){
AggregationBuilder agg=AggregationBuilders.sum("aggSum").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).get();
Sum sum=response.getAggregations().get("aggSum");
return sum.getValue();
}
4、avg统计
代码语言:javascript复制/**
* 平均值统计
* @param field
* @return
*/
public double avg(String field){
AggregationBuilder agg=AggregationBuilders.avg("aggAvg").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).get();
Avg avg=response.getAggregations().get("aggAvg");
return avg.getValue();
}
5、基本统计
代码语言:javascript复制 /**
* 基本统计
* @param field
* @return
*/
public Stats stats(String field){
AggregationBuilder agg=AggregationBuilders.stats("aggStats").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("aggStats");
}
6、高级统计
代码语言:javascript复制 /**
* 高级统计
* @param field
* @return
*/
public ExtendedStats extendedStats(String field){
AggregationBuilder agg=AggregationBuilders.extendedStats("exStats").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("exStats");
}
7、基数统计
代码语言:javascript复制 /**
* 基数统计
* @param field
* @return
*/
public double cardinality(String field){
AggregationBuilder agg=AggregationBuilders.cardinality("cardinality").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).get();
Cardinality c=response.getAggregations().get("cardinality");
return c.getValue();
}
8、百分位统计
代码语言:javascript复制/**
* 百分位统计
* @param field
* @return
*/
public Percentiles percentiles(String field){
AggregationBuilder agg=AggregationBuilders.percentiles("percentiles").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
return response.getAggregations().get("percentiles");
}
9、文档数量统计
代码语言:javascript复制/**
* 文档数量统计
* @param field
* @return
*/
public double valueCount(String field){
AggregationBuilder agg=AggregationBuilders.count("valueCount").field(field);
SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet();
ValueCount count=response.getAggregations().get("valueCount");
return count.getValue();
}
10、测试样例
(1)max
代码语言:javascript复制package cn.hadron;
import cn.hadron.es.AggregationUtil;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double max=util.max("age");
System.out.println("max age=" max);
}
}
代码语言:javascript复制max age=31.0
(2)min
代码语言:javascript复制 public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double min=util.min("salary");
System.out.println("min salary=" min);
}
代码语言:javascript复制min salary=15000.0
(3)avg
代码语言:javascript复制 public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double avg=util.avg("salary");
System.out.println("avg salary=" avg);
}
代码语言:javascript复制avg salary=17500.0
(4)sum
代码语言:javascript复制 public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double sum=util.sum("salary");
System.out.println("sum salary=" sum);
}
代码语言:javascript复制sum salary=105000.0
(5)stats
代码语言:javascript复制package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Stats stats=util.stats("salary");
System.out.println("min=" stats.getMin());
System.out.println("max=" stats.getMax());
System.out.println("avg=" stats.getAvg());
System.out.println("sum=" stats.getSum());
System.out.println("count=" stats.getCount());
}
}
代码语言:javascript复制min=15000.0
max=20000.0
avg=17500.0
sum=105000.0
count=6
(6)extendedStats
代码语言:javascript复制package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
ExtendedStats stats=util.extendedStats("salary");
System.out.println("min=" stats.getMin());
System.out.println("max=" stats.getMax());
System.out.println("avg=" stats.getAvg());
System.out.println("sum=" stats.getSum());
System.out.println("count=" stats.getCount());
System.out.println("stdDeviation=" stats.getStdDeviation());
System.out.println("sumOfSquares=" stats.getSumOfSquares());
System.out.println("variance=" stats.getVariance());
}
}
代码语言:javascript复制min=15000.0
max=20000.0
avg=17500.0
sum=105000.0
count=6
stdDeviation=2061.5528128088304
sumOfSquares=1.863E9
variance=4250000.0
(7)cardinality
代码语言:javascript复制 public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double cardinality=util.cardinality("salary");
System.out.println("cardinality=" cardinality);
}
4个工资级别
代码语言:javascript复制cardinality=4.0
(8)percentiles
代码语言:javascript复制package cn.hadron;
import cn.hadron.es.AggregationUtil;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
public class AggregationDemo {
public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
Percentiles percent=util.percentiles("salary");
for(Percentile p:percent) {
System.out.printf("percent [%f],value [%f]n",p.getPercent(),p.getValue());
}
}
}
代码语言:javascript复制percent [1.000000],value [15000.000000]
percent [5.000000],value [15000.000000]
percent [25.000000],value [15500.000000]
percent [50.000000],value [17500.000000]
percent [75.000000],value [19500.000000]
percent [95.000000],value [20000.000000]
percent [99.000000],value [20000.000000]
(9)valueCount
代码语言:javascript复制 public static void main(String[] args) {
AggregationUtil util=new AggregationUtil("my-index");
double count=util.valueCount("salary");
System.out.println("count=" count);
}
代码语言:javascript复制count=6.0