使用 dmesg 来查看一些硬件或驱动程序的信息或问题。

Elastic-Search Java-Rest-client 实现SQL的groupby后的orderby

Elasticsearch | 作者 fzhsh | 发布于2018年02月07日 | 阅读数:4706

ES在postman提交json数据,可以实现GroupBy后的OrderBy,post的data如下
{
"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);
已邀请:

fzhsh

赞同来自: talon

        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_by").field("age");

List<FieldSortBuilder> sorts = new ArrayList<FieldSortBuilder>();
sorts.add(SortBuilders.fieldSort("sum_by").order(SortOrder.ASC));
builder.subAggregation(PipelineAggregatorBuilders.bucketSort("order_age", sorts));

builder.subAggregation(subBuilder);
sourceBuilder.aggregation(builder);


SearchResponse response = client.search(searchRequest);
 
已经实现!!!

要回复问题请先登录注册