2012-09-09 78 views
1

使用Whoosh我無法搜索帶有多字查詢的文檔。我使用了And運算符,如http://packages.python.org/Whoosh/quickstart.html#the-searcher-object 所示,但它顯示錯誤。單詞搜索起作用。使用Whoosh我無法使用多字查詢搜索文檔。

from whoosh.index import create_in 
from whoosh.fields import * 
from whoosh.qparser import QueryParser 
from whoosh.fields import Schema, TEXT, KEYWORD, ID, STORED 
from whoosh.analysis import StemmingAnalyzer 
from whoosh.query import * 
import os.path 

##Schema 

schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) 

##Indexing 

if not os.path.exists("index"): 
    os.mkdir("index") 

ix = create_in("index", schema) 

writer = ix.writer() 
writer.add_document(title=u"First document", path=u"/a", content=u"Michael Jackson") 
writer.add_document(title=u"Second document", path=u"/b", content=u"Roger Federer") 
writer.add_document(title=u"Third document", path=u"/c", content=u"Michael Jordan") 
writer.add_document(title=u"Fifth document", path=u"/d", content=u"Michael Phelps") 
writer.add_document(title=u"Sixth document", path=u"/e", content=u"Michael Schumacher") 
writer.add_document(title=u"Seventh document", path=u"/f", content=u"Michael Dell") 
writer.add_document(title=u"Eigth document", path=u"/g", content=u"Michael Akerfeldt") 
writer.commit() 


##Searching 

searcher = ix.searcher() 

#Single Word Search: 
#myquery = u'Michael' 

#Multiword Search: 
myquery = And([Term("content", u'Roger'), Term("content", u'Michael')]) 

#query = QueryParser("content", schema=ix.schema).parse(myquery) 

parser = QueryParser("content", ix.schema) 
query = parser.parse(myquery) 

#with ix.searcher() as searcher: 

results = searcher.search(query, limit=None) 

print "Length of Results :", len(results) 
print "\nResults : " 
for i in range (0,len(results)): 
    print results[i] 
+0

它顯示了什麼錯誤? – allen213

回答

0

我覺得你對查詢解析器的功能感到困惑。查詢分析器將創建是通過調用明確構造的And查詢:

query = parser.parse("Roger and Michael") 

或者,如果你真的想自己創建的查詢結構,則只是通過myquery直接searcher.search而不是query

更多的信息在這裏:http://packages.python.org/Whoosh/parsing.html