gpt4 book ai didi

elasticsearch - '应该' bool 查询获取不需要的结果

转载 作者:行者123 更新时间:2023-12-03 01:51:31 28 4
gpt4 key购买 nike

我想执行与以下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。我认为查询后将聚合应用于结果。我在这里做错什么了吗?

最佳答案

我会利用"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"] }
}
]
}
},

关于elasticsearch - '应该' bool 查询获取不需要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40045242/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com