通常當我們搜索時,我們有一個故事列表,我們提供一個搜索字符串,並期望返回給定搜索字符串匹配故事的結果列表。如何將搜索字符串與python中的內容匹配
我想要做的是相反的。給出搜索字符串列表和一個故事,找出哪些搜索字符串與該故事匹配。
現在這可以用re來完成,但這裏的情況是我想用solr支持的複雜搜索查詢。 query syntax here的全部細節。注意:我不會使用提升。
基本上我想在下面的示例代碼中獲得一些指針,用於doitmatch函數。
def doesitmatch(contents, searchstring):
"""
returns result of searching contents for searchstring (True or False)
"""
???????
???????
story = "big chunk of story 200 to 1000 words long"
searchstrings = ['sajal' , 'sajal AND "is a jerk"' , 'sajal kayan' , 'sajal AND (kayan OR bangkok OR Thailand OR (webmaster AND python))' , 'bangkok']
matches = [[searchstr] for searchstr in searchstrings if doesitmatch(story, searchstr) ]
編輯:此外也有興趣知道,如果任何模塊存在Lucene的查詢轉換像下面進入正則表達式:
sajal AND (kayan OR bangkok OR Thailand OR (webmaster AND python) OR "is a jerk")
使用我的搜索引擎(solr)的問題是,列表搜索字符串上方的代碼中會有10,000個短語。每個故事的搜索服務器達到10,000次是不理想的。會非常昂貴。 我沒有使用任何複雜的東西,只:和,或,行情和括號 我想寫一個函數將其轉換爲正則表達式,但考慮到我有限的正則表達式技能,我想調查,如果這樣的函數已經存在python ... – sajal 2009-07-09 14:35:26