1. 程式人生 > >ElasticSearch2.3.4之Java Api呼叫例子

ElasticSearch2.3.4之Java Api呼叫例子

ElasticSearch2.3.4

序號 覆蓋功能例子
1 客戶端連結初始化
2 filte無評分查詢用法
3 query有評分查詢用法
4 單欄位分組用法
5 多欄位分組用法
6 讀取有索引無儲存資料的用法
7 設定指定欄位返回

程式碼如下:

package com.curd.es;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import
org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator; import
org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import
org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.joda.time.DateTime; import java.net.InetSocketAddress; import java.util.List; import java.util.Map; public classElasticSearchToolsTest{ //elasticsearch2.3的客戶端例項 static Client client=null; static { //設定叢集名字 Settings settings = Settings.settingsBuilder() .put("cluster.name", "search") .put("client.transport.sniff", true) . build(); try { //初始化連線客戶端 client = new TransportClient.Builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.5",9300))) .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.6",9300))) .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.7",9300))); }catch (Exception e){ e.printStackTrace(); } } /**** * 測試讀取僅僅索引沒有儲存的欄位,從倒排裡面載入資料 * 過濾資料 */ public static void filterQueryString(){ //構建查詢請求 SearchRequestBuilder search=client.prepareSearch("monitor").setTypes("monitor"); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery() .must(QueryBuilders.queryStringQuery("*:*")) .filter(QueryBuilders.queryStringQuery("+mtitle:北京奇虎科技 +dtime:[1443196800000 TO 1451059200000] ").defaultOperator(Operator.AND)); search.setQuery(boolQuery); search.addFieldDataField("t1");//索引,沒有儲存的域,從field data裡面載入資料 search.addFieldDataField("t2");//索引,沒有儲存的域,從field data裡面載入資料 search.addFieldDataField("t3");//索引,沒有儲存的域,從field data裡面載入資料 search.addFieldDataField("dtime");//索引,沒有儲存的域,從field data裡面載入資料 SearchResponse r = search.get(); for(SearchHit hit:r.getHits()){ String id=hit.getId(); String index=hit.getIndex(); String type=hit.getType(); String t1=hit.getFields().get("t1").getValue(); String t2=hit.getFields().get("t2").getValue(); String t3=hit.getFields().get("t3").getValue(); Long time=hit.getFields().get("dtime").getValue(); Float score=hit.getScore(); System.out.println(id+"\t"+t1+"\t"+t2+"\t"+t3+"\t"+time+"\t"+score); } System.out.println("總數量:"+r.getHits().getTotalHits()); System.out.println("耗時:"+r.getTookInMillis()); } public static void main(String[] args) throws Exception { //單個聚合欄位,支援.寫法 String aggField="data.subjectId"; // testOneAggString(aggField); testFilter(); client.close(); }
    /***
    * 每一天的select count(distinct(actid)) from talbe group by date
     */
public static void countDistinctByField(){


      //構造search請求
SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 8, 0, 0, 0).getMillis()
            +" TO "+new DateTime(2016, 8, 15, 0, 0, 0).getMillis()+"}"
));
search.setSize(0);
//一級分組欄位
DateHistogramBuilder dateagg = AggregationBuilders.dateHistogram("dateagg");
dateagg.field("@timestamp");//聚合時間欄位
//    dateagg.interval(DateHistogramInterval.HOUR);//按小時聚合
dateagg.interval(DateHistogramInterval.DAY);//按天聚合
//    dateagg.format("yyyy-MM-dd HH"); //格式化時間
dateagg.format("yyyy-MM-dd"); //格式化時間
dateagg.timeZone("Asia/Shanghai");//設定時區,注意如果程式部署在其他國家使用時,使用Joda-Time來動態獲取時區 new DateTime().getZone()
      //二級分組欄位
//    TermsBuilder twoAgg = AggregationBuilders.terms("stragg").field("actId");
MetricsAggregationBuilder twoAgg = AggregationBuilders.cardinality("stragg").field("actId");
//組裝聚合欄位
dateagg.subAggregation(twoAgg);
//search請求新增
search.addAggregation(dateagg);
//獲取結果
SearchResponse r = search.get();
Histogram h = r.getAggregations().get("dateagg");
//得到一級聚合結果裡面的分桶集合
List<Histogram.Bucket> buckets = (List<Histogram.Bucket>) h.getBuckets();
//遍歷分桶集
for(Histogram.Bucket b:buckets){
         //讀取二級聚合資料集引用
Aggregations sub = b.getAggregations();
//獲取二級聚合集合
Cardinality agg = sub.get("stragg");
//獲取去重後的值
long value = agg.getValue();
//如果設定日期的format的時候,需要使用keyAsString取出,否則獲取的是UTC的標準時間
System.out.println(b.getKeyAsString() +"  " +b.getDocCount()+" "+value);
}
   }



    /***
     *  獲取search請求的結果,並輸出列印結果資訊
     * @param search
     * @throws Exception
     */
    public  static void showResult(SearchRequestBuilder search) throws Exception{
        SearchResponse r = search.get();//得到查詢結果
        for(SearchHit hits:r.getHits()){
            //只能獲取addFields裡面新增的欄位
//            System.out.println(hits.getFields().get("userId").getValue());
            //預設可會source裡面獲取所需欄位
            System.out.println(hits.getSource().get("actId"));
            //注意不支援data.subjectName這樣的訪問方式
            //System.out.println(hits.getId()+"  "+hits.score()+"  "+data.get("subjectName"));
            //如果是個巢狀json,需要轉成map後,訪問其屬性
//            Map data=(Map) hits.getSource().get("data");
//            System.out.println(hits.getId()+"  "+hits.score()+"  "+data.get("subjectName"));


        }
        long hits=r.getHits().getTotalHits();//讀取命中數量
        System.out.println(hits);
    }

    /***
     * 最新版elasticsearch2.3的query測試,結果會評分
     * @throws Exception
     */
    public static void testQuery() throws Exception{
        SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
        String subjectName="語文";
        //注意查詢的時候,支援巢狀的json查詢,通過點符號訪問下層欄位,讀取結果時不支援這種方式
         search.setQuery(QueryBuilders.queryStringQuery("+data.subjectName:* -data.subjectName:"+subjectName+"  "));
        showResult(search);
    }

    /***
     * 最新版的elasticsearch2.3的filterquery測試,結果不會評分
     * @throws Exception
     */
    public static void testFilter() throws Exception{
        SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
        //第一個引數包含的欄位陣列,第二個欄位排除的欄位陣列
//        search.setFetchSource(new String[]{"userId","actId"},null);
//        search.addFields("actId","userId"); //另一種寫法
        String schoolName="沙河市第三小學";
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.queryStringQuery("*:*"))
                .filter(QueryBuilders.queryStringQuery("+data.subjectName:* +schoolName:"+schoolName).defaultOperator(Operator.AND));
        //設定query
        search.setQuery(boolQuery);
        //列印結果資料
        showResult(search);
    }

    /***
     *  兩個欄位分組測試,在時間的維度上加上任意其他的欄位聚合,類似group by field1,field2
     * @throws Exception
     */
    public static void testTwoAggString() throws Exception{
        //構造search請求
        SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
        search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 10, 0, 0, 0).getMillis()
        +" TO "+new DateTime(2016, 8, 11, 0, 0, 0).getMillis()+"}"
        ));
        //一級分組欄位
        DateHistogramBuilder dateagg = AggregationBuilders.dateHistogram("dateagg");
        dateagg.field("@timestamp");//聚合時間欄位
        dateagg.interval(DateHistogramInterval.HOUR);//按小時聚合
        dateagg.format("yyyy-MM-dd HH"); //格式化時間
        dateagg.timeZone("Asia/Shanghai");//設定時區,注意如果程式部署在其他國家使用時,使用Joda-Time來動態獲取時區 new DateTime().getZone()

        //二級分組欄位
        TermsBuilder twoAgg = AggregationBuilders.terms("stragg").field("module");
        //組裝聚合欄位
        dateagg.subAggregation(twoAgg);
        //向search請求新增
        search.addAggregation(dateagg);
        //獲取結果
        SearchResponse r = search.get();
        Histogram h = r.getAggregations().get("dateagg");
        //得到一級聚合結果裡面的分桶集合
        List<Histogram.Bucket> buckets = (List<Histogram.Bucket>) h.getBuckets();
        //遍歷分桶集
        for(Histogram.Bucket b:buckets){
            //讀取二級聚合資料集引用
            Aggregations sub = b.getAggregations();
            //獲取二級聚合集合
            StringTerms count = sub.get("stragg");
            //如果設定日期的format的時候,需要使用keyAsString取出,否則獲取的是UTC的標準時間
            System.out.println(b.getKeyAsString() +"  " +b.getDocCount());
            System.out.println("=============================================");
            for(Terms.Bucket bket:(List<Terms.Bucket>)count.getBuckets()){

                System.out.println(bket.getKeyAsString() +"  "+bket.getDocCount());
            }
            System.out.println("************************************************");

        }

    }


    /***
     *  一個欄位聚合,類似資料庫的group by field1
     * @param field 測試聚合的欄位
     * @throws Exception
     */
    public static  void testOneAggString(String field)throws Exception{
        //構造search請求
        SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
        //查詢昨天的資料
        search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 10, 0, 0, 0).getMillis()
                +" TO "+new DateTime(2016, 8, 11, 0, 0, 0).getMillis()+"}"
        ));
        //聚合構造
        TermsBuilder termsBuilder = AggregationBuilders.terms("agg").field(field);
        //新增到search請求
        search.addAggregation(termsBuilder);
        //獲取結果
        SearchResponse searchResponse = search.get();
        //獲取agg標識下面的結果
        Terms agg1 = searchResponse.getAggregations().get("agg");
        //獲取bucket
        List<Terms.Bucket> buckets = (List<Terms.Bucket>) agg1.getBuckets();
        long sum=0;
        for(Terms.Bucket b:buckets){
            Aggregations sub = b.getAggregations();
            System.out.println(b.getKeyAsString()+"  "+b.getDocCount());
            sum+=b.getDocCount();
        }
        System.out.println("總數:"+sum);
    }

}