Whoosh索引查看器

14 投票
2 回答
6429 浏览
提问于 2025-04-15 20:06

我在一个Django应用中使用haystack,后端是whoosh。

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

2 个回答

7
from whoosh.index import open_dir
ix = open_dir('whoosh_index')
ix.searcher().documents()  # will show all documents in the index.

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

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的视图中返回文档(这样可以利用Django的模板系统进行漂亮的格式化):更多信息可以参考Whoosh的文档:http://packages.python.org/Whoosh/index.html

撰写回答