[英]How to filter by sub-aggregated results in Elasticsearch
我有以下彈性搜索查詢,以獲取按產品 ID 和銷售時間分組的每小時產品銷售數量。
POST /my_sales/_search?size=0
{
"aggs": {
"sales_per_hour": {
"date_histogram": {
"field": "event_time",
"fixed_interval": "1h",
"format": "yyyy-MM-dd:HH:mm"
},
"aggs": {
"sales_per_hour_per_product": {
"terms": {
"field": "name.keyword"
}
}
}
}
}
}
數據示例之一:
{
"@timestamp" : "2020-10-29T18:09:56.921Z",
"name" : "my-beautifull_product",
"event_time" : "2020-10-17T08:01:33.397Z"
}
此查詢返回多個存儲桶(每小時和每個產品一個),但我只想檢索那些 doc_count 高於 10 的存儲桶,例如,這可能嗎? 對於這些結果,我想知道產品的 ID 和 event_time 存儲桶。
謝謝你的幫助。
也許使用Bucket Selector功能將有助於過濾結果。
試試下面的搜索查詢:
{
"aggs": {
"sales_per_hour": {
"date_histogram": {
"field": "event_time",
"fixed_interval": "1h",
"format": "yyyy-MM-dd:HH:mm"
},
"aggs": {
"sales_per_hour_per_product": {
"terms": {
"field": "name.keyword"
},
"aggs": {
"the_filter": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": "params.the_doc_count > 10"
}
}
}
}
}
}
}
}
它將根據"params.the_doc_count > 10"
過濾掉所有計數大於10的文檔
謝謝你的幫助,這與我想要的不遠,但不完全是; 使用桶選擇器,我有這樣的事情:
"aggregations" : {
"sales_per_hour" : {
"buckets" : [
{
"key_as_string" : "2020-08-31:23:00",
"key" : 1598914800000,
"doc_count" : 16,
"sales_per_hour_per_product" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "my_product_1",
"doc_count" : 2
},
{
"key" : "my_product_2",
"doc_count" : 2
},
{
"key" : "myproduct_3",
"doc_count" : 12
}
]
}
}
]
}
有時沒有一個桶大於 10,是否有可能有相同的東西,但 _count 上的過濾器應用於第二級聚合 (sales_per_hour_per_product) 而不是第一級 (sales_per_hour) ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.