其他分享
首页 > 其他分享> > 30-RestHighLevelClient---聚合查询

30-RestHighLevelClient---聚合查询

作者:互联网

package com.study;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
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.ParsedDoubleTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.ParsedSum;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
public class RestHighLevelClientForAggsTest {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public RestHighLevelClientForAggsTest(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    /**
     *基于terms类型进行聚合  基于字段类型进行分组聚合
     */
    @Test
    public void testAggs() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder
                .query(QueryBuilders.matchAllQuery())//查询条件
                .aggregation(AggregationBuilders.terms("price_group").field("price"));//用来设置聚合处理
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //处理聚合结果
        Aggregations aggregations = searchResponse.getAggregations();
        ParsedDoubleTerms parsedDoubleTerms = aggregations.get("price_group");//根据处理字段的类型来选择ParsedXXXTerms

        List<? extends Terms.Bucket> buckets = parsedDoubleTerms.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println("价格:" + bucket.getKey() + "---" + "文档数量:" + bucket.getDocCount());
        }
    }

    /**
     * max(返回值:ParseMax) min(返回值:ParseMin) sum(返回值:ParseSum) avg(返回值:ParseAvg) 聚合函数     桶中只有一个返回值,区别于terms聚合
     */
    @Test
    public void testFun() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder
                .query(QueryBuilders.matchAllQuery())//查询条件
                .aggregation(AggregationBuilders.sum("sum_price").field("price"));//用来设置聚合处理
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //处理聚合结果
        Aggregations aggregations = searchResponse.getAggregations();
        ParsedSum parsedSum = aggregations.get("sum_price");//根据处理字段的类型来选择ParsedXXXTerms

        System.out.println(parsedSum.getValue());
    }
}

标签:search,聚合,30,aggregations,---,elasticsearch,org,import,RestHighLevelClient
来源: https://www.cnblogs.com/morehair/p/15824321.html