ES聚合(仅供自己参考)

时间:2024-11-01 09:33:17

1、聚合:可以实现对文档数据的统计、分析、运算。聚合常见的是三大类

2、聚合的分类:

   1、桶(Bucket)聚合:用来对文档做分组

     + TermAggregation:按照文档字段值分组

      + Date Histogram:按照日期阶梯分组,例如一周为一组,或者一月为一组

2、度量(Metric)聚合:用以计算一些值,AVG,MAX,MIN,SUM,STATS(都做前面的几个)

3、管道聚合(Pipeline):其他聚合的结果为基础做聚合(比如度量和桶的结果做基础)

4、参与聚合的字段不能是可分词的(Text类型):keyword,Date,数值,布尔

#桶聚合,按照字段品牌来分类
GET /hotel/_search
{
  "size": 0,
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 25
      }
    }
  }
}

#同聚合,按照字段品牌来分类,同时指定排序方式

GET /hotel/_search
{
  "size": 0,        #指定文档输出的数量
  "aggs": {
    "brandAgg": {
      "terms": {            #分类的类型
        "field": "brand",    #分类的字段
        "size": 20,            #分类的大小
        "order": {
          "_count": "asc"      #输出的顺序,默认为降序
        }
      }
    }
  }
}


#桶聚合,因为数据可能很多,通过查询条件筛选之后在做分类
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "lte": 200
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10
      }
    }
  }
}

聚合嵌套和Metric聚合

#metric聚合和桶聚合和度量聚合的嵌套

GET /hotel/_search
{
  "size": 0, 
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 20,
        "order": {
          "scoreAgg.avg": "asc"
        }
      },
      "aggs": {        #内嵌一个agg,使得上面每个分类自己计算自己
        "scoreAgg": {
          "stats": {
            "field": "score"
          }
        }
      }
    }
  }
}

java代码:

package cn.itcast.hotel;

import com.google.protobuf.MapEntry;
import lombok.val;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
public class HotelAggText {

    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.136.128:9200")
        ));
    }

    @AfterEach
    void tearDown() {
        try {
            client.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void bucketAggTest() throws IOException {

        SearchRequest request = new SearchRequest("hotel");
        request.source().size(0);
        request.source().aggregation(AggregationBuilders.terms("brandAgg").field("brand").size(20));
        request.source().aggregation(AggregationBuilders.
                terms("starName").field("starName").size(10));
        //查询城市city

        request.source().aggregation(AggregationBuilders
                .terms("city").field("city").size(5));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        print(response);
    }

    private void print(SearchResponse response)
    {
        Aggregations aggregations = response.getAggregations();
        Terms brandAgg = aggregations.get("brandAgg");
        Terms city =aggregations.get("city");
        List<? extends Terms.Bucket> buckets = brandAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println(bucket.getKey() +"  "+bucket.getDocCount());
        }

        List<? extends Terms.Bucket> buckets1 = city.getBuckets();
        for (Terms.Bucket bucket : buckets1) {
            System.out.println(bucket.getKey() +"  "+bucket.getDocCount());
        }
        //Map<String, Aggregation> asMap = aggregations.getAsMap();
        //Aggregation brandAgg = asMap.get("brandAgg");
        //Map<String, Object> metadata = brandAgg.getMetadata();
        //for (Map.Entry<String, Object> entry : metadata.entrySet()) {
        //    System.out.println(entry.getKey() + "  "+entry.getValue());
        //}
    }
}