'宜' bool查询获取不需要的结果

时间:2016-10-14 14:08:55

标签: elasticsearch elasticsearch-query

我想执行一个等同于以下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。我认为在查询之后会对结果应用聚合。我在这里做错了吗?

2 个答案:

答案 0 :(得分: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"] }
            }
        ]
    }
},

答案 1 :(得分:0)

根据最新的Elasticsearch documentation,您应该在聚合内部移动过滤器部分。像这样:

{
   "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"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

希望括号没问题。如果这让你更接近结果,请告诉我:))

相关问题