代码之家  ›  专栏  ›  技术社区  ›  daniels

呼呼索引查看器

  •  14
  • daniels  · 技术社区  · 15 年前

    我正在使用Haystack和whoosh作为django应用程序的后端。

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

    2 回复  |  直到 8 年前
        1
  •  14
  •   Frerich Raabe    8 年前

    在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

        2
  •  6
  •   Collin Anderson    11 年前
    from whoosh.index import open_dir
    ix = open_dir('whoosh_index')
    ix.searcher().documents()  # will show all documents in the index.