通常当我们搜索时,我们有一个故事列表,我们提供一个搜索字符串,并期望返回给定搜索字符串匹配故事的结果列表。如何将搜索字符串与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