1. 程式人生 > >Elasticsearch5.0 Java Api(八) -- 常用DSL查詢

Elasticsearch5.0 Java Api(八) -- 常用DSL查詢

stack pub metrics get ddr 結果 dsl onclick bsp

  測試常用的DSL組合查詢功能

技術分享
  1 package com.juyun.test;
  2 
  3 import java.net.InetAddress;
  4 
  5 import org.elasticsearch.action.search.SearchResponse;
  6 import org.elasticsearch.client.Client;
  7 import org.elasticsearch.common.settings.Settings;
  8 import org.elasticsearch.common.transport.InetSocketTransportAddress;
9 import org.elasticsearch.index.query.QueryBuilder; 10 import org.elasticsearch.index.query.QueryBuilders; 11 import org.elasticsearch.search.SearchHit; 12 import org.elasticsearch.search.SearchHits; 13 import org.elasticsearch.search.aggregations.AggregationBuilder; 14 import org.elasticsearch.search.aggregations.AggregationBuilders;
15 import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; 16 import org.elasticsearch.search.aggregations.metrics.sum.InternalSum; 17 import org.elasticsearch.search.sort.SortBuilder; 18 import org.elasticsearch.search.sort.SortBuilders; 19 import org.elasticsearch.search.sort.SortOrder;
20 import org.elasticsearch.transport.client.PreBuiltTransportClient; 21 import org.joda.time.DateTimeZone; 22 23 public class DslQuery { 24 25 private static Client client; 26 27 // 測試時常用的幾種dsl 28 public static void main(String[] args) { 29 30 try { 31 // 設置集群名稱 32 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 33 // 創建client 34 client = new PreBuiltTransportClient(settings) 35 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.157"), 9300)); 36 37 long startTime=System.currentTimeMillis(); // 獲取開始時間 38 39 commonQuery("newindex","newhttp"); 40 //commonQuery2("newindex","newhttp"); 41 42 long endTime=System.currentTimeMillis(); //獲取結束時間 43 System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); 44 45 46 // 關閉client 47 client.close(); 48 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } 52 } 53 54 /** 55 * 以時間戳降序排列並查詢出所有數據 56 * @param indexName 57 * @param typeName 58 */ 59 public static void commonQuery(String indexName, String typeName){ 60 61 SortBuilder sortBuilder = SortBuilders.fieldSort("@timestamp") 62 .order(SortOrder.DESC).unmappedType("boolean"); // 定義排序方式 63 QueryBuilder queryBuilder =QueryBuilders.boolQuery() 64 .must(QueryBuilders.queryStringQuery("*").analyzeWildcard(true).defaultField("_all")); // 定義查詢方式 65 66 SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(typeName) 67 .setQuery(queryBuilder) 68 .addSort(sortBuilder) 69 .setSize(500) 70 .execute().actionGet(); 71 72 SearchHits hits = searchResponse.getHits(); // 獲取搜索結果 73 for (SearchHit searchHit : hits) { // 遍歷並以字符串形式打印搜索結果 74 System.out.println(searchHit.getSourceAsString()); 75 } 76 System.out.println("查詢到記錄數:" + hits.getTotalHits()); 77 } 78 79 80 /** 81 * 集合查詢統計總值 82 * @param indexName 83 * @param typeName 84 */ 85 public static void commonQuery2(String indexName, String typeName){ 86 87 SortBuilder sortBuilder = SortBuilders.fieldSort("@timestamp") 88 .order(SortOrder.DESC).unmappedType("boolean"); // 定義排序方式 89 QueryBuilder queryBuilder =QueryBuilders.boolQuery() 90 .must(QueryBuilders.queryStringQuery("*").analyzeWildcard(true).defaultField("_all")); // 定義查詢方式 91 92 AggregationBuilder aggBuilder1=AggregationBuilders.dateHistogram("dateagg") // 定義時間間隔直方圖 93 .field("@timestamp") 94 .dateHistogramInterval(DateHistogramInterval.minutes(1)) 95 .timeZone(DateTimeZone.forID("+08:00")); 96 97 AggregationBuilder aggBuilder2=AggregationBuilders.sum("sumOfOut").field("http.bytes_out"); // 聚合統計 98 AggregationBuilder aggBuilder3=AggregationBuilders.sum("sumOfIn").field("http.bytes_in"); 99 100 SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(typeName) 101 .setQuery(queryBuilder) 102 .addSort(sortBuilder) 103 .addAggregation(aggBuilder1) 104 .addAggregation(aggBuilder2) 105 .addAggregation(aggBuilder3) 106 .setSize(500) 107 .execute().actionGet(); 108 109 SearchHits hits = searchResponse.getHits(); // 獲取搜索結果 110 for (SearchHit searchHit : hits) { // 遍歷並以字符串形式打印搜索結果 111 System.out.println(searchHit.getSourceAsString()); 112 } 113 InternalSum sumOfOut = searchResponse.getAggregations().get("sumOfOut"); // 獲取出流量總值 114 InternalSum sumOfIn = searchResponse.getAggregations().get("sumOfIn"); // 獲取進流量總值 115 116 System.out.println("查詢到記錄數:" + hits.getTotalHits()); 117 System.out.println("outbyte的總值是:"+sumOfOut.getValue()); 118 System.out.println("inbyte的總值是:"+sumOfIn.getValue()); 119 } 120 }
DslQuery

Elasticsearch5.0 Java Api(八) -- 常用DSL查詢