编程语言
首页 > 编程语言> > python – 用whoosh搜索文本

python – 用whoosh搜索文本

作者:互联网

我正在测试飞快移动进行文本搜索,现在一个简单的人为例子对我不起作用.我想我在这里错过了一些东西.在下面的代码中,我希望它给出一个搜索结果,但我得到0次点击.

import sys
import os

from whoosh.fields import Schema, TEXT, STORED
from whoosh.index import create_in, open_dir
from whoosh.query import *

#creating the schema
schema = Schema(tax_id=STORED,
                name=TEXT(stored=True))

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

ix = create_in("index",schema)
ix = open_dir("index")
writer = ix.writer()
writer.add_document(tax_id="17",name=u"Methyliphilus methylitrophus")
writer.add_document(tax_id="17",name=u"Methylophilus methylotrophus Jenkins et al. 1987")
writer.add_document(tax_id="45",name=u"Chondromyces lichenicolus") 
writer.commit()

myquery = And([Term("name",u"Chondromyces")])
with ix.searcher() as searcher:
    print searcher.search(myquery)

输出:

<Top 0 Results for And([Term('name', u'Chondromyces lichenicolus')]) runtime=9.41753387451e-05>

谢谢!

解决方法:

能够使它工作

from whoosh.qparser import QueryParser
ix=open_dir("index")
with ix.searcher() as searcher:
    query = QueryParser("name", ix.schema).parse(u'Chondromyces')
    results = searcher.search(query)
    for result in results:
        print result

标签:python,whoosh
来源: https://codeday.me/bug/20190613/1232267.html