ElasticSearch 2.3.3 java API操作二


点击(此处)折叠或打开

  1. import org.elasticsearch.action.search.SearchResponse;
  2. import org.elasticsearch.client.transport.TransportClient;
  3. import org.elasticsearch.index.query.QueryBuilders;
  4. import org.elasticsearch.search.aggregations.AggregationBuilders;
  5. import org.elasticsearch.search.aggregations.bucket.filters.Filters;
  6. import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
  7. import org.elasticsearch.search.aggregations.metrics.stats.Stats;
  8. import org.elasticsearch.search.aggregations.metrics.sum.Sum;

  9. import com.sany.util.ESUtil;

  10. public class EsAggregationTest {
  11.     /**
  12.      * histogram
  13.      * 统计每隔interval区间的数值
  14.      * 下面的返回结果 表示的含义 [10000~20000)的记录有3条,这三条记录的price总和
  15.      * 10000    3    37000.0
  16.         20000    3    65000.0
  17.         30000    1    30000.0
  18.         40000    0    0.0
  19.         50000    0    0.0
  20.         60000    0    0.0
  21.         70000    0    0.0
  22.         80000    1    80000.0

  23.      */
  24.        public static void verticleBarAggs(String index,String type){
  25.          TransportClient client=ESUtil.getClient();
  26.          SearchResponse searchResponse = client.prepareSearch(index).setTypes(type).
  27.          addAggregation(AggregationBuilders.histogram("price_verticle").field("price").interval(10000)
  28.                   .subAggregation(AggregationBuilders.sum("sumprice").field("price"))).get();
  29.          Histogram hist = searchResponse.getAggregations().get("price_verticle");
  30.          for (Histogram.Bucket bucket : hist.getBuckets()) {
  31.               Sum s=bucket.getAggregations().get("sumprice");
  32.                  System.out.println(bucket.getKey()+"\t"+bucket.getDocCount()+"\t"+s.getValue());
  33.             }
  34.          client.close();
  35.        }
  36.        
  37.       /**
  38.        * filter aggregation
  39.        * 根据过滤条件生成bucket, 统计bucket中数据总数,返回值
  40.        * 0    4 颜色为红色的有4个,blue的有2个
  41.          1    2
  42.        */
  43.        public static void filterAggregation(){
  44.          TransportClient client = ESUtil.getClient();
  45.          SearchResponse searchResponse = client.prepareSearch("cars").setTypes("transactions").addAggregation(
  46.                   AggregationBuilders.filters("colorfilter")
  47.                   .filter(QueryBuilders.termQuery("color", "red"))
  48.                   .filter(QueryBuilders.termQuery("color", "blue"))).get();
  49.          Filters ff=searchResponse.getAggregations().get("colorfilter");
  50.          for (Filters.Bucket bucket : ff.getBuckets()) {
  51.                  // System.out.println(bucket.getKey()+"\t"+bucket.getDocCount());
  52.               System.out.println(bucket.getKeyAsString()+"\t"+bucket.getDocCount());
  53.             }
  54.        }
  55.        
  56.        /**
  57.         * stat,根据单一统计的一个最大,最小,平均值,总和
  58.         */
  59.        public static void statAggregation(){
  60.          TransportClient client = ESUtil.getClient();
  61.          SearchResponse searchResponse = client.prepareSearch("cars").setTypes("transactions").addAggregation(AggregationBuilders.stats("price_stat").field("price")).get();
  62.          Stats sta= searchResponse.getAggregations().get("price_stat");
  63.          System.out.println("max value="+sta.getMax());
  64.          System.out.println("min value="+sta.getMin());
  65.          System.out.println("avg value="+sta.getAvg());
  66.          System.out.println("sum value="+sta.getSum());
  67.          client.close();
  68.        }
  69.        
  70.        public static void main(String[] args) {
  71.          EsAggregationTest.filterAggregation();
  72.      }
  73. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31347383/viewspace-2120936/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31347383/viewspace-2120936/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值