【发布时间】:2021-01-06 18:14:48
【问题描述】:
我想用条件进行基数聚合。
我想统计产品列表中销售总额应大于 100 的所有唯一卖家。
有没有办法使用基数或其他方法来做到这一点?
我已尝试使用 Bucket selector 和 Cardinality 以下查询,但没有成功。
映射
{
"mappings": {
"properties": {
"product_id": {
"type": "long"
},
"seller_id": {
"type": "long"
},
"sell": {
"type": "double"
}
}
}
}
示例文档
[
{
"product_id": 1,
"seller_id": 1,
"sell": 70
},
{
"product_id": 1,
"seller_id": 1,
"sell": 40
},
{
"product_id": 1,
"seller_id": 2,
"sell": 10
},
{
"product_id": 2,
"seller_id": 1,
"sell": 20
},
{
"product_id": 2,
"seller_id": 2,
"sell": 120
},
{
"product_id": 2,
"seller_id": 3,
"sell": 90
},
{
"product_id": 2,
"seller_id": 3,
"sell": 20
}
]
查询
{
"size": 0,
"aggregations": {
"products": {
"terms": {
"field": "product_id"
},
"aggregations": {
"seller_count": {
"cardinality": {
"field": "seller_id"
},
"aggregations": {
"total_sell": {
"sum": {
"field": "sell"
}
},
"sell_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"totalSell": "total_sell"
},
"script": {
"source": "params.totalSell > 100"
}
}
}
}
}
}
}
}
}
预期样本结果
{
"aggregations": {
"products": {
"buckets": [
{
"key": 1, // product_id
"seller_count": {
"value": 1 // 1 Seller is present whose sell sum is greater than 100
}
},
{
"key": 2, // product_id
"seller_count": {
"value": 2 // 2 Seller is present whose sell sum is greater than 100
}
}
]
}
}
}
【问题讨论】:
-
Rohit - 请添加 a) 示例文档 b) 映射 c) 确切需要获取的示例。
-
@SahilGupta 你现在可以帮忙吗?
-
@ECoder 你能帮忙吗?
标签: elasticsearch