14

我正在使用带有 whoosh 的 haystack 作为 Django 应用程序的后端。

有什么方法可以查看 whoosh 生成的索引的内容(以易于阅读的格式)?我想看看索引了哪些数据以及如何索引,以便更好地了解它是如何工作的。

4

2 回答 2

17

你可以从 python 的交互式控制台很容易地做到这一点:

>>> from whoosh.index import open_dir
>>> ix = open_dir('whoosh_index')
>>> ix.schema
<<< <Schema: ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'id', 'lexer', 'lexer_exact', 'published', 'published_exact']>

您可以直接在索引上执行搜索查询并做各种有趣的事情。要获取每个文件,我可以这样做:

>>> from whoosh.query import Every
>>> results = ix.searcher().search(Every('content'))

如果您想将其全部打印出来(用于查看或其他用途),您可以使用 python 脚本轻松完成。

for result in results:
    print "Rank: %s Id: %s Author: %s" % (result.rank, result['id'], result['author'])
    print "Content:"
    print result['content']

您也可以在 django 视图中直接从 whoosh 返回文档(也许使用 django 的模板系统进行漂亮的格式化):有关更多信息,请参阅 whoosh 文档:http ://packages.python.org/Whoosh/index.html 。

于 2011-03-14T09:46:48.157 回答
7
from whoosh.index import open_dir
ix = open_dir('whoosh_index')
ix.searcher().documents()  # will show all documents in the index.
于 2012-10-23T03:00:10.633 回答