SpringBoot ElasticSearch 聚合统计

SpringBoot ElasticSearch 聚合统计

Spring Boot和Elasticsearch的集成主要涉及使用Spring Data Elasticsearch库。Elasticsearch是一个分布式搜索引擎,它提供了丰富的RESTful API,用于索引、搜索和分析大量数据。

在Spring Boot中,你可以使用Spring Data Elasticsearch来简化与Elasticsearch的交互。Spring Data Elasticsearch提供了对Elasticsearch的高级查询、聚合和其他操作的支持。

聚合(Aggregation): 聚合是一种在Elasticsearch中进行数据分析的方式。它允许你对数据集执行各种统计、计算和分析操作。聚合框架支持的一些常见聚合类型包括:

BucketAggregations: 将文档分配到不同的"桶"中,类似于SQL中的GROUP BY。常见的桶聚合包括terms、date histogram等。

MetricAggregations: 计算文档字段的度量值,如求和、平均值、最大值、最小值等。

PipelineAggregations: 允许你在其他聚合的结果上执行进一步的操作,类似于数据流管道。

Spring Data Elasticsearch中的聚合支持: Spring Data Elasticsearch通过ElasticsearchTemplate或ElasticsearchRepository接口提供对Elasticsearch聚合的支持。你可以使用NativeSearchQueryBuilder构建查询,然后使用addAggregation方法添加各种聚合。

java 复制代码
package org.jeecg.module.mark.task;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
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.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;
import org.jeecg.modules.mark.common.es.entity.AudioMarkInfo;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

@Slf4j
@SpringBootTest
public class Test {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Test
    public void count() {

        String indexName = "app_student_1";

        List<String> ids = new ArrayList<>();
        ids.add("1");
        ids.add("2");

        NativeSearchQueryBuilder query = new NativeSearchQueryBuilder();
        query.withQuery(QueryBuilders.termsQuery("id", ids));

        TermsAggregationBuilder group = AggregationBuilders.terms("group").field("id");

        // 分组 求和
        SumAggregationBuilder math = AggregationBuilders.sum("math").field("math");
        AggregationBuilder english = AggregationBuilders.filter("english",
                        QueryBuilders.termQuery("status", 1)).
                subAggregation(AggregationBuilders.count("english").field("id"));
        group.subAggregation(math);
        group.subAggregation(english);

        // 分组 求平均数
        AvgAggregationBuilder science = AggregationBuilders.avg("science").field("science");
        // 分组 求最小
        MinAggregationBuilder geography = AggregationBuilders.min("geography").field("geography");
        // 分组 求最大
        MinAggregationBuilder chemistry = AggregationBuilders.min("chemistry").field("chemistry");
        // 分组 统计
        ValueCountAggregationBuilder physics = AggregationBuilders.count("physics").field("physics");

        query.addAggregation(science);
        query.addAggregation(geography);
        query.addAggregation(chemistry);
        query.addAggregation(physics);

        SearchHits<AudioMarkInfo> search = restTemplate.search(query.build(), AudioMarkInfo.class,
                IndexCoordinates.of(indexName));

        Aggregations aggregations = search.getAggregations();
        System.out.println(aggregations);
        ParsedStringTerms terms = aggregations.get("group");
        List<? extends Terms.Bucket> buckets = terms.getBuckets();

        Map<String, Map<String, Object>> result = new HashMap<>();
        for (Terms.Bucket bucket : buckets) {
            String taskId = bucket.getKeyAsString();
            long totalCount = buckets.get(0).getDocCount();
            // 保存数据
            Map<String, Object> tmp = new HashMap<>((int) totalCount);
            tmp.put("total", totalCount);
            result.put(taskId, tmp);
            List<Aggregation> lists = bucket.getAggregations().asList();
            for (Aggregation list : lists) {
                Map<String, Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(list), Map.class);
                if (MapUtil.isEmpty(map)) {
                    continue;
                }
                Object value = null == map.get("value") ? map.get("docCount") : map.get("value");
                result.get(taskId).put((String) map.get("name"), value);
            }
        }

        System.out.println(JSONUtil.toJsonPrettyStr(result));

    }

}
相关推荐
lifeng43213 小时前
Jenkins集成部署(图文教程、超级详细)
运维·jenkins
HaiFan.3 小时前
SpringBoot 事务
java·数据库·spring boot·sql·mysql
无所不在的物质5 小时前
Jenkins基础教程
运维·云原生·自动化·jenkins
大梦百万秋5 小时前
Spring Boot实战:构建一个简单的RESTful API
spring boot·后端·restful
LI JS@你猜啊5 小时前
Elasticsearch 集群
大数据·服务器·elasticsearch
斌斌_____5 小时前
Spring Boot 配置文件的加载顺序
java·spring boot·后端
苹果醋36 小时前
React系列(八)——React进阶知识点拓展
运维·vue.js·spring boot·nginx·课程设计
等一场春雨8 小时前
springboot 3 websocket react 系统提示,选手实时数据更新监控
spring boot·websocket·react.js
荆州克莱8 小时前
Golang的性能监控指标
spring boot·spring·spring cloud·css3·技术
AI人H哥会Java9 小时前
【Spring】控制反转(IoC)与依赖注入(DI)—IoC容器在系统中的位置
java·开发语言·spring boot·后端·spring