How to handle file in Whoosh?

Peter Otten __peter__ at web.de
Tue Mar 31 09:52:27 EDT 2015


subhabrata.banerji at gmail.com wrote:

> I am trying to build a search engine, I started with Whoosh. The tutorial 
and web based materials are fine. Web has sizable question and answers. The 
initial experiments seem going fine. But I want to handle files located in 
various parts of my machine. I found "from whoosh.filedb.filestore import 
FileStorage", but I am looking for a simple example to start with. I tried 
something as below, it may be giving some result but is it going okay? If 
any one may please see and correct if required?
> 
> >>> txt_file1=open("/python27/whooshtext1.txt","r").read()
> >>> txt_file2=open("/python27/whooshtext3.txt","r").read()
> >>> writer.add_document(title=u"First document", path= 
unicode("indexdir"+os.sep+"a"),content=u"txt_file1")

I'm not a whoosh user myself, but if I were to guess:

The content argument should contain the contents of the file, not the 
variable name, e. g.:
import io
...
ENCODING = "ascii" # replace with actual encoding of the file
txt_file1 = io.open("/python27/whooshtext1.txt", encoding=ENCODING).read()
...
writer.add_document(
    title=u"First document", 
    path=os.path.join(u"indexdir", u"a"),
    content=txt_file1)


> >>> writer.add_document(title=u"Second document", path= 
unicode("indexdir"+os.sep+"b"),content=u"txt_file2")
> >>> writer.commit()
> >>> with ix.searcher() as searcher:
>     query = QueryParser("content", ix.schema).parse("flood")
>         results = searcher.search(query)
>         print results
>         for result in results:
>         print result
> 
> 
> <Top 0 Results for Term('content', u'flood') runtime=0.000124042337439>

u"flood" is indeed not in the string u"txt_file2" nor the string 
u"txt_file1", so that's the expected number of matches.





More information about the Python-list mailing list