代码之家  ›  专栏  ›  技术社区  ›  carousallie

Python:If-else语句变为错误语句

  •  -3
  • carousallie  · 技术社区  · 6 年前

    我得到了一些示例Elasticsearch查询,这些查询需要能够在我的项目中运行,从那里开始,它们需要对它们执行不同的操作。因此,我编写了一个if-else语句,这样,如果查询遵循某个模式,那么它将执行某些操作。但是,6/8查询会转到else语句,即使(据我所知)它们的特征在其他if/elif语句中得到了满足。

    询问

    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
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Prune    6 年前

    您正在执行特定的子字符串搜索,但间距不匹配。例如,您试图匹配以下两个子字符串:

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

    一串 in

    正如前面的评论所建议的,您应该解析输入并匹配关键字段,而不是寻找完美匹配。