【问题标题】:How to get highlighted searches on whoosh如何在嗖嗖声上获得突出显示的搜索
【发布时间】:2016-03-08 04:31:23
【问题描述】:

我使用了来自 pythonhosted.org 的示例代码,但似乎没有任何反应。这是我使用的代码:

results = mysearcher.search(myquery)
for hit in results:
    print(hit["title"])

我在 python 中输入了这段代码,但它给出了一个错误提示 mysearcher is not defined。所以我真的不确定我是否遗漏了一些东西,因为我只是想了解一些基础知识来让我启动和运行。

【问题讨论】:

    标签: python highlighting whoosh


    【解决方案1】:

    您缺少定义搜索器mysearcher,复制整个代码。这是一个完整的例子:

    >>> import whoosh
    >>> 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()
    >>> 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"}
    

    你可以像这样突出显示:

    for hit in results:
        print(hit["title"])
        # Assume "content" field is stored
        print(hit.highlights("content"))
    

    【讨论】:

    • 我之前也尝试过,但这是它给出的错误:ImportError: No module named 'whoosh'
    • 你应该这样做import whoosh
    • 你应该安装 whoosh sudo pip install whoosh
    • 我在代码的开头添加了import whoosh,但它仍然给出了同样的错误
    • 我到底在哪里编写安装代码,那仍然在 python 或命令行上吗?
    猜你喜欢
    • 2019-03-30
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多