简体   繁体   English

Python:If-else 语句会出错

[英]Python: If-else statement going to wrong statement

from what I've seen this isn't a duplicate but if it is many apologies.从我所看到的,这不是重复的,但如果有很多道歉。

I've been given a few sample Elasticsearch queries that need to be able to run through my project, and from there they need to have different actions performed on them.我得到了一些示例 Elasticsearch 查询,这些查询需要能够在我的项目中运行,从那里他们需要对它们执行不同的操作。 As a result, I wrote an if-else statement so that if the query follows a certain pattern then it has certain actions performed.结果,我写了一个 if-else 语句,这样如果查询遵循某种模式,那么它就会执行某些操作。 However, 6/8 queries go to the else statement even though (as far as I can tell) their characteristics are fulfilled in the other if/elif statements.然而,6/8 查询转到 else 语句,即使(据我所知)它们的特征在其他 if/elif 语句中得到满足。

Queries查询

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

Results (in order)结果(按顺序)

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

What am I doing wrong?我究竟做错了什么?

You're doing a specific substring search, and your spacing doesn't match.您正在执行特定的子字符串搜索,并且您的间距不匹配。 For instance, you're trying to match these two sub-strings:例如,您正在尝试匹配这两个子字符串:

{ "must": [ {"match_phrase"
{"must": [{"match_phrase"

String in is not an approximate match, nor does it have flexibility for white-space. String in不是近似匹配,也没有空格的灵活性。 Since you have variable spacing in your queries, you'll need to account for that in your code -- exact match won't handle your actual use cases.由于您的查询中有可变间距,您需要在代码中考虑到这一点——精确匹配不会处理您的实际用例。

As comments before me have suggested, you should parse the input and match the critical fields, rather than looking for a perfect match.正如我之前的评论所建议的,您应该解析输入并匹配关键字段,而不是寻找完美匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM