Elasticsearch 聚合分页排序

Elasticsearch 聚合分页排序

{
    "size":0,
    "aggs":{
        "group_name":{
            "terms":{
                "field":"field.keyword",
                "size":99999
            },
            "aggs":{
                "sum_field":{
                    "sum":{
                        "field":"money"
                    }
                },
                "bucket_field":{
                    "bucket_sort":{
                        "sort":[
                            {
                                "sum_field":{
                                    "order":"desc"
                                }
                            }
                        ],
                        "from":6000,
                        "size":10
                    }
                }
            }
        }
    }
}

Java代码

        TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("group_name").field("field.keyword").size(99999);
        termsBuilder.subAggregation(AggregationBuilders.sum("sum_field").field("money"));
        List<FieldSortBuilder> fieldSorts=new ArrayList<>();
        fieldSorts.add(new FieldSortBuilder("sum_field").order(SortOrder.DESC));
        termsBuilder.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field", fieldSorts).from(6000).size(10));

说明:因为bucket_sort是对聚合之后的桶进行分页排序,所以terms里面的size设置要大于bucket_sort里面分页大小,不然会取不到数据

你可能感兴趣的:(Java)