简体   繁体   中英

The whoosh can not search the key word successfully

1.I'm writing a very simple whoosh project. Firstly, I read a txt file and use read() method to get all the contents in the txt file. Then build an index for this content.

2.Here is the code for implementation:

for the txt file content:

#import functions from whoosh
import whoosh
from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser


schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
ix = create_in(".", schema)
writer = ix.writer()
i = 0
f = open("read.txt", "r")
print(f.read())
writer.add_document(title=u"document "+str(i), path=u".",content=f.read()) #python iterator i starting from 0
writer.commit(optimize=True)
searcher = ix.searcher()


parser = QueryParser("content", ix.schema)
stringquery = parser.parse("Hello")
results = searcher.search(stringquery)
print ("search 1 result:")
print (results)
for r in results:
    print (r)

for the txt file content:

Hello this is the test 
I hope you are doing well
I think you can do it without problem 
This is so cool without funciton

'Hello'suppose to be stored in the index but when I was trying to search hello it returns nothing

search 1 result:
<Top 0 Results for Term('content', 'hello') runtime=7.878600001731684e-05>

Your first call f.read() prints the text from the file, the next call to f.read() has nothing to read and returns nothing. Store the text.

file_content = f.read()
print(file_content)
writer.add_document(title=u"document "+str(i), path=u".", content=file_content)

To further demonstrate,

$ cat test.txt
Hello this is the test
I hope you are doing well
I think you can do it without problem
This is so cool without funciton
$ ipython
Python 3.9.0 (default, Dec  2 2020, 10:34:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: f = open("test.txt", "r")

In [2]: print(f.read())
Hello this is the test
I hope you are doing well
I think you can do it without problem
This is so cool without funciton


In [3]: print(f.read())


In [4]: quit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM