1. 程式人生 > >elasticsearch 常見查詢及聚合的JAVA API

elasticsearch 常見查詢及聚合的JAVA API

最近在研究ES 有點心得記錄下備忘

 ES 常見查詢

  • 根據ID 進行單個查詢
    GetResponse response = client.prepareGet("accounts", "person", "1")
                    .setOperationThreaded(false)
                    .get();

    相對於sql 的 select * from accounts.person  where id=1 ;

  • 分頁查詢所有記錄

    QueryBuilder qb=new MatchAllQueryBuilder();
            SearchResponse response= client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFrom(0)
                    .setSize(100).get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
    response.getHits()是所有命中記錄  相較於sql select * from accounts.person limit 100;
  • 根據多條件組合與查詢
     QueryBuilder qb=QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title","JAVA開發工程師")).must(QueryBuilders.termQuery("age",30)) ;
    
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }

    must 就像sql裡的and   相較於sql  select * from accounts.person where title='JAVA開發工程師' and age=30

  • 多條件或查詢

     QueryBuilder qb=QueryBuilders.termQuery("user","kimchy14");
            QueryBuilder qb1=QueryBuilders.termQuery("user","kimchy15");
    
            SortBuilder sortBuilder=SortBuilders.fieldSort("age");
            sortBuilder.order(SortOrder.DESC);
            QueryBuilder s=QueryBuilders.boolQuery().should(qb).should(qb1);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).addSort(sortBuilder).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
        

    should 就像sql裡的or  SortBuilder 的作用不言而喻就是用來排序 以上程式碼相較於sql  select * from   accounts.person where user='kimchy14' or  user='kimchy15'   ;       

  • 範圍查詢 

    // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
           // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(30 );
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gte(30 );
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }

    範圍查詢rangeQuery.from(30,true)方法是大於30  後面的引數是是否包含 為true的話就是大於等於30 to就相當於小於 如果也有包含引數為true的話就是小於等於  gt 是大於 gte是大於等於   lt是小於 lte是小於等於  第一句的builder就相當於 select * from accounts.person where age >=30 and age<=30;              

  • 包含查詢

     List<String> strs=new ArrayList<>();
            strs.add("kimchy14");
            strs.add("kimchy15");
            strs.add("kimchy16");
            QueryBuilder qb=QueryBuilders.termsQuery("user",strs);
    
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("age",null).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }

    包含查詢使用termsQuery 可以傳列表 也可以傳多個引數 或者陣列 setFetchSource有兩個引數 第一個引數是包含哪些引數 第二個引數是排除哪些引數   以上這段程式碼就相當於sql  select age from accounts.person where user in ('kimchy14','kimchy15','kimchy16');

  • 專門按id進行的包含查詢

    QueryBuilder qb=QueryBuilders.idsQuery(0+"");
    
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("age",null).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits(); 
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
  • 按萬用字元查詢

      QueryBuilder qb = QueryBuilders.wildcardQuery("user", "k*hy17*");
             //Fuzziness fuzziness=Fuzziness.fromEdits(2);
    
          // QueryBuilder qb = QueryBuilders.fuzzyQuery("user","mchy2").fuzziness(fuzziness);
            //QueryBuilder qb = QueryBuilders.prefixQuery("user", "kimchy2");
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(qb).setFetchSource("user",null).setFrom(0)
                    .setSize(100);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }

    萬用字元查詢像我們sql裡的like 但是還不一樣 like的百分號可以加到前後 但是萬用字元只能加在中間或者末尾 不能加在開頭   

  常見統計  統計分為指標 和 桶 桶就是我們統計的樣本  指標就是我們平時所查的count  sum  與sql不一樣的是 我們還可以將統計的樣本拿到 就是response.getHits

  • 統計count
    AggregationBuilder  termsBuilder = AggregationBuilders.count("ageCount").field("age");
    
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
            ValueCount valueCount= response.getAggregations().get("ageCount");
            long value=valueCount.getValue();

    這段程式碼就相當於 sql select count(age) ageCount form accounts.person  where age >=30 and age<=30   

  • 查詢最大值

     AggregationBuilder  termsBuilder = AggregationBuilders.max("max").field("age");
    
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
            Max valueCount= response.getAggregations().get("max");
            double value=valueCount.getValue();
             
  • 統計總和

     AggregationBuilder  termsBuilder = AggregationBuilders.sum("sum").field("age");
    
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
            Sum valueCount= response.getAggregations().get("sum");
            double value=valueCount.getValue();
  • 平均數

     AggregationBuilder  termsBuilder = AggregationBuilders.avg("avg").field("age");
    
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
            Avg valueCount= response.getAggregations().get("avg");
            double value=valueCount.getValue();
  • 統計樣本基本指標

            AggregationBuilder  termsBuilder = AggregationBuilders.stats("stats").field("age");
    
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(30,true);
            QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
            SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFrom(0).setSize(100).addAggregation(termsBuilder);
            logger.log(Level.INFO,sv.toString());
            SearchResponse response=  sv.get();
            SearchHits searchHits =  response.getHits();
            for(SearchHit hit:searchHits.getHits()){
                logger.log(Level.INFO , hit.getSourceAsString());
            }
            Stats valueCount= response.getAggregations().get("stats");
            logger.log(Level.INFO,"max"+valueCount.getMaxAsString());
            logger.log(Level.INFO,"avg"+valueCount.getAvgAsString());
            logger.log(Level.INFO,"sum"+valueCount.getSumAsString());
            logger.log(Level.INFO,"min"+valueCount.getMinAsString());
            logger.log(Level.INFO,"count"+valueCount.getCount());

    分組統計 相當於group by 後拿各組指標進行統計 

 分組求各組資料

 AggregationBuilder  termsBuilder = AggregationBuilders.terms("by_age").field("age");
        AggregationBuilder  sumBuilder=AggregationBuilders.sum("ageSum").field("age");
        AggregationBuilder  avgBuilder=AggregationBuilders.avg("ageAvg").field("age");
        AggregationBuilder  countBuilder=AggregationBuilders.count("ageCount").field("age");

        termsBuilder.subAggregation(sumBuilder).subAggregation(avgBuilder).subAggregation(countBuilder);
        //TermsAggregationBuilder all = AggregationBuilders.terms("age").field("age");
        //all.subAggregation(termsBuilder);
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(36,true);
        QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
        SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(s).setFetchSource(null,"gender").setFrom(0).setSize(100).addAggregation(termsBuilder);
        logger.log(Level.INFO,sv.toString());
        SearchResponse response=  sv.get();

        Aggregations terms= response.getAggregations();
        for (Aggregation a:terms){
            LongTerms teamSum= (LongTerms)a;
            for(LongTerms.Bucket bucket:teamSum.getBuckets()){
                logger.info(bucket.getKeyAsString()+"   "+bucket.getDocCount()+"    "+((Sum)bucket.getAggregations().asMap().get("ageSum")).getValue()+"    "+((Avg)bucket.getAggregations().asMap().get("ageAvg")).getValue()+"    "+((ValueCount)bucket.getAggregations().asMap().get("ageCount")).getValue());

            }
        }

第一行 termsBuilder 就相當於根據年齡對資料進行分組 group by   後面對sumBuilder avgBuilder countBuilder等就是在組內 求和 求平均數 求數量

 多分組求各組資料


        TermsAggregationBuilder all = AggregationBuilders.terms("by_gender").field("gender");
         AggregationBuilder age = AggregationBuilders.terms("by_age").field("age");
        AggregationBuilder  sumBuilder=AggregationBuilders.sum("ageSum").field("age");
        //AggregationBuilder  avgBuilder=AggregationBuilders.avg("ageAvg").field("age");
       // AggregationBuilder  countBuilder=AggregationBuilders.count("ageCount").field("age");
         all.subAggregation(age.subAggregation(sumBuilder));
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(30,true).to(32,true);
        QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder);//.must(qb5);
        SearchRequestBuilder sv=client.prepareSearch("accounts").setTypes("person").setQuery(rangeQueryBuilder).addAggregation(all);
        logger.log(Level.INFO,sv.toString());
        SearchResponse response=  sv.get();

        Aggregations terms= response.getAggregations();
        for (Aggregation a:terms){
            StringTerms stringTerms= (StringTerms)a;
             for(StringTerms.Bucket bucket:stringTerms.getBuckets()){
              //  logger.info(bucket.getKeyAsString());
                Aggregation aggs=bucket.getAggregations().getAsMap().get("by_age");
                 LongTerms terms1= (LongTerms)aggs;
                for (LongTerms.Bucket bu:terms1.getBuckets()){
                     logger.info(bucket.getKeyAsString()+"  "+bu.getKeyAsString()+" "+bu.getDocCount()+"    "+((Sum)bu.getAggregations().asMap().get("ageSum")).getValue());
                }

            }
        }

每增加一個分組指標就需要多加一個termsBuilder  其他等一切跟普通分組一樣 每次拿到

          以上就是我總結的基本的查詢 聚合 等常見功能 其他等諸如 求各組前多少資料是用topHits 這些基本夠我們日常操作了 。

          最後我們總結下    精確查詢用term 組合查詢用bool 範圍用range    and查詢用must    or查詢用should  not查詢用must not  常見的接收聚合返回結果的型別 ValueCount   AVG  SUM  MAX  MIN  按照英文意義就可以理解  分組聚合查詢時候還需要根據實際情況看是返回那種terms 

相關推薦

elasticsearch 常見查詢聚合JAVA API

最近在研究ES 有點心得記錄下備忘  ES 常見查詢 根據ID 進行單個查詢 GetResponse response = client.prepareGet("accounts", "person", "1") .setOperatio

Elasticsearch 之(43) Java API 實現 ES 的 增刪改查、聚合分析

package com.es.app; import java.net.InetAddress; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.Transpo

Elasticsearch 5.4.3實戰--Java API調用:搜索

fas ack 聚合 turn result ets str iterator ise ES有多種查詢方式,我自己的業務是需要對多個字段進行查詢,具體實現類代碼如下。 1 package com.cs99lzzs.elasticsearch.service.imp

ElasticSearch(十二) Bool Query JAVA API

contain app dex size 範圍 一起 util 子句 lte Bool Query 用於組合多個葉子或復合查詢子句的默認查詢 must 相當於 與 & = must not 相當於 非 ~ != should 相當於 或 | or fil

elasticsearch常見異常解決辦法

err error transport warn info onf conf earch org 報錯信息:Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000853300

elasticsearch常見錯誤解決方案

1.OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threa

Elasticsearch的學習以及其JAVA API的使用

  此文章主要整理Elasticsearch的實際使用中遇到的一些搜尋技巧以及JAVA API的呼叫方法。後續會不斷地補充。 簡單搜尋 一條搜尋的json語句如下: { "query": { ... } } 可以指定起始值和返回結果數

Elasticsearch 常見查詢

1、查詢所有 { "query": { "match_all": {} } } 2、count查詢 { "query": { "match_all": {} }, "aggs": { "agg_start_co

elasticsearch(一):JAVA api操作

1.建立一個mavan專案,專案的以來配置如下。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www

[Elasticsearch] 過濾查詢以及聚合(Filtering Queries and Aggregations) 5

過濾查詢以及聚合 A natural extension to aggregation scoping is filtering. Because the aggregation operates in the context of the query scope,

ElasticSearch常見問題解決方法

1、問題一: org.elasticsearch.bootstrap.StartupException:BindTransportException[Failed to bind to [9300]]; nested: BindException[無法指定被請求的地址];

大資料(hadoop-HDFS的本地開發環境的配置與常見的HDFS的JAVA API)

HDFS的本地開發環境搭建 1:所需工具 1)hadoop2.7.3安裝包 2)hadoop-eclipse-plugin

Elasticsearch(二):使用JAVA API實現簡單查詢聚合查詢

ES版本:2.3.1 JDK:1.8 所需要的jar包請在ES安裝路徑下的jars包中獲得,不要使用其他的jar否則容易出現版本問題! 注意:程式碼中TransportClient client=ESLink.getTransportClient()

elasticsearch elk最全java api 搜尋 聚合、巢狀查詢

一、 一般查詢... 2 (一) matchAllQuery(client). 2 (二) matchQu

Elasticsearch java api 常用查詢方法QueryBuilder構造舉例

舉例 div detail lang 模糊查詢 dna true builder earch 轉載:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查詢

Elasticsearch JAVA api輕松搞定groupBy聚合

ont text 遇到 初始化 演示 是我 getprop square 遍歷 本文給出如何使用Elasticsearch的Java API做類似SQL的group by聚合。 為了簡單起見,只給出一級groupby即group by field1(而不涉及到多級,例如

Elasticsearch Java API 查詢

tty pan prepare fault 統計 建立 rep clas ase 一.查詢的時候,需要建立一個SearchRequestBuilder,這裏面將給出對於哪一個index或者type進行查詢,並且所有的設置都可以在這裏面進行實現,例如模糊查詢,範圍查詢,前綴查

Elasticsearch - JAVA -API 多條件查詢資料

只為記錄這個批量查詢方法 條件設定 searchSourceBuilder.query(QueryBuilders.boolQuery() .should(QueryBuilders.matchQuery("name", filter.getKeyWo

ElasticsearchJava API/查詢/分頁等

Elasticsearch的Java API 新增Maven依賴。 連結到ES集 1.通過TransportClient這個介面,我們可以不啟動節點就可以和es叢集進行通訊,它需要指定es叢集中其中一

Elasticsearch 聚合查詢複合查詢

集合查詢 sum聚合 sum是一個求累加值的聚合,其作用與關係型資料庫中相同。 GET /lib4/items/_search { "size": 0, //表示查詢多少條文件,聚合只需就和結果,輸出文件可以設定為0條 "aggs"