这篇文章主要介绍了java使用elasticsearch分组进行聚合查询过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

java连接elasticsearch 进行聚合查询进行相应操作

一:对单个字段进行分组求和

1、表结构图片:

根据任务id分组,分别统计出每个任务id下有多少个文字标题

1.SQL:select id, count(*) as sum from task group by taskid;

java ES连接工具类

public class ESClientConnectionUtil {

public static TransportClient client=null;

public final static String HOST = "192.168.200.211"; //服务器部署

public final static Integer PORT = 9301; //端口

public static TransportClient getESClient(){

System.setProperty("es.set.netty.runtime.available.processors", "false");

if (client == null) {

synchronized (ESClientConnectionUtil.class) {

try {

//设置集群名称

Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();

//创建client

client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));

} catch (Exception ex) {

ex.printStackTrace();

System.out.println(ex.getMessage());

}

}

}

return client;

}

public static TransportClient getESClientConnection(){

if (client == null) {

System.setProperty("es.set.netty.runtime.available.processors", "false");

try {

//设置集群名称

Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();

//创建client

client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));

} catch (Exception ex) {

ex.printStackTrace();

System.out.println(ex.getMessage());

}

}

return client;

}

//判断索引是否存在

public static boolean judgeIndex(String index){

client= getESClientConnection();

IndicesAdminClient adminClient;

//查询索引是否存在

adminClient= client.admin().indices();

IndicesExistsRequest request = new IndicesExistsRequest(index);

IndicesExistsResponse responses = adminClient.exists(request).actionGet();

if (responses.isExists()) {

return true;

}

return false;

}

}

java ES语句(根据单列进行分组求和)

//根据 任务id分组进行求和

SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");

//根据taskid进行分组统计,统计出的列别名叫sum

TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");

sbuilder.addAggregation(termsBuilder);

SearchResponse responses= sbuilder.execute().actionGet();

//得到这个分组的数据集合

Terms terms = responses.getAggregations().get("sum");

List lists = new ArrayList<>();

for(int i=0;i

//statistics

String id =terms.getBuckets().get(i).getKey().toString();//id

Long sum =terms.getBuckets().get(i).getDocCount();//数量

System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());

}

//分别打印出统计的数量和id值

根据多列进行分组求和

//根据 任务id分组进行求和

SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");

//根据taskid进行分组统计,统计出的列别名叫sum

TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");

//根据第二个字段进行分组

TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace");

//如果存在第三个,以此类推;

sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));

SearchResponse responses= sbuilder.execute().actionGet();

//得到这个分组的数据集合

Terms terms = responses.getAggregations().get("sum");

List lists = new ArrayList<>();

for(int i=0;i

//statistics

String id =terms.getBuckets().get(i).getKey().toString();//id

Long sum =terms.getBuckets().get(i).getDocCount();//数量

System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());

}

//分别打印出统计的数量和id值

对多个field求max/min/sum/avg

SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot");

//根据taskid进行分组统计,统计别名为sum

TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("sum").field("taskid")

//根据tasktatileid进行升序排列

.order(Order.aggregation("tasktatileid", true));

// 求tasktitleid 进行求平均数 别名为avg_title

AggregationBuilder aggregationBuilder2 = AggregationBuilders.avg("avg_title").field("tasktitleid");

//

AggregationBuilder aggregationBuilder3 = AggregationBuilders.sum("sum_taskid").field("taskid");

requestBuilder.addAggregation(aggregationBuilder1.subAggregation(aggregationBuilder2).subAggregation(aggregationBuilder3));

SearchResponse response = requestBuilder.execute().actionGet();

Terms aggregation = response.getAggregations().get("sum");

Avg terms2 = null;

Sum term3 = null;

for (Terms.Bucket bucket : aggregation.getBuckets()) {

terms2 = bucket.getAggregations().get("avg_title"); // org.elasticsearch.search.aggregations.metrics.avg.InternalAvg

term3 = bucket.getAggregations().get("sum_taskid"); // org.elasticsearch.search.aggregations.metrics.sum.InternalSum

System.out.println("编号=" + bucket.getKey() + ";平均=" + terms2.getValue() + ";总=" + term3.getValue());

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

java操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询过程解析...相关推荐

  1. java操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询(group by)-项目中实际应用...

    java连接elasticsearch 进行聚合查询进行相应操作 一:对单个字段进行分组求和 1.表结构图片: 根据任务id分组,分别统计出每个任务id下有多少个文字标题 1.SQL:select i ...

  2. java 对es的操作

    1.创建连接: SearchRequest request = new SearchRequest(GlobalConstant.INDEX_NAME_NEW);SearchSourceBuilder ...

  3. 20 个超级使用的 Java 8 Stream,玩转集合的筛选、归约、分组、聚合

    先贴上几个案例,水平高超的同学可以挑战一下: 1. 从员工集合中筛选出salary大于8000的员工,并放置到新的集合里. 2. 统计员工的最高薪资.平均薪资.薪资之和. 3. 将员工按薪资从高到低排 ...

  4. java界面怎么加图片不显示不出来_Java登录界面中添加背景图片,程序无错,但加载不了图片,求帮忙...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 import java.awt.*; import javax.swing.*; public class Demo extends JFrame { p ...

  5. java中怎么让字体可以显示下划线呢_java中怎么让字体可以显示下划线呢

    在.net中,对于Font类来说,可以直接使用常量来生成带有下划线的字体. 但是,到了Java中,想生成带有下划线的字体,就稍微复杂了一点,需要借助于TextAttribute类来生成.(推荐:jav ...

  6. java中怎么让字体可以显示下划线呢_Java如何在数字文字中使用下划线?

    在代码中写一长串数字是一件很难读的东西.在JDK 7引入的新功能中,现在允许我们使用下划线字符来写数字文字,以打断数字以使其更易于阅读. 在以下示例中,您可以看到如何在数字文字中使用下划线.您会发现, ...

  7. servlet中显示mysql字段_Java Servlet:从数据库获取信息并在屏幕上显示它

    创建它代表了表的每一个项目(行)一个JavaBean类.创建一个使用JDBC返回这些项目列表的DAO类.然后在servlet中,只需使用HttpServletRequest#setAttribute( ...

  8. java 入参校验_Java Validation方法入参校验实现过程解析

    一.前言 在日常的开发中,经常需要对方法参数进行校验(非空.长度等).如果采用hardcode去校验(if..else..),会造成代码冗余,复用性低,导致维护成本比较高.借助Validation组件 ...

  9. java操作es聚合操作并显示其他字段_深入浅析Elasticsearch中的聚合操作

    如果写过Elasticsearch的聚合操作DSL,都知道它非常的繁琐,很简单的业务就导致异常复杂的json.因为它的聚合操作是嵌套的,一个聚合的输出可以是另一个聚合的输入,并且聚合还支持pipeli ...

最新文章

  1. 同济大学和东南大学计算机哪个好,东南大学和同济大学哪个好?选择哪个以后发展比较好?...
  2. linux ftp服务器搭建及用户的分配,Linux搭建FTP服务器
  3. python爬虫新手项目-给新手推荐几个实用又适合上手的Python爬虫项目
  4. Eclipse配置Android开发环境
  5. Qt Creator支持平台
  6. 1123:图像相似度
  7. TCP/IP数据包结构分解
  8. long long or int
  9. Linux 管理员技术
  10. 我心中的核心组件(可插拔的AOP)~大话开篇及目录
  11. JAVA实现中点画线_Java 实现中点法画线
  12. SpringCloud整合Feign的调用源码流程解析
  13. 我要换博客啦~Github+Hexo~Windows版本
  14. sus补丁分发,局域网自动打补丁服务的架设过程
  15. debezium系列之:理解database.server.name和database.history.kafka.topic
  16. 熔断机制什么意思_熔断机制是什么意思(图文)
  17. 180个非常有用的电脑知识
  18. IMP遇到IMP-00009错误
  19. Python爬取链家网上的房源信息
  20. 【Python】np.where()替换缺失值

热门文章

  1. ubuntu利用apt-get卸载软件
  2. php 删除硬链接,RHCE系列之文件管理----硬链接和软链接
  3. 给接口自动化测试框架增色,实现企业微信测试报告
  4. java环境变量javac不能成功 win7_Java开发:Java环境搭建
  5. jQuery实现浮动层跟随页面滚动效果
  6. atomikos mysql,记一次 Atomikos 分布式事务的使用
  7. protoc支持c_protoc 命令参数
  8. 怎么进入mysql workspace_MySQL 继续-- Win7 安装及后续工作
  9. c语言怎么倒计时,如何在c语言程序中插入一个倒计时命令?
  10. 杀毒时能否使用计算机,电脑杀毒以后,程序无法使用,电脑杀毒后共享不能使用-...