赞
踩
使用es聚合时,有时还需要获取query(或filter) 的相关文档结果(数据)。
比如统计各个地区编码的营业额,得到了聚合的统计结果,还想知道query结果中对应的地区名称,并根据营业额进行排序,
这时可以使用 top_hits。
top_hits有以下的属性:
- from - 从第几个结果开始获取。
- size - 每个桶返回的query结果的数量。默认情况下,返回前三个匹配的结果。
- sort - 根据字段进行排序。默认情况下,按主查询的分数排序。
格式如下:
- {
- "size" : 0,
- "query" : { },
- "aggregations" : {
- "自己命名的聚合名称" : {
- "terms" : {
- "field" : "聚合字段",
- "size" : 10000,
- "order" : {
- "_term" : "asc"
- }
- },
- "aggregations" : {
- "hits" : {
- "top_hits" : {
- "sort": [
- {
- "排序字段": {
- "order": "desc"
- }
- }
- ],
- "from" : 0,
- "size" : 5
- }
- },
-
- "自己命名的聚合统计的名称" : {
- "sum" : {
- "field" : "聚合统计字段"
- }
- }
- }
- }
- }
- }
示例如下:
- {
- "size" : 0,
- "query" : { },
- "aggregations" : {
- "agg_area" : {
- "terms" : {
- "field" : "area",
- "size" : 10000,
- "order" : {
- "_term" : "asc"
- }
- },
- "aggregations" : {
- "hits" : {
- "top_hits" : {
- "sort": [
- {
- "amount": {
- "order": "desc"
- }
- }
- ],
- "from" : 0,
- "size" : 5
- }
- },
-
- "area_sum" : {
- "sum" : {
- "field" : "amount"
- }
- }
- }
- }
- }
- }
-
java代码格式:
- public static String getTopHitsDSL() {
-
- SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
-
- AggregationBuilder areaCodeAgg = AggregationBuilders.terms(自己命名的聚合名称).field(聚合字段)
- .order(Terms.Order.aggregation("_term", true)).size(10000)
- .subAggregation(AggregationBuilders.topHits("hits").sort(排序字段).size(5))
- .subAggregation(AggregationBuilders.sum(自己命名的聚合统计的名称).field(聚合字段));
-
- return searchSourceBuilder.query().aggregation(areaCodeAgg).size(0).toString();
- }
-
如下所示:
- public static String getTopHitsDSL() {
-
- SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
-
- AggregationBuilder areaCodeAgg = AggregationBuilders.terms("agg_area").field("area")
- .order(Terms.Order.aggregation("_term", true)).size(10000)
- .subAggregation(AggregationBuilders.topHits("hits").sort("amount").size(5))
- .subAggregation(AggregationBuilders.sum("area_sum").field("amount"));
-
- return searchSourceBuilder.query().aggregation(areaCodeAgg).size(0).toString();
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。