mardi 8 janvier 2019

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. As a result, I wrote an if-else statement so that if the query follows a certain pattern then it has certain actions performed. 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.

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?

Aucun commentaire:

Enregistrer un commentaire