点击(此处)折叠或打开
-
import org.elasticsearch.action.search.SearchResponse;
-
import org.elasticsearch.client.transport.TransportClient;
-
import org.elasticsearch.index.query.QueryBuilders;
-
import org.elasticsearch.search.aggregations.AggregationBuilders;
-
import org.elasticsearch.search.aggregations.bucket.filters.Filters;
-
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
-
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
-
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
-
-
import com.sany.util.ESUtil;
-
-
public class EsAggregationTest {
-
/**
-
* histogram
-
* 统计每隔interval区间的数值
-
* 下面的返回结果 表示的含义 [10000~20000)的记录有3条,这三条记录的price总和
-
* 10000 3 37000.0
-
20000 3 65000.0
-
30000 1 30000.0
-
40000 0 0.0
-
50000 0 0.0
-
60000 0 0.0
-
70000 0 0.0
-
80000 1 80000.0
-
-
*/
-
public static void verticleBarAggs(String index,String type){
-
TransportClient client=ESUtil.getClient();
-
SearchResponse searchResponse = client.prepareSearch(index).setTypes(type).
-
addAggregation(AggregationBuilders.histogram("price_verticle").field("price").interval(10000)
-
.subAggregation(AggregationBuilders.sum("sumprice").field("price"))).get();
-
Histogram hist = searchResponse.getAggregations().get("price_verticle");
-
for (Histogram.Bucket bucket : hist.getBuckets()) {
-
Sum s=bucket.getAggregations().get("sumprice");
-
System.out.println(bucket.getKey()+"\t"+bucket.getDocCount()+"\t"+s.getValue());
-
}
-
client.close();
-
}
-
-
/**
-
* filter aggregation
-
* 根据过滤条件生成bucket, 统计bucket中数据总数,返回值
-
* 0 4 颜色为红色的有4个,blue的有2个
-
1 2
-
*/
-
public static void filterAggregation(){
-
TransportClient client = ESUtil.getClient();
-
SearchResponse searchResponse = client.prepareSearch("cars").setTypes("transactions").addAggregation(
-
AggregationBuilders.filters("colorfilter")
-
.filter(QueryBuilders.termQuery("color", "red"))
-
.filter(QueryBuilders.termQuery("color", "blue"))).get();
-
Filters ff=searchResponse.getAggregations().get("colorfilter");
-
for (Filters.Bucket bucket : ff.getBuckets()) {
-
// System.out.println(bucket.getKey()+"\t"+bucket.getDocCount());
-
System.out.println(bucket.getKeyAsString()+"\t"+bucket.getDocCount());
-
}
-
}
-
-
/**
-
* stat,根据单一统计的一个最大,最小,平均值,总和
-
*/
-
public static void statAggregation(){
-
TransportClient client = ESUtil.getClient();
-
SearchResponse searchResponse = client.prepareSearch("cars").setTypes("transactions").addAggregation(AggregationBuilders.stats("price_stat").field("price")).get();
-
Stats sta= searchResponse.getAggregations().get("price_stat");
-
System.out.println("max value="+sta.getMax());
-
System.out.println("min value="+sta.getMin());
-
System.out.println("avg value="+sta.getAvg());
-
System.out.println("sum value="+sta.getSum());
-
client.close();
-
}
-
-
public static void main(String[] args) {
-
EsAggregationTest.filterAggregation();
-
}
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31347383/viewspace-2120936/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31347383/viewspace-2120936/
本文介绍了Elasticsearch中几种常见的聚合查询方法,包括直方图聚合、过滤聚合及统计数据聚合等,并通过具体示例展示了如何使用这些聚合来分析数据。
514

被折叠的 条评论
为什么被折叠?



