Skip to content

Commit

Permalink
使mongodb全文搜索支持两个属性:phrase & caseSensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
neargle committed Sep 29, 2017
1 parent 578ad9f commit 9b3d3fc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion views/lib/QueryLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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}
Expand Down

0 comments on commit 9b3d3fc

Please sign in to comment.