我正在尝试进行聚合,需要显示至少有 10 家餐厅和至少有 100 票的餐厅的城市,并找出得分最高的 7 个城市 两人的平均成本
我知道如何在熊猫中做到这一点,将采用以下方式:
import pandas as pd
import numpy as np
df_filtered = df[df.groupby('City')['RestaurantName'].transform('nunique') >= 10]
df_filtered = df_filtered[df_filtered['Votes'] >= 100]
df_city_avg = df_filtered.groupby('City')['AverageCostForTwo'].mean().reset_index()
df_top_cities = df_city_avg.sort_values(by='AverageCostForTwo', ascending=False).head(7)
但我不确定如何在弹性中执行此聚合,任何建议或想法都会非常有帮助,我对此有点迷茫。
以下可能是 Elasticsearch 聚合的解决方案。这里将返回两个平均成本最高的前 7 个城市,其中每个城市至少有 10 家餐厅,每家餐厅至少有 100 票。
curl -XGET localhost:9200/restaurants/_search -d '{
"size": 0,
"aggs": {
"cities": {
"terms": {
"field": "City",
"size": 7,
"include": {
"must": [
{
"filter": {
"terms": {
"RestaurantName": {
"size": 10
}
}
}
},
{
"filter": {
"range": {
"Votes": {
"gte": 100
}
}
}
}
]
}
},
"avg_cost": {
"avg": {
"field": "AverageCostForTwo"
}
}
}
}
}'