簡體   English   中英

Python:If-else 語句會出錯

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

從我所看到的,這不是重復的,但如果有很多道歉。

我得到了一些示例 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

我究竟做錯了什么?

您正在執行特定的子字符串搜索,並且您的間距不匹配。 例如,您正在嘗試匹配這兩個子字符串:

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

String in不是近似匹配,也沒有空格的靈活性。 由於您的查詢中有可變間距,您需要在代碼中考慮到這一點——精確匹配不會處理您的實際用例。

正如我之前的評論所建議的,您應該解析輸入並匹配關鍵字段,而不是尋找完美匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM