我的输出没有提供与查询匹配的文档 [英] My output is not giving the documents matched for the query

查看:42
本文介绍了我的输出没有提供与查询匹配的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 pads 的文件夹,其中有六个记事本文档,每个文档都有一些文本.我正在尝试构建一个 whoosh 代码,该代码将为查询字符串返回适当的文档,但正在作为运行时获取输出,感谢帮助

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)

---输出---垫/5.txt垫/4.txt垫/6.txt垫/3.txt垫/2.txtpads/1.txt

---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)   

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

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

我希望输出从 Pad 文件夹返回 4.txt ,因为它有字符串 "barzini" .你能帮我输出吗

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

推荐答案

实际上,您需要将 print(results) 放在with"代码块中,如下所示.

Actually, you need to place print(results) inside the "with" code block as shown below.

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

这篇关于我的输出没有提供与查询匹配的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆