我想数一数所有销售金额应大于100的唯一卖家。
我已经尝试过使用术语聚合的方法,但这将返回一个桶列表。但我想要的只是卖方总数,其销售金额应大于100。
是否有任何方法可以使用、基数、或其他任何方法来完成此操作?
我也尝试过基数,但没有起作用。
查询
{
"size": 0,
"aggregations": {
"seller_count": {
"terms": {
"field": "seller_id"
},
"aggregations": {
"total_sell": {
"sum": {
"field": "sell"
}
},
"sell_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"totalSell": "total_sell"
},
"script": {
"source": "params.totalSell > 100"
}
}
}
}
}
}
}发布于 2021-01-03 14:23:29
seller_id 您可以使用 stats桶聚合来计算 sell 和大于100的stats桶聚合的计数。
使用索引数据、映射、搜索查询和搜索结果添加一个工作示例。
索引映射:
{
"mappings": {
"properties": {
"sell": {
"type": "integer"
}
}
}
}索引数据:
{
"seller_id": 1,
"sell": 50
}
{
"seller_id": 2,
"sell": 50
}
{
"seller_id": 2,
"sell": 60
}
{
"seller_id": 3,
"sell": 60
}
{
"seller_id": 3,
"sell": 100
}搜索查询:
{
"size": 0,
"aggregations": {
"seller_count": {
"terms": {
"field": "seller_id"
},
"aggregations": {
"total_sell": {
"sum": {
"field": "sell"
}
},
"sell_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"totalSell": "total_sell"
},
"script": {
"source": "params.totalSell > 100"
}
}
}
}
},
"bucketcount":{
"stats_bucket":{
"buckets_path":"seller_count._count"
}
}
}
}搜索结果:
"aggregations": {
"seller_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2,
"doc_count": 2,
"total_sell": {
"value": 110.0
}
},
{
"key": 3,
"doc_count": 2,
"total_sell": {
"value": 160.0
}
}
]
},
"bucketcount": {
"count": 2, // note this
"min": 2.0,
"max": 2.0,
"avg": 2.0,
"sum": 4.0
}
}https://stackoverflow.com/questions/65550325
复制相似问题