I am tyring to test Whoosh for text searching and right now a simple contrived example is not working for me. I assume I am missing something here. In the following code I would expect it 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!
Whoosh is a fast, featureful full-text indexing and searching library implemented in pure Python. Programmers can use it to easily add search functionality to their applications and websites.
Whoosh is a library of classes and functions for indexing text and then searching the index. It allows you to develop custom search engines for your content. For example, if you were creating blogging software, you could use Whoosh to add a search function to allow users to search blog entries.
Was able to 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With