我的输出未提供与查询匹配的文档

问题描述 投票:0回答:1

我有一个名为pads的文件夹,其中有六个记事本文档,每个文档中都有一些文本。我正在尝试构建一个飞快移动的代码,该代码将为查询字符串返回适当的文档,但是正在输出为运行时,请多加帮助

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)

-输出-pads / 5.txtpads / 4.txtpads / 6.txtpads / 3.txtpads / 2.txtpads / 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)   

-输出-Term('content','barzini')runtime的前1个结果= 0.00048629400043864734>

我希望输出从Pad文件夹返回4.txt,因为它具有字符串“ barzini”。您能否帮我提供输出信息>

我有一个名为pads的文件夹,其中有六个记事本文档,每个文档中都有一些文本。我正在尝试构建一个代码,该代码将为查询字符串返回适当的文档,但...

python output whoosh
1个回答
0
投票

实际上,您需要将打印(结果)放置在“ with”代码块中,如下所示。

© www.soinside.com 2019 - 2024. All rights reserved.