java简单操作elasticsearch

1.基本过滤查询

long start = System.currentTimeMillis();
long end = start - 4 * 60 * 60 * 1000;
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(end,true).to(start,true);
QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);
QueryBuilder qb=new MatchAllQueryBuilder();
SearchResponse response= elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu").setQuery(s).setFrom(0)
  .setSize(100).get();
SearchHits searchHits =  response.getHits();
for(SearchHit hit:searchHits.getHits()){
  System.out.println(hit.getSourceAsString());
}

2.条件过滤,进然后行分组,对组内数据求平均,然后排行查询

//ES中查询所有主机的监控数据
        BoolQueryBuilder uuidsBoolQuery = QueryBuilders.boolQuery();

        uuidsBoolQuery.must(QueryBuilders.matchQuery("uuid", uuidStr));

        //暂定向前推一天,计算平均
        long end = System.currentTimeMillis();
        long start = end - 24 * 60 * 60 * 1000;
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(start,true).to(end,true);
        QueryBuilder timeFilter = QueryBuilders.boolQuery().must(rangeQueryBuilder);

        //开始cputop查询
        //分组字段是id,排序由多个字段排序组成
        TermsAggregationBuilder orderCpu = AggregationBuilders.terms("group-uuid").field("uuid.keyword").order(Terms.Order.compound(
                Terms.Order.aggregation("avg-cpuuse", true)
        ));

        //求和字段1
        AvgAggregationBuilder avgCpu = AggregationBuilders.avg("avg-cpuuse").field("usage_idle");

        orderCpu.subAggregation(avgCpu);//添加到分组聚合请求中
        orderCpu.size(10);//top10限制

        FilterAggregationBuilder cpuAggregationBuilder = AggregationBuilders.filter("uuidFilter", uuidsBoolQuery)
                .subAggregation(AggregationBuilders.filter("timeFilter",timeFilter).subAggregation(orderCpu));

        SearchResponse response = elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu")
                .addAggregation(cpuAggregationBuilder)
                .get();

        InternalFilter uuidFilterRe = response.getAggregations().get("uuidFilter");
        InternalFilter timeFilterRe = uuidFilterRe.getAggregations().get("timeFilter");

        Terms tms =  timeFilterRe.getAggregations().get("group-uuid");
        //遍历每一个分组的key
        for(Terms.Bucket tbb:tms.getBuckets()){
            //获取count的和
            InternalAvg avg = tbb.getAggregations().get("avg-cpuuse");
            for (Map userResource : userResources) {
                Object uuid = userResource.get("uuid");
                if (uuid != null && !"".equals(uuid.toString())){
                    if (uuid.equals(tbb.getKey())){
                        userResource.put("cupPercent",numberFormat.format(100.0 - avg.getValue()));
                        cpuSort.add(userResource);
                    }
                }
            }
        }

3.过滤聚合求平均查询

//ES中查询所有主机的监控数据
        BoolQueryBuilder uuidsBoolQuery = QueryBuilders.boolQuery();

        uuidsBoolQuery.must(QueryBuilders.matchQuery("uuid", "1,2,4"));

        //暂定向前推一天,计算平均
        long end = System.currentTimeMillis();
        long start = end - 24 * 60 * 60 * 1000;
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(start,true).to(end,true);
        QueryBuilder timeFilter = QueryBuilders.boolQuery().must(rangeQueryBuilder);

        //开始查询Cpu平均使用率
        FilterAggregationBuilder cpuAggregationBuilder = AggregationBuilders.filter("uuidFilter", uuidsBoolQuery)
                .subAggregation(AggregationBuilders.filter("timeFilter",timeFilter)
                        .subAggregation(AggregationBuilders.avg("avgCpu").field("usage_idle")));


        SearchResponse response = elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu")
                .addAggregation(cpuAggregationBuilder)
                .get();

        InternalFilter uuidFilterRe = response.getAggregations().get("uuidFilter");
        InternalFilter timeFilterRe = uuidFilterRe.getAggregations().get("timeFilter");
        InternalAvg avgCpuRe = timeFilterRe.getAggregations().get("avgCpu");

        String cpupercent = "0.00";
        if (!"NaN".equals(avgCpuRe.getValue() + "")){
            cpupercent = numberFormat.format(100.0 - avgCpuRe.getValue());
        }

相关推荐