Search text with whoosh

I am using tyring to check whoosh to find text, and now a simple far-fetched example does not work for me. I guess something is missing here. In the following code, I expect it to give a single search result, but I get 0 hits.

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) 

Output:

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

Thanks!

+4
source share
1 answer

Could make it work

 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 
+6
source

All Articles