首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的输出没有给出与查询匹配的文档

我的输出没有给出与查询匹配的文档
EN

Stack Overflow用户
提问于 2019-09-08 12:51:51
回答 1查看 68关注 0票数 0

我有一个名为pads的文件夹,其中有六个记事本文档,每个文档中都有一些文本。我试图建立一个呼呼代码,将返回适当的文档查询字符串,但我得到的输出作为运行时,帮助感谢

代码语言:javascript
复制
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)

-输出-焊盘/5.txt焊盘/4.txt焊盘/6.txt焊盘/3.txt焊盘/2.txt焊盘/1.txt

代码语言:javascript
复制
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=0.00048629400043864734>的前1个结果

我希望输出从Pad文件夹返回4.txt,因为它包含字符串"barzini“。你能帮我处理一下输出吗?

EN

回答 1

Stack Overflow用户

发布于 2020-04-20 12:19:16

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

代码语言:javascript
复制
with ix.searcher() as searcher:
    results = searcher.search(q, limit=None)
    print(results)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57839279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档