ELASTICSEARCH6.2.4版本,按照时间查询某个字段的最大最小值

       SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(timeParam);
        if (!"".equals(startTime)) {
            rangeQueryBuilder.gte(startTime);
        }
        if (!"".equals(endTime)) {
            rangeQueryBuilder.lte(endTime);
        }
        searchSourceBuilder.query(rangeQueryBuilder);
        searchSourceBuilder.aggregation(AggregationBuilders.stats(rangeParam).field(rangeParam));
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = null;
        response = restHighClient.search(searchRequest);
        Stats statsAggregation = response.getAggregations().get(rangeParam);
        logger.info("按时间范围查询出最大最小值["+index+"],类型["+type+"],聚合查询参数["+rangeParam+"]");
        double max=statsAggregation.getMax();
        double min=statsAggregation.getMin();

你可能感兴趣的:(java开发)