說在前面:elasticsearch
Elasticsearch Java API 有四種實現方式:分別是 TransportClient、RestClient、Jest、Spring Data Elasticsearch。
本文使用第一種方式,也就是 TransportClient 的方式進行實現。想要了解其餘三種的方式能夠看一下這篇文章:https://blog.csdn.net/qq_3331...ui
一、group 以後不能自動分頁,須要手動設置;.net
二、size 須要指定,不然會出錯。code
須要手動截取分頁對應範圍內的數據。blog
好比:這裏排序
倒序,獲取到數據集的第 (currentPage-1) limit 到 currentPage limit 條記錄;ip
升序,獲取到數據集的 第 buckets.size() - (currentPage - 1) limit 到 buckets.size() - currentPage limit 條記錄。get
// 獲取到 response 以後 Aggregations aggregations = response.getAggregations(); Terms carids = aggregations.get("group_car_bayId"); List<? extends Terms.Bucket> buckets = carids.getBuckets(); List<carBean> listCarTgs = new ArrayList<>(); carBean carBean ; // buckets 所有數據,分頁就是取固定位置的 limit 條數據 // 默認按照統計以後的數量倒序, 若是要正序,則第一頁從最後一條開始取 if(buckets.size()>0) { int i=0; if("desc".equalsIgnoreCase(order)) {// 倒序 for(Terms.Bucket bucket : buckets){ if(i++<(currentPage-1) * limit){ continue; } if (i > currentPage * limit) { break; } carBean = new carBean(); carBean.setPassTimes((int)bucket.getDocCount()); carBean.setBayId(bucket.getKeyAsString().split("_")[2]); carBean.setPlateNumber(bucket.getKeyAsString().split("_")[0]); carBean.setPlateType(bucket.getKeyAsString().split("_")[1]); listCarTgs.add(carBean); } }else if("asc".equalsIgnoreCase(order)) {// 升序 for(i = buckets.size() - 1; i >= 0; i--){ if(i < buckets.size() - currentPage * limit){ break; } if(i > buckets.size() - (currentPage - 1) * limit) continue;; carBean = new carBean(); carBean.setPassTimes((int)buckets.get(i).getDocCount()); carBean.setBayId(buckets.get(i).getKeyAsString().split("_")[2]); carBean.setPlateNumber(buckets.get(i).getKeyAsString().split("_")[0]); carBean.setPlateType(buckets.get(i).getKeyAsString().split("_")[1]); listCarTgs.add(carBean); } } }
注意:須要設置 sizeit
TermsAggregationBuilder tb= AggregationBuilders.terms("group_bayId").field("bay_id").size(Integer.MAX_VALUE);
以腳本的形式io
TermsAggregationBuilder tb= AggregationBuilders.terms("group_carId").script(new Script("doc['plateNumber'].value+'_'+doc['plateType'].value")); tb.subAggregation(AggregationBuilders.topHits("max_time").sort("reportTime", SortOrder.DESC).size(1));
再好比:三個 group
BoolQueryBuilder filter = QueryBuilders.boolQuery(); if (carList != null && carList.size() >0) { filter.must(QueryBuilders.termsQuery("car_plate_number", carList.stream().map(SimpleCar:: getPlateNumber).collect(Collectors.toList()))); } if (startTime != null && endTime != null) { filter.filter(QueryBuilders.rangeQuery("timestamp").gt(startTime.getTime()).lt(endTime.getTime())); } else if (startTime != null) { filter.filter(QueryBuilders.rangeQuery("timestamp").gt(startTime.getTime())); } else if (endTime != null) { filter.filter(QueryBuilders.rangeQuery("timestamp").lt(endTime.getTime())); } FieldSortBuilder sort = SortBuilders.fieldSort("transit_times").order("asc".equalsIgnoreCase(order)?SortOrder.ASC:SortOrder.DESC); TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("group_car_bayId") .script(new Script("doc['car_plate_number'].value+'_'+doc['car_plate_type'].value + '_' +doc['bay_id'].value")).size(Integer.MAX_VALUE); SearchResponse response = search(filter, sort, termsAggregationBuilder, elasticsearchProperties.getTgsIndex(), elasticsearchProperties.getTgsType(), (currentPage-1) * limit, 0); Aggregations aggregations = response.getAggregations();
例子2:
TermsAggregationBuilder tb= AggregationBuilders.terms("group_bayId").field("bay_id").size(Integer.MAX_VALUE); tb.order(BucketOrder.count(false));