有效的布尔搜索大型索引与whoosh

时间:2013-10-05 14:15:59

标签: parsing search whoosh

我创建了一个带有字段(id,title,url,content)的索引,用于通过抓取来存储网页信息。现在我想用多个单词查询(也是布尔查询)搜索该索引,建议好的高效搜索算法(一些例子)和有效的解析。请帮忙

1 个答案:

答案 0 :(得分:0)

您是否只想搜索标题或内容? 假设您希望允许对标题进行部分搜索,该标题返回URL和/或内容,则架构将为:

 schema = Schema(id=ID(stored=True), title=NGRAM(minsize=2, maxsize=20,stored=True, sortable=ranking_col), url=STORED(), content=STORED())

这适用于标准的Whoosh搜索者,最多可达~1000000个标题。对于更多条目,ngram索引将变得非常大且缓慢。

另外,使用停用词来缩小索引大小:

stopwords = set(['of', 'by', 'the','in','for','a']) #words to be excluded from the index    
def create_whoosh(self):
    writer = ix.writer()
    for t in documents:
        words = [t for t in t.title.split(" ") if t not in stopwords]  #remove stopwords
        writer.add_document(title=" ".join(words), url=t.url, content=t.content)
    writer.commit()    

搜索者:

def lookup(self, terms):
 with ix.searcher() as src:
        query = QueryParser("term", ix.schema).parse(terms)
        results = src.search(query, limit=30)
        return  [[r['url'],r['content']] for r in results]

如果要搜索标题和内容中的完整单词,您可以执行以下操作:

 schema = Schema(id=ID(stored=True), title=TEXT(stored=True), url=STORED(), content=TEXT(stored=True))

这不适用于子字符串搜索,但可以很好地处理一些数百万的文档(取决于内容的大小)

要索引~10万个文档,您需要将内容分别存储在某种数据库中,并仅使用whoosh查找ID。

相关问题