最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

根據(jù)任務(wù)id分組,分別統(tǒng)計(jì)出每個(gè)任務(wù)id下有多少個(gè)文字標(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)計(jì),統(tǒng)計(jì)出的列別名叫sum
 TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");

 sbuilder.addAggregation(termsBuilder);
 SearchResponse responses= sbuilder.execute().actionGet();
//得到這個(gè)分組的數(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)計(jì)的數(shù)量和id值

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

//根據(jù) 任務(wù)id分組進(jìn)行求和
 SearchRequestBuilder sbuilder = client.prepareSearch("hottopic").setTypes("hot");
//根據(jù)taskid進(jìn)行分組統(tǒng)計(jì),統(tǒng)計(jì)出的列別名叫sum
 TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("sum").field("taskid");
//根據(jù)第二個(gè)字段進(jìn)行分組
 TermsAggregationBuilder aAggregationBuilder2 = AggregationBuilders.terms("region_count").field("birthplace");
//如果存在第三個(gè),以此類推;
 sbuilder.addAggregation(termsBuilder.subAggregation(aAggregationBuilder2));
 SearchResponse responses= sbuilder.execute().actionGet();
//得到這個(gè)分組的數(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)計(jì)的數(shù)量和id值

對多個(gè)field求max/min/sum/avg

SearchRequestBuilder requestBuilder = client.prepareSearch("hottopic").setTypes("hot");
//根據(jù)taskid進(jìn)行分組統(tǒng)計(jì),統(tǒng)計(jì)別名為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("編號(hào)=" + bucket.getKey() + ";平均=" + terms2.getValue() + ";總=" + term3.getValue());
    }

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

相關(guān)文章

  • Spring—@Value在static中引用方式

    Spring—@Value在static中引用方式

    這篇文章主要介紹了Spring—@Value在static中引用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java實(shí)現(xiàn)TopK問題的方法

    Java實(shí)現(xiàn)TopK問題的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)TopK問題的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Hibernate中實(shí)現(xiàn)增刪改查的步驟詳解

    Hibernate中實(shí)現(xiàn)增刪改查的步驟詳解

    本篇文章主要介紹了Hibernate中實(shí)現(xiàn)增刪改查的步驟與方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • IntelliJ IDEA2019 安裝lombok的實(shí)現(xiàn)

    IntelliJ IDEA2019 安裝lombok的實(shí)現(xiàn)

    這篇文章主要介紹了IntelliJ IDEA2019 安裝lombok的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • SpringSecurity HttpSecurity 類處理流程分析

    SpringSecurity HttpSecurity 類處理流程分析

    SpringSecurity在SSM項(xiàng)目中使用基于配置文件,通過XML標(biāo)簽定義認(rèn)證信息,HttpSecurity在SpringBoot中通過代碼配置實(shí)現(xiàn)與XML相同功能,詳細(xì)介紹了HttpSecurity的類結(jié)構(gòu)、處理過程及其與SecurityBuilder的關(guān)系,感興趣的朋友一起看看吧
    2024-09-09
  • 詳解Java打包及上傳到私服務(wù)的方法

    詳解Java打包及上傳到私服務(wù)的方法

    這篇文章主要介紹了Java打包及上傳到私服務(wù)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)的解決方案

    Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了Mybatis 中Mapper使用package方式配置報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java從零編寫汽車租賃系統(tǒng)全程分析

    Java從零編寫汽車租賃系統(tǒng)全程分析

    這篇文章介紹了Java實(shí)現(xiàn)汽車租賃系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼

    SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼

    本文主要介紹了SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • java通過URLClassLoader類加載器加載外部jar代碼示例

    java通過URLClassLoader類加載器加載外部jar代碼示例

    ClassLoader翻譯過來就是類加載器,普通的java開發(fā)者其實(shí)用到的不多,但對于某些框架開發(fā)者來說卻非常常見,下面這篇文章主要給大家介紹了關(guān)于java通過URLClassLoader類加載器加載外部jar的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評(píng)論

沙洋县| 昆明市| 开封县| 正阳县| 尖扎县| 西盟| 治县。| 日土县| 大连市| 麦盖提县| 东兴市| 汕尾市| 闵行区| 舒兰市| 舒兰市| 铜鼓县| 丰原市| 马公市| 峨边| 建昌县| 太康县| 高密市| 绥德县| 巴里| 许昌县| 西平县| 敦化市| 民权县| 泰安市| 洛宁县| 新营市| 广安市| 永吉县| 从化市| 汉沽区| 永川市| 大丰市| 灵武市| 朝阳县| 湛江市| 尼木县|