在Whoosh中保存索引

时间:2015-05-31 06:40:01

标签: python python-2.7 whoosh

我正在寻找一个建议或最好是一个例子来存储或保存Whoosh的索引。我在Windows 7 Professional上使用Python 2.7。 如果有人可以帮忙。

1 个答案:

答案 0 :(得分:0)

Whoosh有详细记录,您可以在Official documentation找到大部分内容。以下是快速入门中的Indexer-Searcher示例:

# Indexer
>>> from whoosh.index import create_in
>>> from whoosh.fields import *
>>> schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
>>> ix = create_in("indexdir", schema)
>>> writer = ix.writer()
>>> writer.add_document(title=u"First document", path=u"/a",
...                     content=u"This is the first document we've added!")
>>> writer.add_document(title=u"Second document", path=u"/b",
...                     content=u"The second one is even more interesting!")
>>> writer.commit()

# Searcher 
>>> from whoosh.qparser import QueryParser
>>> with ix.searcher() as searcher:
...     query = QueryParser("content", ix.schema).parse("first")
...     results = searcher.search(query)
...     results[0]
...
{"title": u"First document", "path": u"/a"}

详细解释了这个例子there