文字搜索与飞快移动

时间:2012-08-08 19:59:31

标签: 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>

谢谢!

1 个答案:

答案 0 :(得分:8)

能够让它发挥作用

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