亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將桶排序添加到查詢聚合

如何將桶排序添加到查詢聚合

30秒到達戰場 2021-12-10 16:24:23
我有一個運行良好的 ElasticSearch 查詢 (curl),是我的第一個查詢,首先我按組織(多租戶)過濾,然后按客戶分組,最后總結銷售量,但我只想擁有 3 個最好的客戶。我的問題是..如何使用 AggregationBuilders 構建聚合以獲得“bucket_sort”語句。我使用 Java API 按客戶進行了銷售分組。彈性查詢是: curl -X POST 'http://localhost:9200/sales/sale/_search?pretty'  -H 'Content-Type: application/json' -d '     {         "aggs": {     "filtered": {       "filter": {         "bool": {           "must": [             {               "term": {                 "organization_id": "15"               }             }           ]         }       },       "aggs": {               "by_customer": {                 "terms": {                   "field": "customer_id"                 },                  "aggs": {                      "sum_total" : {                          "sum": {                              "field": "amount"                          }                      },                      "total_total_sort": {                           "bucket_sort": {                               "sort": [                                 {"sum_total": {"order": "desc"}}                               ],                               "size": 3                           }                       }                  }               }           }     } } }'我的Java代碼:@Testpublic void queryBestCustomers() throws UnknownHostException {    Client client = Query.client();    AggregationBuilder sum = AggregationBuilders.sum("sum_total").field("amount");    AggregationBuilder groupBy = AggregationBuilders.terms("by_customer").field("customer_id").subAggregation(sum);    AggregationBuilder aggregation =            AggregationBuilders                    .filters("filtered",                            new FiltersAggregator.KeyedFilter("must", QueryBuilders.termQuery("organization_id", "15"))).subAggregation(groupBy);    SearchRequestBuilder requestBuilder = client.prepareSearch("sales")            .setTypes("sale")            .addAggregation(aggregation);    SearchResponse response = requestBuilder.execute().actionGet();}
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

我希望我答對了你的問題。嘗試將“訂單”添加到您的 groupBy agg:

AggregationBuilder groupBy = AggregationBuilders.terms("by_customer").field("customer_id").subAggregation(sum).order(Terms.Order.aggregation("sum_total", false));

還有一件事,如果您想要前 3 個客戶,那么您也.size(3)應該在 groupBy agg 上設置而不是在排序上。像那樣:
AggregationBuilder groupBy = AggregationBuilders.terms("by_customer").field("customer_id").subAggregation(sum).order(Terms.Order.aggregation("sum_total", false)).size(3);


查看完整回答
反對 回復 2021-12-10
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

正如另一個答案所提到的,“訂單”確實適用于您的用例。


然而,還有其他用例可能需要使用 bucket_sort。例如,如果有人想要分頁瀏覽聚合存儲桶。


由于 bucket_sort 是一個管道聚合,您不能使用 AggregationBuilders 來實例化它。相反,您需要使用 PipelineAggregatorBuilders。


您可以在此處閱讀有關存儲桶排序/管道聚合的更多信息。


以下代碼中的“.from(50)”是如何分頁瀏覽存儲桶的示例。如果適用,這會導致存儲桶中的項目從項目 50 開始。不包括“from”相當于“.from(0)”


BucketSortPipelineAggregationBuilder paging = PipelineAggregatorBuilders.bucketSort(

                    "paging", List.of(new FieldSortBuilder("sum_total").order(SortOrder.DESC))).from(50).size(10);

AggregationBuilders.terms("by_customer").field("customer_id").subAggregation(sum).subAggregation(paging);



查看完整回答
反對 回復 2021-12-10
  • 2 回答
  • 0 關注
  • 244 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號