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
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
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.ValueCountAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.BucketHelpers;
import org.elasticsearch.search.aggregations.pipeline.BucketSortPipelineAggregationBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.jeecg.modules.mark.common.es.entity.AudioMarkInfo;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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;
@SpringBootTest
public class ElasticSearchTest {
@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 counts = AggregationBuilders.count("count").field("id");
group.subAggregation(counts);
// 统计字段排序
group.subAggregation(new BucketSortPipelineAggregationBuilder("orderType",
Collections.singletonList(new FieldSortBuilder("count").order(SortOrder.DESC)))
.gapPolicy(BucketHelpers.GapPolicy.INSERT_ZEROS));
SearchHits<AudioMarkInfo> search = restTemplate.search(query.build(), AudioMarkInfo.class,
IndexCoordinates.of(indexName));
Aggregations aggregations = search.getAggregations();
ParsedStringTerms terms = aggregations.get("group");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
String id = bucket.getKeyAsString();
long count = bucket.getDocCount();
for (Aggregation list : bucket.getAggregations().asList()) {
// TODO:
}
}
}
}