Elastic-Search Java-Rest-client 实现SQL的groupby后的orderby
Elasticsearch | 作者 fzhsh | 发布于2018年02月07日 | 阅读数:4710
ES在postman提交json数据,可以实现GroupBy后的OrderBy,post的data如下
有大神知道,在使用 elasticsearch-rest-client 怎么实现么?
目前我只能用java代码实现GroupBy,还实现不了OrderBy
代码环境
代码如下:
{
"size":0,
"aggs":{
"terms_age":{
"terms":{
"field":"age"
},
"aggs":{
"group_age":{
"sum":{
"field":"age"
}
},
"sort_by":{
"bucket_sort":{
"sort":[
{"group_age": {"order": "desc"}}
]
}
}
}
}
}
}
有大神知道,在使用 elasticsearch-rest-client 怎么实现么?
目前我只能用java代码实现GroupBy,还实现不了OrderBy
代码环境
- java 1.8
- elasticsearch-rest-client 6.1.3
代码如下:
SearchRequest searchRequest = new SearchRequest("test");
searchRequest.types("tester");
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
searchRequest.source(sourceBuilder);
sourceBuilder.size(0);
AbstractAggregationBuilder builder = AggregationBuilders.terms("groupy_age").field("age");
AbstractAggregationBuilder subBuilder = AggregationBuilders.sum("sum_age").field("age");
builder.subAggregation(subBuilder);
sourceBuilder.aggregation(builder);
SearchResponse response = client.search(searchRequest);
1 个回复
fzhsh
赞同来自: talon
已经实现!!!