如何在java中使用elasticsearch进行分组

  介绍

本篇文章给大家分享的是有关如何在java中使用elasticsearch进行分组,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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

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

1,表结构图片:

如何在java中使用elasticsearch进行分组

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

1。SQL:选择id, count(*)从taskid任务组;

java ES连接工具类

public  class  ESClientConnectionUtil  {   public 才能;static  TransportClient 客户=零;   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",,真的).build ();   ,,,,,,,,,//创建客户端   ,,,,,,,,,client =, new  PreBuiltTransportClient(设置).addTransportAddress (new  InetSocketTransportAddress (InetAddress.getByName(主机),端口));   ,,,,,,,},catch  (Exception 交货),{   ,,,,,,,,,ex.printStackTrace ();      ,,,,,,,,,System.out.println (ex.getMessage ());   ,,,,,,,}   ,,,,,}   ,,,}   ,,,return 客户端;   ,,}   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",,真的).build ();   ,,,,,,,,,//创建客户端   ,,,,,,,,,client =, new  PreBuiltTransportClient(设置).addTransportAddress (new  InetSocketTransportAddress (InetAddress.getByName(主机),端口));   ,,,,,,,},catch  (Exception 交货),{   ,,,,,,,,,ex.printStackTrace ();   ,,,,,,,,,System.out.println (ex.getMessage ());   ,,,,,}   ,,,}   ,,,return 客户端;   ,,}//才能判断索引是否存在   public 才能static  boolean  judgeIndex (String 指数){   ,,,客户=,getESClientConnection ();   ,,,,IndicesAdminClient  adminClient;   ,,,//查询索引是否存在   ,,,adminClient=, client.admin () .indices ();   ,,,IndicesExistsRequest  request =, new  IndicesExistsRequest(指数);   ,,,IndicesExistsResponse  responses =, adminClient.exists(请求).actionGet ();      ,,,if  (responses.isExists ()), {   ,,,,,return 真实;   ,,,}   ,,,return 假;   ,,}   }

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

//根据,任务id分组进行求和   ,SearchRequestBuilder  sbuilder =, client.prepareSearch (“hottopic") .setTypes (“hot");//根据taskid进行分组统计,统计出的列别名叫求和   ,TermsAggregationBuilder  termsBuilder =, AggregationBuilders.terms (“sum") .field (“taskid");      ,sbuilder.addAggregation (termsBuilder);   ,SearchResponse 反应=,sbuilder.execute () .actionGet ();//得到这个分组的数据集合   ,Terms  Terms =, responses.getAggregations () . get (“sum");   ,List lists =, new  ArrayList<在();   (int  i=0; i< terms.getBuckets () .size();我+ +){   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在java中使用elasticsearch进行分组