2015-12-03 37 views
1

我使用了pythonhosted.org的示例代碼,但似乎沒有發生任何事情。這是代碼我使用:如何在whoosh上獲得突出顯示的搜索

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

我進入這個代碼到蟒蛇,但它給出了一個錯誤說mysearcher is not defined。所以我真的不確定我是否錯過了一些東西,因爲我只是想讓基本知識讓我開始運行。

回答

2

您缺少定義搜索器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")) 
+0

我試過,以及較早,但是這是它給人的錯誤:'導入錯誤:沒有模塊名爲「whoosh'' –

+0

你應該'輸入whoosh' –

+0

,你應該安裝whoosh肯定'sudo pip安裝whoosh' –

相關問題