1. Group statistics
/** * Grouping statistics */ public static void termsAgg() { //Group count how many people of each age AggregationBuilder aggregation = AggregationBuilders.terms("terms").field("age"); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute() .actionGet(); Terms terms = response.getAggregations().get("terms"); for (Terms.Bucket bucket : terms.getBuckets()) { System.out.println(bucket.getKey() + ":" + bucket.getDocCount()); } }
Test:
public static void main(String[] args) { termsAgg(); }
Execution result:
20:3 22:1 23:1 28:1
2. Filter statistics
/** * Filter grouping */ public static void filterAgg() { //Filter condition QueryBuilder query = QueryBuilders.termQuery("age", 20); //Group by filter AggregationBuilder aggregation = AggregationBuilders.filter("filter", query); SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute() .actionGet(); System.out.println(response.getAggregations().get("filter").toString()); }
test
public static void main(String[] args) { filterAgg(); }
Execution result:
{"filter":{"doc_count":3}}
3. Statistics of multiple filter conditions
/** * Statistics of multiple filter conditions */ public static void filtersAgg() { //Group statistics of multiple filter conditions (each filter condition is counted separately, with complementary influence) AggregationBuilder aggregation = AggregationBuilders.filters("filters", new FiltersAggregator.KeyedFilter("ageAgg", QueryBuilders.termQuery("age", 20)),//Number of documents aged 20 new FiltersAggregator.KeyedFilter("salaryAgg", QueryBuilders.termQuery("salary", 6000)));//Number of documents for salary 6000 SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute() .actionGet(); Filters filters = response.getAggregations().get("filters"); for (Filters.Bucket bucket : filters.getBuckets()) { System.out.println(bucket.getKeyAsString()+"="+bucket.getDocCount()); } }
test
public static void main(String[] args) { filtersAgg(); }
As a result, the number of documents with "age" of "20" is 3, and the number of documents with "salary" of "6000" is 1
ageAgg=3 salaryAgg=1
4. Interval aggregation
Include front boundary, not back boundary
/** * Scope statistics */ public static void rangeAgg() { AggregationBuilder aggregation = AggregationBuilders.range("rangeAgg") .field("age") .addUnboundedTo(22)//From infinitesimal to 22 (excluding 22) .addRange(22, 28)//22 to 28 inclusive .addUnboundedFrom(28);//From 28 to infinity (including 28) SearchResponse response = getClient().prepareSearch("my_person").addAggregation(aggregation).execute().actionGet(); Range range = response.getAggregations().get("rangeAgg"); for (Range.Bucket bucket : range.getBuckets()) { System.out.println(bucket.getKey()+"="+bucket.getDocCount()); } }
Test, age distribution in document data: 20, 20, 20, 22, 23, 28
public static void main(String[] args) { rangeAgg(); }
results of enforcement
*-22.0=3 22.0-28.0=2 28.0-*=1
5. Date range aggregation
Include front boundary, not back boundary
/** * Date range statistics */ public static void dateRangeAgg() { AggregationBuilder aggregation = AggregationBuilders.dateRange("dateAgg") .field("pubdate") .format("yyyy-MM-dd'T'HH:mm:ss") .addUnboundedTo("2018-07-17T12:33:11")// From infinitesimal to 2018-07-17t12:33:11 (not included) .addUnboundedFrom("2018-07-17T14:14:55");// From 2018-07-17T17:16:30 to infinity (inclusive) SearchResponse response = getClient().prepareSearch("telegraph").addAggregation(aggregation).execute() .actionGet(); Range range = response.getAggregations().get("dateAgg"); for (Range.Bucket bucket : range.getBuckets()) { System.out.println(bucket.getKey()+"="+bucket.getDocCount()); } }
test
public static void main(String[] args) { dateRangeAgg(); }
Result
*-2018-07-17T12:33:11=0 2018-07-17T14:14:55-*=3
6.missing aggregation
/** * Statistics field is empty document quantity */ public static void missingAgg() { //Count the number of documents whose pubdate is empty AggregationBuilder aggregation = AggregationBuilders.missing("missingAgg").field("pubdate"); SearchResponse response = getClient().prepareSearch("telegraph").addAggregation(aggregation).execute() .actionGet(); Missing missing = response.getAggregations().get("missingAgg"); System.out.println(missing.getDocCount()); }