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方法添加各种聚合。
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");
// 计数
ValueCountAggregationBuilder totalAudioCount = AggregationBuilders.count("totalAudioCount").field("taskId");
// 统计字段排序
group.subAggregation(new BucketSortPipelineAggregationBuilder("orderType",
Collections.singletonList(new FieldSortBuilder("totalAudioCount").order(SortOrder.DESC)))
.gapPolicy(BucketHelpers.GapPolicy.INSERT_ZEROS));
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));
}
}