public SearchResult(SearchResponse resp, Select select) throws SqlParseException { Aggregations aggs = resp.getAggregations(); if (aggs.get("filter") != null) { InternalFilter inf = aggs.get("filter"); aggs = inf.getAggregations(); } if (aggs.get("group by") != null) { InternalTerms terms = aggs.get("group by"); Collection<Bucket> buckets = terms.getBuckets(); this.total = buckets.size(); results = new ArrayList<>(buckets.size()); for (Bucket bucket : buckets) { Map<String, Object> aggsMap = toAggsMap(bucket.getAggregations().getAsMap()); aggsMap.put("docCount", bucket.getDocCount()); results.add(aggsMap); } } else { results = new ArrayList<>(1); this.total = 1; Map<String, Object> map = new HashMap<>(); for (Aggregation aggregation : aggs) { map.put(aggregation.getName(), covenValue(aggregation)); } results.add(map); } }
/* * 统计每种兴趣下职工的平均年龄 * 聚合也允许分级汇总 */ @Test public void subAggregationTest() { Client client = ClientTemplate.getClient(); SearchResponse response = client .prepareSearch("fccs") .setTypes("employee") .addAggregation( AggregationBuilders.terms("by_interests") .field("interests") .subAggregation(AggregationBuilders.avg("avg_age").field("age"))) .get(); Terms terms = response.getAggregations().get("by_interests"); List<Bucket> buckets = terms.getBuckets(); for (Bucket bucket : buckets) { Avg avg = bucket.getAggregations().get("avg_age"); System.out.println( "key:" + bucket.getKey() + " doc_count:" + bucket.getDocCount() + " avg_age:" + avg.getValue()); } client.close(); }
public void testNestedWithSubTermsAgg() throws Exception { SearchResponse response = client() .prepareSearch("idx") .addAggregation( nested("nested", "nested") .subAggregation( terms("values") .field("nested.value") .size(100) .collectMode(aggCollectionMode))) .execute() .actionGet(); assertSearchResponse(response); long docCount = 0; long[] counts = new long[numParents + 6]; for (int i = 0; i < numParents; ++i) { for (int j = 0; j < numChildren[i]; ++j) { final int value = i + 1 + j; ++counts[value]; ++docCount; } } int uniqueValues = 0; for (long count : counts) { if (count > 0) { ++uniqueValues; } } Nested nested = response.getAggregations().get("nested"); assertThat(nested, notNullValue()); assertThat(nested.getName(), equalTo("nested")); assertThat(nested.getDocCount(), equalTo(docCount)); assertThat((long) nested.getProperty("_count"), equalTo(docCount)); assertThat(nested.getAggregations().asList().isEmpty(), is(false)); LongTerms values = nested.getAggregations().get("values"); assertThat(values, notNullValue()); assertThat(values.getName(), equalTo("values")); assertThat(values.getBuckets(), notNullValue()); assertThat(values.getBuckets().size(), equalTo(uniqueValues)); for (int i = 0; i < counts.length; ++i) { final String key = Long.toString(i); if (counts[i] == 0) { assertNull(values.getBucketByKey(key)); } else { Bucket bucket = values.getBucketByKey(key); assertNotNull(bucket); assertEquals(counts[i], bucket.getDocCount()); } } assertThat((LongTerms) nested.getProperty("values"), sameInstance(values)); }