From 9b3d3fc51b22acf87449ec2a081145175782e1fd Mon Sep 17 00:00:00 2001 From: neargle Date: Fri, 29 Sep 2017 19:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BFmongodb=E5=85=A8=E6=96=87=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=94=AF=E6=8C=81=E4=B8=A4=E4=B8=AA=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=EF=BC=9Aphrase=20&=20caseSensitive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- views/lib/QueryLogic.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/views/lib/QueryLogic.py b/views/lib/QueryLogic.py index cd20e9bd..8a987ff7 100644 --- a/views/lib/QueryLogic.py +++ b/views/lib/QueryLogic.py @@ -2,6 +2,14 @@ import re +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 + + # 搜索逻辑 def querylogic(list): query = {} @@ -18,7 +26,8 @@ def querylogic(list): if match: query['banner'] = {"$regex": _.split(':')[1], '$options': 'i'} else: - query['$text'] = {'$search': _.split(':')[1]} + text_query = mgo_text_split(_.split(':')[1]) + query['$text'] = {'$search': text_query, '$caseSensitive':True} elif _.split(':')[0] == 'ip': ip = _.split(':')[1] query['ip'] = {"$regex": ip}