/ ** @param startTime 開始的時間
* @param endTime 結束的時間
* @param termAggName term過濾
* @param fieldName 要作count的字段
* @param top 返回的數量
*/
RangeQueryBuilder actionPeriod = QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second");
TermsBuilder termsBuilder = AggregationBuilders.terms(termAggName).field(fieldName).size(top).order(Terms.Order.count(false));
return client.prepareSearch(INDICE).setQuery(actionPeriod).addAggregation(termsBuilder).setSize(0).execute().actionGet();
複製代碼
order(Terms.Order.count(false)):表示降序java
size(top):top表示只要排序的數量bash
prepareSearch(INDICE):INDICE表示索引的名字ui
setSize(0):表示只要聚合結果spa
若是須要去掉某些特殊字段取值 client爲構建的ES客戶端code
BoolQueryBuilder actionPeriodMustNot = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second")).mustNot(QueryBuilders.termQuery(field, value));
複製代碼
若是是單個字段特定的多個值orm
//values是個List
BoolQueryBuilder actioPeriodMust = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second")).must(QueryBuilders.termsQuery(field, values));
複製代碼
使用結果排序
Terms clickCount= sr.getAggregations().get(termAggName);
for (Terms.Bucket term:clickCount.getBuckets()){
int key = term.getKeyAsNumber().intValue(); //要排序字段的值
long docCount = term.getDocCount(); //數量
}
複製代碼
BoolQueryBuilder actioPeriodMust = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second"));
DateHistogramBuilder actionInterval = AggregationBuilders.dateHistogram(dateNickName).field("myTimeField").timeZone("Asia/Shanghai");
if (timeInterval<MINUTE){
actionTimeInterval.interval(DateHistogramInterval.seconds(timeInterval)).format("HH:mm:ss");
}else if (timeInterval<HOUR){
actionTimeInterval.interval(DateHistogramInterval.minutes(timeInterval / MINUTE)).format("dd HH:mm");
}else if (timeInterval < DAY){
actionTimeInterval.interval(DateHistogramInterval.hours(timeInterval / HOUR)).format("HH:mm");
}else if (timeInterval < THIRTY_DAY){
actionTimeInterval.interval(DateHistogramInterval.days(timeInterval / DAY));
}else{
actionTimeInterval.interval(DateHistogramInterval.MONTH);
}
actionInterval.format("yyyy-MM-dd HH:mm:ssZ");
return client.prepareSearch(INDICE).setQuery(actioPeriodMust).addAggregation(actionInterval).setSize(0).execute().actionGet();
複製代碼
es自己默認設置的時間戳是 UTC形式,在國內要設置TimeZone(「Asia/Shanghai」);索引
java的SimpleDateFormate會默認獲取虛擬機所在時區的時間戳,因此存時間的時候,最好存與時區無關的時間,再作本地化顯示get
使用結果虛擬機
Histogram histogram=sr.getAggregations().get(dateNickName);
for(Histogram.Bucket entry:histogram.getBuckets()){
String key = entry.getKeyAsString();//時間間隔
long count = entry.getDocCount();//數量
}
複製代碼
至關於合併上述兩個場景
BoolQueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second"))
.must(QueryBuilders.termsQuery("action", orderValue));
DateHistogramBuilder actionTimeInterval = AggregationBuilders.dateHistogram(dateNickName).field("myTimeField").timeZone("Asia/Shanghai");
actionTimeInterval.subAggregation(AggregationBuilders.terms(termNickName).field("action").size(size));
return client.prepareSearch(INDICE).setQuery(query).addAggregation(actionTimeInterval).setSize(0).execute().actionGet();
複製代碼
使用結果
Histogram hitogram = sr.getAggregations().get(dateAggName);
for (Histogram.Bucket date : hitogram.getBuckets()) {
String intervalName = date.getKeyAsString();
long timeIntervalCount = date.getDocCount();
if (timeIntervalCount != 0) {
Terms terms = date.getAggregations().get(termAggName);
for (Terms.Bucket entry : terms.getBuckets()) {
int key= entry.getKeyAsNumber().intValue();
long childCount = entry.getDocCount();
}
}
}
複製代碼
BoolQueryBuilder actionPeriodMust = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(key, value)).must(QueryBuilders.rangeQuery("myTimeField").gte(startTime).lte(endTime).format("epoch_second"));
return client.prepareSearch(INDICE).setQuery(actionPeriodMust).addSort(SortBuilders.fieldSort("myTimeField").order(SortOrder.ASC)).setFrom(from).setSize(size).execute().actionGet();
複製代碼
使用
Iterator<SearchHit> iterator = sr.getHits().iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
JSONObject jo = JSONObject.parseObject(next.getSourceAsString());
}
複製代碼
BoolQueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("myTimeField").gte(startTimeInSec*1000).lte(endTimeInSec*1000).format("epoch_millis"));
CardinalityBuilder fieldCardinality = AggregationBuilders.cardinality(cardinalityAggName).field(field);//field 要獲取的字段
return client.prepareSearch(INDICE).setQuery(query).addAggregation(fieldCardinality).execute().actionGet();
複製代碼
使用結果
Cardinality cardinality = sr.getAggregations().get(cardinalityAggName);
long value = cardinality.getValue();
複製代碼
好比想要addr是beijing的,同時必須知足條件:name是 paxi,或者,phoneNumber是 1234567890
BoolQueryBuilder searchIdQuery = QueryBuilders.boolQuery();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
while (kvs.hasNext()){
Map.Entry<String, String> fieldValue = kvs.next();
String field=fieldValue.getKey();
String value=fieldValue.getValue();
searchIdQuery.should(QueryBuilders.termQuery(field, value));
}
boolQueryBuilder.must(searchIdQuery);
boolQueryBuilder.must(QueryBuilders.termsQuery(key, values));
return client.prepareSearch(INDICE).setQuery(boolQueryBuilder).execute().actionGet();
複製代碼