Elasticsearch 7.X 聚合查询 及 ElasticsearchRestTemplate 操作

Posted 小毕超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch 7.X 聚合查询 及 ElasticsearchRestTemplate 操作相关的知识,希望对你有一定的参考价值。

一、创建测试索引

创建索引结构,向 ES 服务发送 PUT 请求:

http://127.0.0.1:9200/jh_test

	"settings": ,
	"mappings": 
		"properties": 
			"name": 
				"type": "text",
                "fields": 
                    "keyword": 
                        "type": "keyword",
                        "ignore_above": 256
                    
                
			,
			"sex": 
				"type": "keyword"
			,
			"buyCount": 
				"type": "long"
			,
            "createMonth":
                "type":"keyword"
            
		
	

其中字段的含义为:
name:姓名、buyCount:购买数量,sex:性别,createMonth:创建月

添加测试数据:

"name":"张三","buyCount":5,"sex":"男","createMonth":"2021-01"
"name":"李四","buyCount":5,"sex":"男","createMonth":"2021-01"
"name":"小卷","buyCount":18,"sex":"女","createMonth":"2021-01"
"name":"小明","buyCount":6,"sex":"女","createMonth":"2021-01"
"name":"张三","buyCount":3,"sex":"男","createMonth":"2021-02"
"name":"王五","buyCount":8,"sex":"男","createMonth":"2021-02"
"name":"赵四","buyCount":4,"sex":"男","createMonth":"2021-02"
"name":"诸葛亮","buyCount":6,"sex":"男","createMonth":"2021-02"
"name":"黄忠","buyCount":9,"sex":"男","createMonth":"2021-03"
"name":"李白","buyCount":1,"sex":"男","createMonth":"2021-03"
"name":"赵四","buyCount":3,"sex":"男","createMonth":"2021-03"
"name":"张三","buyCount":2,"sex":"男","createMonth":"2021-03"
"name":"李四","buyCount":6,"sex":"男","createMonth":"2021-04"
"name":"王五","buyCount":9,"sex":"男","createMonth":"2021-04"
"name":"李四","buyCount":4,"sex":"男","createMonth":"2021-04"
"name":"王五","buyCount":2,"sex":"男","createMonth":"2021-04"

二、聚合查询

聚合查询的用法,向 ES 服务发送 GET 请求:

http://127.0.0.1:9200/jh_test/_search
"aggs" : 
    "<aggregation_name>" :                                  <!--聚合名称 -->
        "<aggregation_type>" :                              <!--聚合类型 -->
            <aggregation_body>                               <!--聚合具体字段 -->
        
        [,"meta" :   [<meta_data_body>]  ]?                <!--元信息 -->
        [,"aggs" :  [<sub_aggregation>]+  ]?       <!--子聚合 -->
    


1. 查询 buyCount 的总和:


  "size":0,
  "aggs":
    "buyCountSum":
      "sum": 
        "field": "buyCount"
      
    
  

2. 查询 2021-02 月 buyCount 的总和:


  "size":0,
   "query": 
		"term":  
            "createMonth": "2021-02" 
        
	,
  "aggs":
    "buyCountSum":
      "sum": 
        "field": "buyCount"
      
    
  

3. 查询 2021-03 月 buyCount 的最大值:


  "size":0,
   "query": 
		"term":  
            "createMonth": "2021-03" 
        
	,
  "aggs":
    "buyCountMax":
      "max": 
        "field": "buyCount"
      
    
  

4. 查询 2021-03 月 buyCount 的最小值:


  "size":0,
   "query": 
		"term":  
            "createMonth": "2021-03" 
        
	,
  "aggs":
    "buyCountMin":
      "min": 
        "field": "buyCount"
      
    
  

5. 同时查询 2021-03 月 buyCount 的最大值和最小值:


  "size":0,
   "query": 
		"term":  
            "createMonth": "2021-03" 
        
	,
  "aggs":
    "buyCountMax":
      "max": 
        "field": "buyCount"
      
    ,
     "buyCountMin":
      "min": 
        "field": "buyCount"
      
    
  

6. 查询所有 name 的去重后的数量


  "size":0,
  "aggs":
    "distinctName":
      "cardinality": 
        "field": "name.keyword"
      
    
  

7. 查询 2021-04 月 name 的去重后的数量


  "size":0,
  "query": 
		"term":  
            "createMonth": "2021-04" 
        
	,
  "aggs":
    "distinctName":
      "cardinality": 
        "field": "name.keyword"
      
    
  

8. 查询 BuyCount 的平均值


    "size":"0",
    "aggs":
        "buyCountAvg":
            "avg":
                "field":"buyCount"
            
        
    

9. 一次查询 总数,最大值,最小值,平均值,总和


  "size":0,
  "aggs":
    "statsAll":
      "stats":
        "field":"buyCount"
      
    
  

10. 根据 createMonth 分组查询每个月的最大 buyCount


    "size":0,
    "aggs": 
    "createMonthGroup": 
      "terms": 
        "field": "createMonth"
      ,
      "aggs": 
        "buyCountMax": 
          "max": 
            "field": "buyCount"
          
        
      
    
  

11. 查询每 createMonth 下,根据 sex 区分,统计buyCount 的平均值


    "size":0,
    "aggs": 
    "createMonthGroup": 
      "terms": 
        "field": "createMonth"
      ,
      "aggs": 
        "sexGroup": 
          "terms": 
            "field": "sex"
          ,
          "aggs": 
                "buyCountAvg": 
                    "avg": 
                        "field": "buyCount"
                    
                
            
        
      
    
  

三、ElasticsearchRestTemplate 聚合查询

创建实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "jh_test", shards = 3, replicas = 1, refreshInterval = "30s")
public class JhTestEntity 
    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String name;

    @Field(type = FieldType.Keyword)
    private String sex;

    @Field(type = FieldType.Long)
    private Long buyCount;

    @Field(type = FieldType.Keyword)
    private String createMonth;

1. 查询 buyCount 的总和:

 @Test
 void aggs() 
     SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field("buyCount");
     Query query = new NativeSearchQueryBuilder()
             .addAggregation(buyCountSum)
             .build();
     SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
     if (search.hasAggregations()) 
         Aggregations aggregations = search.getAggregations();
         if (Objects.nonNull(aggregations)) 
             Sum sum = aggregations.get("buyCountSum");
             log.info("计算 buyCount 总数: ", sum.getValue());
         
     
 

2. 查询 2021-02 月 buyCount 的总和:

@Test
void aggs() 
    QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-02");
    SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field("buyCount");
    Query query = new NativeSearchQueryBuilder()
            .withQuery(queryBuilder)
            .addAggregation(buyCountSum)
            .build();
    SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
    if (search.hasAggregations()) 
        Aggregations aggregations = search.getAggregations();
        if (Objects.nonNull(aggregations)) 
            Sum sum = aggregations.get("buyCountSum");
            log.info("计算 buyCount 总数: ", sum.getValue());
        
    

3. 查询 2021-03 月 buyCount 的最大值:

@Test
void aggs() 
    QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-03");
    MaxAggregationBuilder buyCountMax = AggregationBuilders.max("buyCountMax").field("buyCount");
    Query query = new NativeSearchQueryBuilder()
            .withQuery(queryBuilder)
            .addAggregation(buyCountMax)
            .build();
    SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
    if (search.hasAggregations()) 
        Aggregations aggregations = search.getAggregations();
        if (Objects.nonNull(aggregations)) 
            Max max = aggregations.get("buyCountMax");
            log.info("计算 buyCount 最大值: ", max.getValue());
        
    

4. 查询 2021-03 月 buyCount 的最小值:

@Test
void aggs() 
    QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-03");
    MinAggregationBuilder buyCountMin = AggregationBuilders.min("buyCountMin").field("buyCount");
    Query query = new NativeSearchQueryBuilder()
            .withQuery(queryBuilder)
            .addAggregation(buyCountMin)
            .build();
    SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
    if (search.hasAggregations()) 
        Aggregations aggregations = search.getAggregations();
        if (Objects.nonNull(aggregations)) 
            Min min = aggregations.get("buyCountMin");
            log.info("计算 buyCount 最小值: ", min.getValue());
        
    

5. 同时查询 2021-03 月 buyCount 的最大值和最小值:

@Test
void aggs() 
    QueryBuilder queryBuilder = QueryBuilders.termQuery04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作

ElasticSearch(ES)使用Nested结构存储KV及聚合查询

Elasticsearch 7.x 深入【1】索引【四】常用属性

[Elasticsearch] 聚合中的重要概念 - Buckets(桶)及Metrics(指标)

Elasticsearch 7.X 中文分词器 ik 使用,及词库的动态扩展

Elasticsearch聚合查询