elasticsearch笔记(8)聚合查询

聚合查询: 也就是类似mysql的count,max,avg等查询,但要更为强大

聚合查询语法

POST /index/type/_search
{
    "aggs":{
        "名字":{
            "agg_type":{
                "属性":"值"
            }
        }
    }
}

一.  去重计数查询:Cardinality

去掉重复的数据,然后算出总数,也就是Cardinality; 如查询总共几个省.

 

 

 java 中的重复计数查询

 1 //重复计数查询
 2     @Test
 3     public void cardinalityQuery() throws IOException {
 4         //        1. 创建查询对象
 5         String index = "sms-logs-index";
 6         String type = "sms-logs-type";
 7         SearchRequest searchRequest = new SearchRequest(index);//指定索引
 8         searchRequest.types(type);//指定类型
 9 //    2. 封装查询条件
10         String aggName = "agg";
11         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
12         searchSourceBuilder.aggregation(AggregationBuilders.cardinality(aggName).field("province"));
13         searchRequest.source(searchSourceBuilder);
14 
15         //        3.执行查询
16         // client执行
17         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
18         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
19         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
20         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
21 
22 //        4.获取数据(source中的数据)
23         Cardinality aggregation = search.getAggregations().get(aggName); //用Cardinality接收
24         System.out.println(aggregation.getValue());
25     }

 二. 范围统计

根据某个属性的范围,统计文档的个数,(from 包含, to不包含)

针对不同的类型指定不同的方法,

数值:range

时间:date_range

ip:ip_range

数值统计:

 

日期型范围查询(data_range):加一个format来规定格式

 

 

 ip范围统计(ip_range)

 

 

 

java中用range查询

 1     @Test
 2     public void rangeQuery() throws IOException {
 3         //        1. 创建查询对象
 4         String index = "sms-logs-index";
 5         String type = "sms-logs-type";
 6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
 7         searchRequest.types(type);//指定类型
 8 //    2. 封装查询条件
 9         String aggName = "agg";
10         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
11         searchSourceBuilder.aggregation(AggregationBuilders.range(aggName).field("fee").addUnboundedTo(3)
12                 .addRange(3,4)
13                 .addUnboundedFrom(4));
14         searchRequest.source(searchSourceBuilder);
15 
16         //        3.执行查询
17         // client执行
18         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
19         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
20         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
21         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
22 
23 //        4.获取数据(source中的数据)
24         Range range = search.getAggregations().get(aggName);//用Range接收
25         List<? extends Range.Bucket> buckets = range.getBuckets();
26         for(  Range.Bucket bucket : buckets ){
27             System.out.println(bucket.getDocCount());
28 
29         }
30 
31 
32     }

 3. 统计聚合查询

可以查询属性(field)的最大值,最小值,平均值,平方和.......

 

 

 1   @Test
 2     public void extended_status_Query() throws IOException {
 3         //        1. 创建查询对象
 4         String index = "sms-logs-index";
 5         String type = "sms-logs-type";
 6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
 7         searchRequest.types(type);//指定类型
 8 //    2. 封装查询条件
 9         String aggName = "agg";
10         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
11         searchSourceBuilder.aggregation(AggregationBuilders.extendedStats(aggName).field("fee"));
12         searchRequest.source(searchSourceBuilder);
13 
14         //        3.执行查询
15         // client执行
16         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
17         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
18         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
19         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
20 
21 //        4.获取数据(source中的数据)
22         ExtendedStats aggregation = search.getAggregations().get(aggName);//用Range接收
23         System.out.println(aggregation.getMax());
24         System.out.println(aggregation.getSum());
25         System.out.println(aggregation.getAvg());
26     }

 

https://www.elastic.co/guide/en/elasticsearch/reference/6.5/getting-started.html

 

posted @ 2021-02-21 12:41  当当和瓶瓶  阅读(877)  评论(0编辑  收藏  举报