![](/img/trans.png)
[英]How to parse the buckets key values of an ElasticSearch Aggregations query to a list of integers in C#
[英]ElasticSearch - Aggregations: filtering and sorting buckets
使用ElasticSearch 5.2
例如,我將使用一個代表世界上所有人的數據庫。
示例請求:獲取30歲以上的每個人的平均工資(按國家/地區分組)。 (回復應包含平均工資最高的10個國家/地區)
我采取的建立聚合查詢的步驟:
filter: age > 30
) country
” salary
”字段上使用“平均”指標 我面臨的問題是我可以在聚合之前應用過濾器,也可以對存儲桶進行排序,但是似乎ES不允許我同時執行這兩項操作?
換句話說:我無法在“ myInnerAggregation
”上應用“ order
”查詢,因為它前面myInnerAggregation
“ myFilterAggregation
”
GET myIndex/_search
{
"size":0,
"aggs": {
"myAggregation": {
"terms": {
"size":10,
"field": "country",
"order": [
{
"myInnerAggregation": "desc"
// I cannot specify "myInnerAggregation" here, only
// "myFilterAggregation" (because it's out of scope I guess)
}
]
},
"aggs": {
"myFilterAggregation": {
"filter": { /* age > 30 (filter syntax is not the problem)*/},
"aggs": {
"myInnerAggergation": {
"avg": {
"field": "salary"
}
}
}
}
}
}
}}
我已經看到ES 6.1+支持“ bucket_sort
”,這可能會解決此問題,但是我不敢相信ES 5.2無法處理像這樣的簡單聚合?
嘗試將過濾器移至請求的query
部分。 您不必將過濾器聲明為聚合,聚合尊重您請求的query
部分中定義的過濾器。 希望以此方式可以解決您的問題。
這應該可以解決問題:
"order": [
{
"MyFilterAggregation>MyInnerAggregation": "desc"
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.