ES 去重/聚合查询

es 安装版本为 7.14.0
springboot 有默认的版本,防止冲突设置如下

    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.14.0</elasticsearch.version>
    </properties>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 一休
 * @date 2021/10/19 14:52
 */
@Configuration
public class EsClientConfig {

    @Value("${es.url}")
    private String[] esUrls;
    @Value("${es.username}")
    private String esUserName;
    @Value("${es.password}")
    private String esPassword;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 测试
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.202.128", 9200, "http"));
        RestClient client = builder.build();
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(client);
        return restHighLevelClient;
        //账号密码连接
       /* HttpHost[] hosts = new HttpHost[esUrls.length];
        for (int i = 0; i < esUrls.length; i++) {
            String[] strings = esUrls[i].split(":");
            hosts[i] = new HttpHost(strings[0], Integer.parseInt(strings[1]));
        }
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUserName, esPassword));
        RestClientBuilder builder =
                RestClient.builder(hosts).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });
        return new RestHighLevelClient(builder.build());*/
    }

}

去重查询

 @Resource
 private RestHighLevelClient client;

 @Test
    void contextLoads() throws Exception {
        SearchRequest request = new SearchRequest();
        request.indices("students").types("student");
        // 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //指定去重字段
        CollapseBuilder collapseBuilder = new CollapseBuilder("name.keyword");
        // 查询去重后的结果数量
        CardinalityAggregationBuilder aggregationBuilder = AggregationBuilders.cardinality("name").field("name.keyword");
        sourceBuilder.collapse(collapseBuilder);
        sourceBuilder.aggregation(aggregationBuilder);
        sourceBuilder.size(1000);
        request.source(sourceBuilder);

        SearchResponse response = client.search(request);
        System.out.println(JSON.toJSONString(response.getAggregations()));
        SearchHits hits = response.getHits();
        System.out.println(hits.getHits().length);
        System.out.println(JSON.toJSONString(hits));
        for (SearchHit hit : hits) {
            System.out.println(JSON.toJSONString(hit.getSource()));
        }

    }

聚合取值 、重点获取桶内数据

 @Test
    void contextLoads() throws Exception {
        SearchRequest request = new SearchRequest();
      // 设置索引和type
      //  request.indices("students").types("student");
        // 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
      // 分组,排序 :key 和 hit 为自定义,第二个参数为 条件查询字段
        TermsAggregationBuilder terms = AggregationBuilders.terms("key").field("recText.keyword")
                .subAggregation(AggregationBuilders.topHits("hit").sort("statisDate.keyword", SortOrder.DESC));
        sourceBuilder.aggregation(terms);
        sourceBuilder.size(1000);
        request.source(sourceBuilder);
        SearchResponse response = client.search(request);
        Long hs = response.getHits().getTotalHits();
        System.out.println(hs);

        ParsedTerms terms1 = (ParsedTerms) response.getAggregations().getAsMap().get("key");
        List<ParsedTerms.ParsedBucket> list = (List<ParsedTerms.ParsedBucket>) terms1.getBuckets();
        for (ParsedTerms.ParsedBucket parsedBucket : list) {
            System.out.println(parsedBucket.getKeyAsString());
            ParsedTopHits topHits = parsedBucket.getAggregations().get("hit");
            SearchHit hit = topHits.getHits().getHits()[0];
            String time = hit.getSourceAsMap().get("createTime").toString();
            System.out.println(time);
            return;
        }
posted @ 2022-01-20 23:22  暮雨寒冬  阅读(1318)  评论(0编辑  收藏  举报