ES 6.1.3 java聚合查询并获取结果

有数据,然后按照host_ip,category 进行group by,再取traffic_index的最大值:

PUT traffic_infos/doc/1
{
  "host_ip":"10.42.28.10",
  "category":10012,
  "date":"2019-12-03T00:05:00",
  "traffic_index": 100
}
PUT traffic_infos/doc/2
{
  "host_ip":"10.42.28.11",
  "category":10012,
  "date":"2019-12-03T00:05:00",
  "traffic_index": 1000
}
PUT traffic_infos/doc/3
{
  "host_ip":"10.42.28.10",
  "category":10013,
  "date":"2019-12-03T00:05:00",
  "traffic_index": 500
}
PUT traffic_infos/doc/4
{
  "host_ip":"10.42.28.11",
  "category":10013,
  "date":"2019-12-03T00:05:00",
  "traffic_index": 1500
}
PUT traffic_infos/doc/5
{
  "host_ip":"10.42.28.10",
  "category":10012,
  "date":"2019-12-03T00:10:00",
  "traffic_index": 200
}
PUT traffic_infos/doc/6
{
  "host_ip":"10.42.28.11",
  "category":10012,
  "date":"2019-12-03T00:10:00",
  "traffic_index": 1200
}
PUT traffic_infos/doc/7
{
  "host_ip":"10.42.28.10",
  "category":10013,
  "date":"2019-12-03T00:10:00",
  "traffic_index": 700
}
PUT traffic_infos/doc/8
{
  "host_ip":"10.42.28.11",
  "category":10013,
  "date":"2019-12-03T00:10:00",
  "traffic_index": 1700
}

结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_ip": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "10.42.28.10",
          "doc_count": 4,
          "group_category": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 10012,
                "doc_count": 2,
                "max_index": {
                  "value": 200
                }
              },
              {
                "key": 10013,
                "doc_count": 2,
                "max_index": {
                  "value": 700
                }
              }
            ]
          }
        },
        {
          "key": "10.42.28.11",
          "doc_count": 4,
          "group_category": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 10012,
                "doc_count": 2,
                "max_index": {
                  "value": 1200
                }
              },
              {
                "key": 10013,
                "doc_count": 2,
                "max_index": {
                  "value": 1700
                }
              }
            ]
          }
        }
      ]
    }
  }
}

使用java查询:

import java.util.Map;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class EsForBLHighTotalTraffic2
{
    public static void main(String[] args)
    {      
        try
        {
            String startTime = "2019-12-03T00:00:00";
            String endTime = "2019-12-04T00:00:00";
            TransportClient client = EsOperation.getInstance();
            
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            
            
            BoolQueryBuilder searchQB = QueryBuilders.boolQuery();                
            searchQB.must(QueryBuilders.rangeQuery("date").gte(startTime).lt(endTime)
                    .includeLower(true).includeUpper(false));
            searchSourceBuilder.size(0);
            
            AggregationBuilder aggregation = 
            		AggregationBuilders.terms("group_ip").field("host_ip.keyword")
            		.subAggregation(AggregationBuilders.terms("group_category").field("category")
            				.subAggregation(AggregationBuilders.max("max_index").field("traffic_index"))
            		)
            		.size(Integer.MAX_VALUE);
            
            searchSourceBuilder.aggregation(aggregation);
            
            try
            {
                SearchRequest searchRequest = new SearchRequest("traffic_infos");
                searchRequest.types("doc"); 
                searchRequest.source(searchSourceBuilder);
                
                long haha = System.currentTimeMillis();
                
                SearchResponse searchResponse = client.search(searchRequest).get();
                Aggregations aggregations = searchResponse.getAggregations();
                //用来检测查询结果对不对,返回查询结果
//				for (Aggregation a:aggregations)
//				{
//					StringTerms stringTerms= (StringTerms)a;
//					System.out.println(stringTerms);
//				}
                Terms ip_Aggregations = searchResponse.getAggregations().get("group_ip");
                for (Terms.Bucket ip_buck : ip_Aggregations.getBuckets())
                {
                	String ip = ip_buck.getKeyAsString();
					Long ip_docCount = ip_buck.getDocCount();
					System.out.println(ip);
					System.out.println(ip_docCount);
                	Map<String, Aggregation> aggregationMap = ip_buck.getAggregations().getAsMap();
                	Terms categoryAggregation = (Terms) aggregationMap.get("group_category");
                	for (Terms.Bucket category_buck : categoryAggregation.getBuckets())
                	{
                		String category = category_buck.getKeyAsString();
    					Long category_docCount = category_buck.getDocCount();
    					System.out.println(category);
    					System.out.println(category_docCount);
    					double maxIndex = ((Max)category_buck.getAggregations().get("max_index")).getValue();
    					System.out.println(maxIndex);
                	}
                }
            }
            catch(Exception e)
            {
            	e.printStackTrace();
            } 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        EsOperation es = new EsOperation();
        es.closeClient();
  
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值