Reverse Nested Aggregation 反转嵌套聚合
一种特殊的单桶聚合,支持在嵌套文档中 聚合父文档。这种聚合可以有效地 跳出 嵌套块结构,并链接到 其他嵌套结构 或根文档,从而允许 将不属于嵌套对象 的其他聚合 嵌套在 嵌套聚合中。
The reverse_nested
aggregation 必须被定义在 inside a nested
aggregation.
AggregationBuilder aggregation =
AggregationBuilders
.nested("agg", "resellers")
.subAggregation(
AggregationBuilders
.terms("name").field("resellers.name")
.subAggregation(
AggregationBuilders
.reverseNested("reseller_to_product")
)
);
// sr is here your SearchResponse object
Nested agg = sr.getAggregations().get("agg");
Terms name = agg.getAggregations().get("name");
for (Terms.Bucket bucket : name.getBuckets()) {
ReverseNested resellerToProduct = bucket.getAggregations().get("reseller_to_product");
resellerToProduct.getDocCount(); // Doc count
}
Children Aggregation 孩子子聚合
AggregationBuilder aggregation =
AggregationBuilders
.children("agg", "reseller");
Terms Aggregation 术语聚合
AggregationBuilders
.terms("genders")
.field("gender");
ORDER 排序
1.按桶的doc_count升序排序
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.count(true))
2.按桶的英文字母顺序,按升序排列:
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.key(true))
3.按单值 度量子聚合 (由聚合名称标识)对桶进行排序:
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.aggregation("avg_height", false))
.subAggregation(
AggregationBuilders.avg("avg_height").field("height")
)
4.按多个标准排序桶:
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.compound( // in order of priority:
BucketOrder.aggregation("avg_height", false), // sort by sub-aggregation first
BucketOrder.count(true))) // then bucket count as a tie-breaker
.subAggregation(
AggregationBuilders.avg("avg_height").field("height")
)
Significant Terms Aggregation 重要术语聚合
AggregationBuilder aggregation =
AggregationBuilders
.significantTerms("significant_countries")
.field("address.country");
// Let say you search for men only
SearchResponse sr = client.prepareSearch()
.setQuery(QueryBuilders.termQuery("gender", "male"))
.addAggregation(aggregation)
.get();
Range Aggregation 范围聚合
AggregationBuilder aggregation =
AggregationBuilders
.range("agg")
.field("height")
.addUnboundedTo(1.0f) // from -infinity to 1.0 (excluded)
.addRange(1.0f, 1.5f) // from 1.0 to 1.5 (excluded)
.addUnboundedFrom(1.5f); // from 1.5 to +infinity
Date Range Aggregation 日期范围聚合
AggregationBuilder aggregation =
AggregationBuilders
.dateRange("agg")
.field("dateOfBirth")
.format("yyyy")
.addUnboundedTo("1950") // from -infinity to 1950 (excluded)
.addRange("1950", "1960") // from 1950 to 1960 (excluded)
.addUnboundedFrom("1960"); // from 1960 to +infinity
Ip Range Aggregation IP范围聚合
AggregatorBuilder<?> aggregation =
AggregationBuilders
.ipRange("agg")
.field("ip")
.addUnboundedTo("192.168.1.0") // from -infinity to 192.168.1.0 (excluded)
.addRange("192.168.1.0", "192.168.2.0") // from 192.168.1.0 to 192.168.2.0 (excluded)
.addUnboundedFrom("192.168.2.0"); // from 192.168.2.0 to +infinity
Histogram Aggregation 直方图聚合
AggregationBuilder aggregation =
AggregationBuilders
.histogram("agg")
.field("height")
.interval(1);
Date Histogram Aggregation 日期直方图句
AggregationBuilder aggregation =
AggregationBuilders
.dateHistogram("agg")
.field("dateOfBirth")
.dateHistogramInterval(DateHistogramInterval.YEAR);
或者你想要设置一个十天的间隔
AggregationBuilder aggregation =
AggregationBuilders
.dateHistogram("agg")
.field("dateOfBirth")
.dateHistogramInterval(DateHistogramInterval.days(10));
Geo Distance Aggregation 地理距离聚合
AggregationBuilder aggregation =
AggregationBuilders
.geoDistance("agg", new GeoPoint(48.84237171118314,2.33320027692004))
.field("address.location")
.unit(DistanceUnit.KILOMETERS)
.addUnboundedTo(3.0)
.addRange(3.0, 10.0)
.addRange(10.0, 500.0);
Geo Hash Grid Aggregation 地理哈希网格聚合
AggregationBuilder aggregation =
AggregationBuilders
.geohashGrid("agg")
.field("address.location")
.precision(4);
各聚合解释可看官方文档:文档文档