ElasticSearch--aggregation query

aggregation query

Introduction

Aggregation: English is Aggregation, which is a function for statistical analysis of es data provided by es in addition to the search function . Aggregation helps provide aggregated data based on search queries. Aggregation query is an important functional feature in databases. As a search engine and database, ES also provides powerful aggregation analysis capabilities. It buckets and calculates data based on query conditions. It is somewhat similar to group by in SQL plus some functional methods.

注意事项:text类型是不支持聚合的。

Test Data

# 创建索引 index 和映射 mapping
PUT /fruit
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "title":{
    
    
        "type": "keyword"
      },
      "price":{
    
    
        "type":"double"
      },
      "description":{
    
    
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
# 放入测试数据
PUT /fruit/_bulk
{
    
    "index":{
    
    }}
  {
    
    "title" : "面包","price" : 19.9,"description" : "小面包非常好吃"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "旺仔牛奶","price" : 29.9,"description" : "非常好喝"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "日本豆","price" : 19.9,"description" : "日本豆非常好吃"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "小馒头","price" : 19.9,"description" : "小馒头非常好吃"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "大辣片","price" : 39.9,"description" : "大辣片非常好吃"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "透心凉","price" : 9.9,"description" : "透心凉非常好喝"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "小浣熊","price" : 19.9,"description" : "童年的味道"}
{
    
    "index":{
    
    }}
  {
    
    "title" : "海苔","price" : 19.9,"description" : "海的味道"}

use

Terms

group by a field

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-sCQYmL8K-1655972286890)(ElasticSearch.assets/1b97f09106322c6bd6194c688b09444c-16516518624202.png)]

# 根据某个字段进行分组 统计数量
GET /fruit/_search
{
  "query": {
    "term": {
      "description": {
        "value": "好吃"
      }
    }
  }, 
  "aggs": {
    "price_group": {
      "terms": {
        "field": "price"
      }
    }
  }
}

find the maximum

# 求最大值 
GET /fruit/_search
{
  "aggs": {
    "price_max": {
      "max": {
        "field": "price"
      }
    }
  }
}

Find the minimum

# 求最小值
GET /fruit/_search
{
  "aggs": {
    "price_min": {
      "min": {
        "field": "price"
      }
    }
  }
}

average

# 求平均值
GET /fruit/_search
{
  "aggs": {
    "price_agv": {
      "avg": {
        "field": "price"
      }
    }
  }
}

to sum

# 求和
GET /fruit/_search
{
  "aggs": {
    "price_sum": {
      "sum": {
        "field": "price"
      }
    }
  }
}

integrated application

 //求和 平均 最大值 最小值聚合,桶中只有一个返回值
    //分组聚合
    @Test
    public void testAggsFun() throws IOException {
    
    

        SearchRequest searchRequest = new SearchRequest("fruit");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        sourceBuilder
                .query(QueryBuilders.matchAllQuery())
                .size(0)
                //聚合处理,针对某个字段进行分组
                .aggregation(AggregationBuilders.sum("price_sum").field("price"));
//                 .aggregation(AggregationBuilders.avg("price_avg").field("price")); //平均值
//                .aggregation(AggregationBuilders.max("price_min").field("price"));    //最小值
//                .aggregation(AggregationBuilders.min("price_max").field("price"));    //最大值


        searchRequest.source(sourceBuilder);

        SearchResponse response = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);

        //处理聚合结果
        Aggregations aggregations = response.getAggregations();

        ParsedSum parsedSum= aggregations.get("price_sum");
        System.out.println(parsedSum.getValue());


    }

Guess you like

Origin blog.csdn.net/qq_50596778/article/details/125429448