1. 程式人生 > >Elasticsearch多欄位分組聚合, 並對分組聚合的count進行篩選

Elasticsearch多欄位分組聚合, 並對分組聚合的count進行篩選

前提
假設我們的文件包括以下幾個欄位 : activityId, clientIp, orderNumber

目標
依據activityId(策略ID) + clientIp(IP地址)分組聚合, 查詢相同策略ID+相同IP下訂單數目超過2的聚合結果

實現

{
    "request_body": {
        // 不返回具體的查詢資料
        "size": 0,
        "aggs": {
            "group_by_activityId": {
                "terms": {
                    // 多欄位聚合
                    "script": "doc['activityId'].values +'#split#'+ doc['clientIp'].values",
                    // 設定聚合返回的最大數目
                    "size": 2147483647
                },
                "aggs": {
                    // 依據orderNumber去重統計數目
                    "orderNumber_count": {
                        "cardinality": {
                            "field": "orderNumber"
                        }
                    },
                    "orderNumber_count_filter": {
                        "bucket_selector": {
                            "buckets_path": {
                                "orderNumberCount": "orderNumber_count"
                            },
                            // 篩選去數目>1
                            "script": "params.orderNumberCount>1"
                        }
                    }
                }
            }
        }
    }
}

結果

{
    "took": 644,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "_clusters": {
        "total": 3,
        "successful": 3,
        "skipped": 0
    },
    "hits": {
        "total": 3742,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_activityId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "[63336]#split#[117.136.89.93]",
                    "doc_count": 3,
                    "orderNumber_count": {
                        "value": 3
                    }
                },
                {
                    "key": "[63336]#split#[1.180.71.158]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[101.67.161.81]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[101.67.161.83]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[112.224.1.196]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[113.56.205.98]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.13.21]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.25.182]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.40.135]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.5.147]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.5.228]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.75.132]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.79.114]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[117.136.87.51]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[124.64.16.10]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[124.88.36.156]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[183.240.195.131]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[221.11.61.224]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[221.192.179.232]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[221.212.180.24]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[221.212.180.28]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[223.104.20.79]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[223.104.250.229]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[223.104.35.228]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[223.104.64.25]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[59.173.193.152]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[59.44.235.74]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[60.15.96.98]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63336]#split#[61.158.147.112]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[63341]#split#[101.67.161.90]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                },
                {
                    "key": "[78657]#split#[223.104.248.6]",
                    "doc_count": 2,
                    "orderNumber_count": {
                        "value": 2
                    }
                }
            ]
        }
    }
}