其他分享
首页 > 其他分享> > ElasticSearch idea 聚合查询

ElasticSearch idea 聚合查询

作者:互联网

package com.msb.mall.mallsearch;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.msb.mall.mallsearch.config.ElasticSearchConfiguration;
import lombok.Data;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;

@SpringBootTest
class MallSearchApplicationTests {
    
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    
    @Test
    void contextLoads() {
    
        System.out.println("restHighLevelClient---->"+restHighLevelClient);
    }
    
    /**
     * 普通聚合
     * GET /blank/_search
     * {
     *  "query": {"match_all": {}},
     *  "aggs": {
     *    "ageAgg": {
     *      "terms": {
     *        "field": "age",
     *        "size": 10
     *      },"aggs": {
     *        "balanceAvg": {
     *          "avg": {
     *            "field": "balance"
     *          }
     *        }
     *      }
     *    }
     *  },"size": 0
     * }
     * @throws IOException
     */
    @Test
    void SearchIndexByAggregation() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("blank");//设置查找的索库
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询blank下address中包含mill记录
        searchSourceBuilder.query(QueryBuilders.matchAllQuery()).size(0);//查询所有的
        //计算blank 下年龄的分布平均薪资
        TermsAggregationBuilder field = AggregationBuilders.terms("ageAgg").field("age").size(10);
        //嵌套聚合
        field.subAggregation(AggregationBuilders.avg("balanceAvg").field("balance"));
        searchSourceBuilder.aggregation(field);
        searchRequest.source(searchSourceBuilder);
        //如何执行检索操作
        SearchResponse response = restHighLevelClient.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
        //获取检索后的响应对象 我们要解析出我们关心的数据
        System.out.println(response);
        
    }
    
    /**
     * GET /blank/_search
     * {
     *   "query": {"match_all": {}},
     *   "aggs": {
     *     "ageAgg": {
     *       "terms": {
     *         "field": "age",
     *         "size": 10
     *       },"aggs": {
     *         "balanceAgg1": {
     *           "avg": {
     *             "field": "balance"
     *           }
     *         }
     *       }
     *     },"balanceAgg2":{
     *       "avg": {
     *         "field": "age"
     *       }
     *     }
     *   },"size": 0
     * }
     * @throws IOException
     */
    @Test
    void SearchIndexByAggregation1() throws IOException {
        SearchRequest searchRequest = new SearchRequest().indices("blank");//设置查找的索库
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询blank下address中包含mill记录
        searchSourceBuilder.query(QueryBuilders.matchAllQuery()).size(0);//查询所有的
        //计算blank 下年龄的分布平均薪资
        TermsAggregationBuilder field = AggregationBuilders.terms("ageAgg").field("age").size(10);
        field.subAggregation(AggregationBuilders.avg("balanceAgg1").field("balance"));
        searchSourceBuilder.aggregation(field);
        //聚合平均年龄
        AvgAggregationBuilder balanceAggBuilder = AggregationBuilders.avg("balanceAgg2").field("age");
        searchSourceBuilder.aggregation(balanceAggBuilder);
        searchRequest.source(searchSourceBuilder);
        //如何执行检索操作
        SearchResponse response = restHighLevelClient.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
        //获取检索后的响应对象 我们要解析出我们关心的数据
        System.out.println(response);
        
    }
    
    
    @Test
    void SearchIndexByAddress() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("blank");//设置查找的索库
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询blank下address中包含mill记录
        searchSourceBuilder.query(QueryBuilders.matchQuery("address", "mill"));
       /* searchSourceBuilder.from();
        searchSourceBuilder.size();
        searchSourceBuilder.aggregation();*/
        searchRequest.source(searchSourceBuilder);
        //如何执行检索操作
        SearchResponse response = restHighLevelClient.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
        //获取检索后的响应对象 我们要解析出我们关心的数据
        System.out.println("response----->"+response);
    }
    
    @Test
    void SearchIndex() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("blank");//设置查找的索库
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        /*searchSourceBuilder.query();
        searchSourceBuilder.from();
        searchSourceBuilder.size();
        searchSourceBuilder.aggregation();*/
        searchRequest.source(searchSourceBuilder);
        //如何执行检索操作
        SearchResponse response = restHighLevelClient.search(searchRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
        //获取检索后的响应对象 我们要解析出我们关心的数据
        System.out.println("response----->"+response);
    }
    
    /**
     * 测试保存文档
     */
    @Test
    void saveIndex() throws IOException {
    
        IndexRequest indexRequest = new IndexRequest("system");
        indexRequest.id("1");
        //indexRequest.source("name","lcc","age","18","gender","男");//测试   文档属性
        User user = new User();
        user.setName("lcc");
        user.setAge(22);
        user.setGender("男");
        //用jackson中的对象转换json数据
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        indexRequest.source(json, XContentType.JSON);
        //执行操作
        IndexResponse index = restHighLevelClient.index(indexRequest, ElasticSearchConfiguration.COMMON_OPTIONS);
        //提取有用的的返回信息
        System.out.println(index);
    
    
    }
    
    @Data
     class User{
        private String name;
        private Integer age;
        private String gender;
    }
    
}

 

标签:search,聚合,searchSourceBuilder,searchRequest,idea,field,ElasticSearch,org,import
来源: https://www.cnblogs.com/Lcch/p/16323288.html