当前位置:网站首页>A general es aggregation query method

A general es aggregation query method

2022-06-10 03:21:00 Yan children's shoes

 /**
     *  A generic es Aggregate query method 
     * @param restHighLevelClient
     * @param start
     * @param end
     * @param index
     * @param aggregationName
     * @param painlessScript
     * @param existsQueryParams
     * @param notExistsQueryParams
     * @return
     * @throws IOException
     */
    private List<String> queryFromEs(RestHighLevelClient restHighLevelClient, String start, String end, String[] index, String aggregationName, String painlessScript, List<String> existsQueryParams, List<String> notExistsQueryParams) throws IOException {
        List<String> resultList = new ArrayList<>();
        // Create a search source 
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //SearchRequest Query by one or more indexes , Need one SearchSourceBuilder, Search sources provide search options 
        SearchRequest searchRequest = new SearchRequest();
        // Conditions for aggregation 
        TermsAggregationBuilder aggregation = AggregationBuilders.terms(aggregationName).script(new Script(painlessScript)).size(Integer.MAX_VALUE).minDocCount(1).shardMinDocCount(0).showTermDocCountError(false);
        // add to bool filter , Query conditions 
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //must -- Time must meet 
        boolQueryBuilder.must(QueryBuilders.rangeQuery("TIME").gte(start).lte(end));
        // The conditions of existence 
        for (String existsQueryParam : existsQueryParams) {
            boolQueryBuilder.must(QueryBuilders.existsQuery(existsQueryParam));
        }
        // Nonexistent condition 
        for (String notExistsQueryParam : notExistsQueryParams) {
            boolQueryBuilder.mustNot(QueryBuilders.existsQuery(notExistsQueryParam));
        }
        // Definition sourceBuilder Sort by time , positive sequence , Then pass in the previous query criteria ,from 0 size 0  Do not check the original data 
        sourceBuilder.sort("TIME", SortOrder.ASC).from(0).size(0).query(boolQueryBuilder).aggregation(aggregation);
        // Define the index of the query , Define search sources , namely sourceBuilder object 
        searchRequest.indices(index);
        searchRequest.source(sourceBuilder);
        log.info("-query from es index:{}, sql:{}-->", index, sourceBuilder);
        // Begin your search , Get the results 
        SearchResponse response = null;
        response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // Get the aggregated data 
        Aggregations aggregations = response.getAggregations();
        if (aggregations != null) {
            ParsedStringTerms empAccountAggregationsTerms = aggregations.get(aggregationName);
            List<? extends Terms.Bucket> buckets = empAccountAggregationsTerms.getBuckets();
            for (Terms.Bucket bucket : buckets) {
                log.info("--query from es index results:-->" + bucket.getKey().toString());
                resultList.add(bucket.getKey().toString());
            }
        }
        return resultList;
    }
原网站

版权声明
本文为[Yan children's shoes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100312389461.html