[英]Python: Whoosh seems to return incorrect results
這段代碼直接來自Whoosh的quickstart文檔 :
import os.path
from whoosh.index import create_in
from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
from whoosh.index import open_dir
from whoosh.query import *
from whoosh.qparser import QueryParser
#establish schema to be used in the index
schema = Schema(title=TEXT(stored=True), content=TEXT,
path=ID(stored=True), tags=KEYWORD, icon=STORED)
#create index directory
if not os.path.exists("index"):
os.mkdir("index")
#create the index using the schema specified above
ix = create_in("index", schema)
#instantiate the writer object
writer = ix.writer()
#add the docs to the index
writer.add_document(title=u"My document", content=u"This is my document!",
path=u"/a", tags=u"first short", icon=u"/icons/star.png")
writer.add_document(title=u"Second try", content=u"This is the second example.",
path=u"/b", tags=u"second short", icon=u"/icons/sheep.png")
writer.add_document(title=u"Third time's the charm", content=u"Examples are many.",
path=u"/c", tags=u"short", icon=u"/icons/book.png")
#commit those changes
writer.commit()
#identify searcher
with ix.searcher() as searcher:
#specify parser
parser = QueryParser("content", ix.schema)
#specify query -- try also "second"
myquery = parser.parse("is")
#search for results
results = searcher.search(myquery)
#identify the number of matching documents
print len(results)
我只是傳遞了一個值,即動詞“是”給parser.parse()調用。 但是,運行此命令時,得到的結果為零,而不是預期的結果為二。 如果將“ is”替換為“ second”,則會得到一個預期的結果。 但是,為什么使用“是”的搜索不能產生匹配?
正如@Philippe指出的那樣,默認的Whoosh索引器會刪除停用詞,因此會發生上述行為。 如果要保留停用詞,則可以指定在索引索引中給定字段的索引時要使用的分析器,並且可以向分析器傳遞參數以避免剝離停用詞。 例如:
schema = Schema(title=TEXT(stored=True, analyzer=analysis.StandardAnalyzer(stoplist=None)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.