巡風源碼閱讀與分析--querylogic函數

 

文件位置:views/lib/QueryLogic.pypython

 

Querylogic()web

 

# 搜索邏輯
def querylogic(list):
    query = {}
    if len(list) > 1 or len(list[0].split(':')) > 1:
        for _ in list:
            if _.find(':') > -1:
                q_key, q_value = _.split(':', 1)
                if q_key == 'port':
                    query['port'] = int(q_value)
                elif q_key == 'banner':
                    zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
                    contents = q_value
                    match = zhPattern.search(contents)
                    # 若是沒有中文用全文索引
                    if match:
                        query['banner'] = {"$regex": q_value, '$options': 'i'}
                    else:
                        text_query = mgo_text_split(q_value)
                        query['$text'] = {'$search': text_query, '$caseSensitive':True}
                elif q_key == 'ip':
                    query['ip'] = {"$regex": q_value}
                elif q_key == 'server':
                    query['server'] = q_value.lower()
                elif q_key == 'title':
                    query['webinfo.title'] = {"$regex": q_value, '$options': 'i'}
                elif q_key == 'tag':
                    query['webinfo.tag'] = q_value.lower()
                elif q_key == 'hostname':
                    query['hostname'] = {"$regex": q_value, '$options': 'i'}
                elif q_key == 'all':
                    filter_lst = []
                    for i in ('ip', 'banner', 'port', 'time', 'webinfo.tag', 'webinfo.title', 'server', 'hostname'):
                        filter_lst.append({i: {"$regex": q_value, '$options': 'i'}})
                    query['$or'] = filter_lst
                else:
                    query[q_key] = q_value
    else:
        filter_lst = []
        for i in ('ip', 'banner', 'port', 'time', 'webinfo.tag', 'webinfo.title', 'server', 'hostname'):
            filter_lst.append({i: {"$regex": list[0], '$options': 'i'}})
        query['$or'] = filter_lst
    return query

  

 

傳過來的是一個列表mongodb

咱們先看列表長度爲1,並且沒有「:」的狀況:數組

else:
    filter_lst = []
    for i in ('ip', 'banner', 'port', 'time', 'webinfo.tag', 'webinfo.title', 'server', 'hostname'):
        filter_lst.append({i: {"$regex": list[0], '$options': 'i'}})
    query['$or'] = filter_lst

  

返回一個對象,相似這樣的: {'$or': [{'ip': {'$options': 'i', '$regex': '127.0.0.1'}},....]}將數組中這些值構形成上面的形式,而後用’$or’ 不難猜想是要將條件代入查詢,用or來鏈接。app

再看列表長度不爲1,或者有「:」的狀況:函數

for _ in list:spa

if _.find(':') > -1:.net

遍歷列表,找到有「:」的狀況。  而後分割  ,若是是查port就轉爲整型。 查banner的話,先判斷是否存在中文,若是有中文就構形成orm

{'banner': {'$options': 'i', '$regex': u'\u963f\u8428\u5fb7aaa'}} 這樣的,代入查詢。server

沒有中文的話:mgo_text_split()這個函數:

def mgo_text_split(query_text):
    ''' split text to support mongodb $text match on a phrase '''
    sep = r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,./<>?]'
    word_lst = re.split(sep, query_text)
    text_query = ' '.join('\"{}\"'.format(w) for w in word_lst)
    return text_query

 

先按這些特殊符號分割,而後鏈接成一個字符串 」xxx」 「xxx」 「xxx」。

 text_query = ' '.join('\"{}\"'.format(w) for w in word_lst)

這句能夠當作:

L =[]
for w in word_lst:
    L.append('\"{}\"'.format(w))
text_query = ' '.join(L)

  

而後構形成這樣:{'$text': {'$caseSensitive': True, '$search': '"xxx" "xxx" "xxx"'}}代入查詢。

下面代碼均同樣。

最後else 就是q_key都沒有定義的話,就是{'key':’value’}

 

 

$regex 是利用正則查詢 $options 等於 i 則是不區分大小寫。

$search 文本索引查詢 。$caseSensitive  是否區分大小寫。

 

這些MongoDB的條件,在這篇博客均有。

https://blog.csdn.net/qq_16313365/article/details/58599253

 

總得來講這個函數就是將搜索的值轉成mongoDB能查詢的語句。

相關文章
相關標籤/搜索