我需要计算管道聚合返回的结果集中的桶数。问题是我的查询在这里使用脚本选择器:
POST visitor_carts/_search
{
"size": 0,
"aggs": {
"visitors": {
"terms": {"field" : "visitor_id"},
"aggs": {
"one_purchase": {
"bucket_selector": {
"buckets_path": {
"nb_purchases": "_count"
},
"script": "params.nb_purchases == 3"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
返回类似的内容:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"visitors" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "2",
"doc_count" : 3
},
{
"key" : "3",
"doc_count" : 3
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
在该buckets键下,我可以看到符合我的条件的访问者列表(由 标识的每个访问者visitor_id必须在索引中恰好具有三个文档visitor_carts),但这并不是很有帮助,因为它应该处理数十万访问者。我使用 PHP 来处理结果,理论上它可以计算结果集,但对于大量访问者来说,这感觉不是最好的主意。doc_count_error_upper_bound有没有办法只输出和旁边的有效桶的数量sum_other_doc_count?有点奇怪的是,bucket_count聚合统计数据中没有包含它,因为它似乎非常有用。
或者也许可以用不同的方式来完成?此问题是此问题的后续问题:获取进行特定购买次数的用户数
这是我的visitor_carts映射:
{
"mapping": {
"_doc": {
"dynamic": "false",
"properties": {
"created_dt": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"order_id": {
"type": "keyword"
},
"visitor_id": {
"type": "keyword"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用统计桶聚合来获取桶的数量。
以下是您的查询方式。
POST visitor_carts/_search
{
"size": 0,
"aggs": {
"visitors": {
"terms": {
"field" : "visitor_id"
},
"aggs": {
"one_purchase": {
"bucket_selector": {
"buckets_path": {
"nb_purchases": "_count"
},
"script": "params.nb_purchases == 3"
}
}
}
},
"mybucketcount":{
"stats_bucket": {
"buckets_path":"visitors._count"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"visitors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "2",
"doc_count": 3
},
{
"key": "3",
"doc_count": 3
}
]
},
"mybucketcount": {
"count": 2, <---- This is the count you are looking for
"min": 3,
"max": 3,
"avg": 3,
"sum": 6
}
}
}
Run Code Online (Sandbox Code Playgroud)
让我知道这是否有帮助!
| 归档时间: |
|
| 查看次数: |
2412 次 |
| 最近记录: |