欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用elasticsearch分組進(jìn)行聚合查詢過程解析

 更新時間:2020年02月14日 12:03:40   作者:陳遠(yuǎn)波  
這篇文章主要介紹了java使用elasticsearch分組進(jìn)行聚合查詢過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了java使用elasticsearch分組進(jìn)行聚合查詢過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

java連接elasticsearch 進(jìn)行聚合查詢進(jìn)行相應(yīng)操作

一:對單個字段進(jìn)行分組求和

1、表結(jié)構(gòu)圖片:

根據(jù)任務(wù)id分組,分別統(tǒng)計出每個任務(wù)id下有多少個文字標(biāo)題

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"; //服務(wù)器部署
  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 {
          //設(shè)置集群名稱
          Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();
          //創(chuàng)建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 {
          //設(shè)置集群名稱
          Settings settings = Settings.builder().put("cluster.name", "es5").put("client.transport.sniff", true).build();
          //創(chuàng)建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語句(根據(jù)單列進(jìn)行分組求和)

//根據(jù) 任務(wù)id分組進(jìn)行求和
 SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
//根據(jù)taskid進(jìn)行分組統(tǒng)計,統(tǒng)計出的列別名叫sum
 TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");

 sbuilder.addAggregation(termsBuilder);
 SearchResponse responses= sbuilder.execute().actionGet();
//得到這個分組的數(shù)據(jù)集合
 Terms terms = responses.getAggregations().get("sum");
 List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
for(int i=0;i<terms.getBuckets().size();i++){
  //statistics
  String id =terms.getBuckets().get(i).getKey().toString();//id
  Long sum =terms.getBuckets().get(i).getDocCount();//數(shù)量
System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());
}
//分別打印出統(tǒng)計的數(shù)量和id值

根據(jù)多列進(jìn)行分組求和

//根據(jù) 任務(wù)id分組進(jìn)行求和
 SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
//根據(jù)taskid進(jìn)行分組統(tǒng)計,統(tǒng)計出的列別名叫sum
 TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");
//根據(jù)第二個字段進(jìn)行分組
 TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace");
//如果存在第三個,以此類推;
 sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));
 SearchResponse responses= sbuilder.execute().actionGet();
//得到這個分組的數(shù)據(jù)集合
 Terms terms = responses.getAggregations().get("sum");
 List<BsKnowledgeInfoDTO> lists = new ArrayList<>();
for(int i=0;i<terms.getBuckets().size();i++){
  //statistics
  String id =terms.getBuckets().get(i).getKey().toString();//id
  Long sum =terms.getBuckets().get(i).getDocCount();//數(shù)量
System.out.println("=="+terms.getBuckets().get(i).getDocCount()+"------"+terms.getBuckets().get(i).getKey());
}
//分別打印出統(tǒng)計的數(shù)量和id值

對多個field求max/min/sum/avg

SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot");
//根據(jù)taskid進(jìn)行分組統(tǒng)計,統(tǒng)計別名為sum
    TermsAggregationBuilder aggregationBuilder1 = AggregationBuilders.terms("sum").field("taskid") 
//根據(jù)tasktatileid進(jìn)行升序排列
        .order(Order.aggregation("tasktatileid", true));
// 求tasktitleid 進(jìn)行求平均數(shù) 別名為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());
    }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Maven pom.xml 添加本地jar包依賴以及打包方法

    Maven pom.xml 添加本地jar包依賴以及打包方法

    這篇文章主要介紹了Maven pom.xml 添加本地jar包依賴以及打包方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • maven項(xiàng)目錯誤:找不到或無法加載主類?XXX問題

    maven項(xiàng)目錯誤:找不到或無法加載主類?XXX問題

    這篇文章主要介紹了maven項(xiàng)目錯誤:找不到或無法加載主類?XXX問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • FluentMybatis實(shí)現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法

    FluentMybatis實(shí)現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法

    本文主要介紹了FluentMybatis實(shí)現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中的日期時間類實(shí)例詳解(Date、Calendar、DateFormat)

    Java中的日期時間類實(shí)例詳解(Date、Calendar、DateFormat)

    在JDK1.0中,Date類是唯一的一個代表時間的類,但是由于Date類不便于實(shí)現(xiàn)國際化,所以從JDK1.1版本開始,推薦使用Calendar類進(jìn)行時間和日期處理,這篇文章主要介紹了Java中的日期時間類詳解(Date、Calendar、DateFormat),需要的朋友可以參考下
    2023-11-11
  • 通過url方式傳遞中文亂碼的解決方法

    通過url方式傳遞中文亂碼的解決方法

    本篇文章主要是對通過url方式傳遞中文亂碼的解決方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12
  • Java Stream 流實(shí)現(xiàn)合并操作示例

    Java Stream 流實(shí)現(xiàn)合并操作示例

    這篇文章主要介紹了Java Stream 流實(shí)現(xiàn)合并操作,結(jié)合實(shí)例形式詳細(xì)分析了Java Stream 流實(shí)現(xiàn)合并操作原理與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • Java中的Enum枚舉使用方法解析

    Java中的Enum枚舉使用方法解析

    這篇文章主要介紹了Java中的Enum枚舉使用方法解析,枚舉是一種數(shù)據(jù)類型,和int、double、string等類型相似,枚舉用于聲明一組命名的常數(shù),當(dāng)一個變量有幾種可能的取值時,可以將它定義為枚舉類型,需要的朋友可以參考下
    2023-11-11
  • Java集合刪除元素ArrayList實(shí)例詳解

    Java集合刪除元素ArrayList實(shí)例詳解

    這篇文章主要介紹了Java集合刪除元素ArrayList實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • java實(shí)現(xiàn)京東登陸示例分享

    java實(shí)現(xiàn)京東登陸示例分享

    這篇文章主要介紹了使用java實(shí)現(xiàn)的京東商城登陸示例,需要的朋友可以參考下
    2014-03-03
  • Spring集成Druid連接池及監(jiān)控配置的全過程

    Spring集成Druid連接池及監(jiān)控配置的全過程

    java程序很大一部分要操作數(shù)據(jù)庫,為了提高性能操作數(shù)據(jù)庫的時候,有不得不使用數(shù)據(jù)庫連接池,下面這篇文章主要給大家介紹了關(guān)于Spring集成Druid連接池及監(jiān)控配置的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論