0

我想执行与以下 MYSQL 查询等效的查询

SELECT http_user, http_req_method, dst dst_port count(*) as total
FROM my_table
WHERE http_req_method='GET' OR http_req_method="POST"
GROUP BY http_user, http_req_method, dst dst_port

我构建了以下查询:

{
    "query":{       
        "bool":{

            "should":[
                {
                    "term":{"http_req_method":"GET"}
                },
                {
                    "term":{"http_req_method":"POST"}
                }
            ],

        }
    },

    "aggs":{           
        suser":{
            "terms":{
                "field":"http_user"
            },
            "aggs":{
                "dst":{
                    "terms":{
                        "field":"dst"
                    },
                    "aggs":{
                        "dst_port":{
                            "terms":{
                                "field":"dst_port"
                            },
                            "aggs":{
                                "http_req_method":{
                                    "terms":{
                                        "field":"http_req_method"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

(我可能在那里遗漏了一些分支,但在我的代码中是正确的)。问题是结果还包括其他方法,例如 CONNECT,尽管我只要求 GET 或 POST。我认为聚合应用于查询后的结果。我在这里做错了吗?

4

2 回答 2

1

我会利用"minimum_should_match",像这样:

"query":{       
    "bool":{
        "minimum_should_match": 1,
        "should":[
            {
                "term":{"http_req_method":"GET"}
            },
            {
                "term":{"http_req_method":"POST"}
            }
        ],

    }
},

另一种效果更好的方法是在子句中利用terms查询bool/filter

"query":{       
    "bool":{
        "filter":[
            {
                "terms": {"http_req_method": ["GET", "POST"] }
            }
        ]
    }
},
于 2016-10-17T11:11:13.770 回答
0

根据最新的Elasticsearch 文档,您应该将过滤器部分移动到聚合中。像这样的东西:

{
   "aggs":{           
        get_post_requests":{
            "filter" : {
                "bool": [
                    { "term":{"http_req_method":"GET"} },
                    { "term":{"http_req_method":"POST"} },
                ]
            },
            "aggs": {
                "suser"{
                    "terms":{
                        "field":"http_user"
                    }
                },
                "aggs":{
                    "dst":{
                        "terms":{
                            "field":"dst"
                        },
                        "aggs":{
                            "dst_port":{
                                "terms":{
                                    "field":"dst_port"
                                },
                                "aggs":{
                                    "http_req_method":{
                                        "terms":{
                                            "field":"http_req_method"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

希望括号没问题。让我知道这是否让您更接近结果:)

于 2016-10-14T14:44:58.887 回答