ElasticSearch 2.3.3 java API操作二

lcz393537415發表於2016-06-24

點選(此處)摺疊或開啟

  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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章