当前位置:   article > 正文

elasticsearch-java api之搜索(二)——聚合_getaggregations

getaggregations

前面一篇文章描述了es一些基本搜索的用法(match、term、fruzzy、matchPhraseQuery等),这篇文章我们着重讲解一下聚合查询的用法。

假设es中有如下数据


1、group by /count:

select team,count(*) from table group by team;

1)代码:

  1. public static void aggre1Query2(String indexName,String indexType) {
  2. SearchRequestBuilder srb = transportClient.prepareSearch(indexName).setTypes(indexType);
  3. srb.setSearchType(SearchType.COUNT);
  4. TermsBuilder teamAgg= AggregationBuilders.terms("player_count").field("team");
  5. srb.addAggregation(teamAgg);
  6. System.out.println(srb.toString());
  7. SearchResponse searchResponse = srb.execute().actionGet();
  8. Aggregations aggregations = searchResponse.getAggregations();
  9. Map<String, Aggregation> asMap = aggregations.asMap();
  10. Terms terms = (Terms)asMap.get("player_count");
  11. List<Bucket> buckets = terms.getBuckets();
  12. for (Bucket bt : buckets) {
  13. logger.info(bt.getKeyAsString() + " :: " + bt.getDocCount());
  14. }
  15. }

2)输出的dsl:

  1. {
  2. "aggregations" : {
  3. "player_count" : {
  4. "terms" : {
  5. "field" : "team"
  6. }
  7. }
  8. }
  9. }
3)输出:

  1. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:461] war :: 3
  2. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:461] cav :: 2
  3. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:461] tim :: 1

2、group by 多个field/count、min、avg:

1)代码:

  1. public static void aggreQuery1(String indexName,String indexType) {
  2. SearchRequestBuilder srb = transportClient.prepareSearch(indexName).setTypes(indexType);
  3. srb.setSearchType(SearchType.COUNT);
  4. TermsBuilder teamAgg= AggregationBuilders.terms("team_count").field("team");
  5. TermsBuilder positionAgg= AggregationBuilders.terms("position_count").field("position");
  6. teamAgg.subAggregation(positionAgg);
  7. srb.addAggregation(teamAgg);
  8. System.out.println(srb.toString());
  9. SearchResponse searchResponse = srb.execute().actionGet();
  10. Aggregations aggregations = searchResponse.getAggregations();//team agg
  11. Terms terms = aggregations.get("team_count");
  12. List<Bucket> buckets = terms.getBuckets();
  13. for (Bucket bt : buckets) {
  14. logger.info(bt.getKeyAsString() + " :: " + bt.getDocCount());
  15. Aggregations aggregations2 = bt.getAggregations();//position agg
  16. Terms terms2 = aggregations2.get("position_count");
  17. List<Bucket> buckets2 = terms2.getBuckets();
  18. for (Bucket bt2 : buckets2) {
  19. logger.info("---"+bt2.getKeyAsString() + " :: " + bt2.getDocCount());
  20. }
  21. }
  22. }
2)输出的dsl:

  1. {
  2. "aggregations" : {
  3. "team_count" : {
  4. "terms" : {
  5. "field" : "team"
  6. },
  7. "aggregations" : {
  8. "position_count" : {
  9. "terms" : {
  10. "field" : "position"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
3)输出结果:

  1. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:430] war :: 3
  2. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---pf :: 1
  3. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---pg :: 1
  4. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---sg :: 1
  5. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:430] cav :: 2
  6. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---pg :: 1
  7. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---sf :: 1
  8. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:430] tim :: 1
  9. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:436] ---pf :: 1

4)说明:

多个field的groupby 是一层一层的,所以代码中有两个for循环进行输出。


3、查询条件+max/min/sum/avg聚合+排序:

1)代码

  1. public static void aggre1Query3(String indexName,String indexType) {
  2. boolean min = true;
  3. boolean avg = true;
  4. String groupCol = "team";
  5. SearchRequestBuilder srb = transportClient.prepareSearch(indexName).setTypes(indexType);
  6. srb.setSearchType(SearchType.COUNT);
  7. QueryBuilder qb = QueryBuilders.rangeQuery("age").from(10).to(39);//查询条件
  8. AggregationBuilder aggBuilder= AggregationBuilders.terms("group_name").field(groupCol).order(Order.aggregation("minSalary", false));
  9. if (min) {
  10. aggBuilder.subAggregation(AggregationBuilders.min("minSalary").field("salary"));
  11. }
  12. if (avg) {
  13. aggBuilder.subAggregation(AggregationBuilders.avg("avgSalary").field("salary"));
  14. }
  15. srb.setQuery(qb).addAggregation(aggBuilder);
  16. System.out.println(srb.toString());
  17. SearchResponse searchResponse = srb.execute().actionGet();
  18. Aggregations aggregations = searchResponse.getAggregations();
  19. Map<String, Aggregation> asMap = aggregations.asMap();
  20. Terms terms = (Terms)asMap.get("group_name");
  21. List<Bucket> buckets = terms.getBuckets();
  22. for (Bucket bt : buckets) {
  23. logger.info(bt.getKeyAsString() + " :: " + bt.getDocCount());
  24. Aggregations aggregations2 = bt.getAggregations();
  25. Min minSalary = aggregations2.get("minSalary");
  26. Avg avgSalary = aggregations2.get("avgSalary");
  27. logger.info("min:"+minSalary.value()+",avg:"+avgSalary.value());
  28. }
  29. }
2)dsl输出:

  1. {
  2. "query" : {
  3. "range" : {
  4. "age" : {
  5. "from" : 10,
  6. "to" : 39,
  7. "include_lower" : true,
  8. "include_upper" : true
  9. }
  10. }
  11. },
  12. "aggregations" : {
  13. "group_name" : {
  14. "terms" : {
  15. "field" : "team",
  16. "order" : {
  17. "minSalary" : "desc"
  18. }
  19. },
  20. "aggregations" : {
  21. "minSalary" : {
  22. "min" : {
  23. "field" : "salary"
  24. }
  25. },
  26. "avgSalary" : {
  27. "avg" : {
  28. "field" : "salary"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
3)结果输出:

  1. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:499] cav :: 2
  2. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:504] min:2000.0,avg:2500.0
  3. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:499] war :: 3
  4. [12-05 18:51:32] [INFO] [cn.edu.nuc.EsTest.EsDao4Search:504] min:1000.0,avg:1666.6666666666667
4)说明:

该例子是一个field的groupby,所以代码中只要一个for循环即可;同时获取min和max等聚合项。


4、聚合后返回的条数:

默认情况下,search执行后,仅返回10条聚合结果,如果想反悔更多的结果,需要在构建TermsBuilder 时指定size:

TermsBuilder teamAgg= AggregationBuilders.terms("team").size(15);  



参考:

http://blog.csdn.net/carlislelee/article/details/52598022
http://outofmemory.cn/code-snippet/38461/elasticsearch-aggregation-search-example

http://blog.csdn.net/it_lihongmin/article/details/78447001

http://blog.csdn.net/jacklin929/article/details/70304127










声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/68148
推荐阅读
相关标签
  

闽ICP备14008679号