My output is not giving the documents matched for the query

♀尐吖头ヾ 提交于 2019-12-11 19:08:14

问题


I have a folder called pads in which there are six notepad documents with some text in each of them. Am trying to build a whoosh code that will return the appropriate document for the query string but am getting output as runtime, help appreciated

import os
from whoosh.index import create_in
from whoosh.fields import Schema, TEXT, ID
import sys
from whoosh.qparser import QueryParser
from whoosh import scoring
from whoosh.index import open_dir

def createSearchableData(root):   

'''
Schema definition: title(name of file), path(as ID), content(indexed
but not stored),textdata (stored text content)
'''
    schema = Schema(title=TEXT(stored=True),path=ID(stored=True),\
          content=TEXT,textdata=TEXT(stored=True))
    if not os.path.exists("indexdir"):
        os.mkdir("indexdir")

# Creating a index writer to add document as per schema
    ix = create_in("indexdir",schema)
    writer = ix.writer()

    filepaths = [os.path.join(root,i) for i in os.listdir(root)]
    for path in filepaths:
        fp = open(path,'r')
        print(path)
        text = fp.read()
        writer.add_document(title=path.split("\\")[0], path=path,\
          content=text,textdata=text)
        fp.close()
    writer.commit()

root = "pads"
createSearchableData(root)

---OUTPUT--- pads/5.txt pads/4.txt pads/6.txt pads/3.txt pads/2.txt pads/1.txt

ix = open_dir("indexdir")
query_str = 'barzini'
# Top 'n' documents as result
topN = 3

qp = QueryParser("content", ix.schema)
q = qp.parse(query_str)

with ix.searcher() as searcher:
    results = searcher.search(q,limit=topN)
print(results)   

---OUTPUT--- Top 1 Results for Term('content', 'barzini') runtime=0.00048629400043864734>

I wanted the output to return 4.txt from Pad folder as it has the string "barzini" . Could you please help me with the output

来源:https://stackoverflow.com/questions/57839279/my-output-is-not-giving-the-documents-matched-for-the-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!