Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text searching with whoosh

Tags:

python

whoosh

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!

like image 933
Abhi Avatar asked Aug 08 '12 19:08

Abhi


People also ask

What is Whoosh search?

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.

What is whoosh library?

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.


1 Answers

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
like image 167
Abhi Avatar answered Sep 30 '22 14:09

Abhi