Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88384

Re: How to handle file in Whoosh?

From Peter Otten <__peter__@web.de>
Subject Re: How to handle file in Whoosh?
Date 2015-03-31 15:52 +0200
Organization None
References <23b11937-781e-473b-ba6e-9b5d68b8ebbd@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.373.1427809979.10327.python-list@python.org> (permalink)

Show all headers | View raw


subhabrata.banerji@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to handle file in Whoosh? subhabrata.banerji@gmail.com - 2015-03-31 06:20 -0700
  Re: How to handle file in Whoosh? Peter Otten <__peter__@web.de> - 2015-03-31 15:52 +0200

csiph-web