条件聚合
head插件条件如下
package com.yb.es;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class GroupBy {
public static void main(String[] args) {
new GroupBy().dowork();
}
private void dowork() {
SearchRequest searchRequest = new SearchRequest("land_chufa");
searchRequest.types("land_chufa");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//可以拼接多个条件
QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.prefixQuery("party", "岱山县"))
// .mustNot(QueryBuilders.matchQuery("", ""))
// .must(QueryBuilders.rangeQuery("createTime").gt(""))//gt大于 lt小于 gte大于等于 lte小于等于
;
TermsAggregationBuilder aggregation = AggregationBuilders.terms("field_count").field("lanmuId");
searchSourceBuilder.query(query).size(0);
searchSourceBuilder.aggregation(aggregation).size(0);
searchRequest.source(searchSourceBuilder);
Client client = ESUtils.getClient();
SearchResponse actionGet = client.search(searchRequest).actionGet();
Terms agg = actionGet.getAggregations().get("field_count");
System.out.println("聚合结果长度:"+agg.getBuckets().size());
for (Terms.Bucket entry : agg.getBuckets()) {
String key = (String) entry.getKey();
long count = entry.getDocCount();
System.out.println("key:" + key + ", count:" + count);
}
}
}
运行结果:
聚合结果长度:1
key:lt_5a1682c4acd75d6cf07bb509, count:9