从我所看到的来看,这不是重复的,但如果道歉的话很多。
已为我提供了一些示例Elasticsearch查询,这些查询需要能够在我的项目中运行,并且从那里需要对它们执行不同的操作。结果,我编写了一条if-else语句,这样,如果查询遵循某种模式,那么它将执行某些操作。但是,即使(据我所知)它们的特性在其他if / elif语句中得到了满足,但6/8查询仍会转到else语句。
查询
q1 = '{"query": {"bool": {"must": {"bool" : {"should": [{"match": {"Username": "user"}},{"match": {"Action": "action"}}]}}}}}'
q2 = '{"query": {"match" : {"Username" : "user"}}}'
q3 = '{"query": {"bool": {"must": [{"match_phrase": { "Username":"user"}},{"bool": {"should": [{"match": {"Action":"action"}},{"match": {"Action":"action"}}]}},{"range" : {"EventDateTime" :{ "gte": "1546896181000", "lte": "1546982581000" } }}]}}}'
q4 = '{ "query": { "bool": { "must": [ {"match_phrase": { "Username":"user"}}, {"bool": { "should": [ {"match": {"Action":"action"}}, {"match": {"Action":"action"}} ] }}, {"bool":{ "must_not":{"multi_match":{ "type":"phrase", "query":"query", "lenient":true}}}}, {"range" : { "EventDateTime" : { "gte": "1546896181000", "lte": "1546982581000" } }} ] } } }'
q5 = '{ "query": { "bool": { "must": [ {"match_phrase": { "Username":"user"}}, {"bool": { "should": [ {"match": {"Action":"action"}} ] }}, {"range" : { "EventDateTime" : { "gte": "1546896181000", "lte": "1546982581000" } }} ] } } }'
q6 = '{ "query": { "bool": { "must": [ {"match_phrase": { "Username":"user"}}, {"bool": { "should": [ {"match": {"Action":"action"}}, {"match": {"Action":"action"}} ] }}, {"range" : { "EventDateTime" : { "gte": "1546896181000", "lte": "1546982581000" } }} ] } } }'
q7 = '{ "query": { "bool": { "must": [ {"match_phrase": { "SearchRequests":"request"}}, {"range" : { "EventDateTime" : { "gte":1546896181000, "lte":1510703999999 } }} ] } } }'
q8 = '{ "query": { "bool": { "filter":{"multi_match":{"type":"best_fields","query":"test","lenient":true}}, "must": [ {"bool": { "should": [ {"match": {"Action":"action"}}, {"match": {"Action":"action"}} ] }}, {"range" : { "EventDateTime" : { "gte":1546896181000, "lte":1546982581000 } }} ] } } }'
def test_function(query):
username = ''
description = ''
action = ''
if '{"query": {"bool": {"must": {"bool" : {"should": [{' in query:
print('I go to the first loop')
elif '{"query": {"bool": {"must": [{"match_phrase": {' in query:
print('I go to the second loop')
elif '{"query": {"bool": {"filter":\\{"multi_match":{' in query:
print('I go to the third loop')
elif '{"query": {"match": {' in query:
print('I go to the fourth loop')
else:
print('I go to the else statement')
return description, username, action
结果(按顺序)
I go to the first loop
I go to the else statement
I go to the second loop
I go to the else statement
I go to the else statement
I go to the else statement
I go to the else statement
I go to the else statement
我在做什么错?
答案 0 :(得分:1)
您正在执行特定的子字符串搜索,并且间距不匹配。例如,您尝试匹配以下两个子字符串:
{ "must": [ {"match_phrase"
{"must": [{"match_phrase"
字符串in
不是近似匹配,也不具有用于空格的灵活性。由于查询中的间距是可变的,因此您需要在代码中加以说明-完全匹配不会处理您的实际用例。
正如我之前的评论所建议的那样,您应该解析输入并匹配关键字段,而不是寻找完美的匹配。