本文介绍了糟糕-访问search_page结果项会引发ReaderClosed异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个简单的分页功能.

Following is a simple pagination function.

from whoosh import index
def _search(q):
    wix = index.open_dir(settings.WHOOSH_INDEX_DIR)
    term = Term("title", q) | Term("content", q)
    page_id = 1

    with wix.searcher() as s:
        return s.search_page(term, page_id, pagelen=settings.ITEMS_PER_PAGE)

In [15]: p = _search("like")

In [16]: p.results[0].reader.is_closed
Out[16]: True

如果我尝试访问Hit的属性,则会出现ReaderClosed异常.

if I try to access an attribute of a Hit, i get ReaderClosed exception.

In [19]: p.results
Out[19]: <Top 10 Results for Or([Term('title', 'like'), Term('content', 'like')]) runtime=0.0214910507202>

[21]: p.results[0]["title"]
---------------------------------------------------------------------------
ReaderClosed                              Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/Django-1.5.3-py2.7.egg/django/core/management/commands/shell.p yc in <module>()
----> 1 p.results[0]["title"]

/usr/local/lib/python2.7/dist-packages/whoosh/searching.pyc in __getitem__(self, fieldname)
1500
1501     def __getitem__(self, fieldname):
-> 1502         if fieldname in self.fields():
1503             return self._fields[fieldname]
1504

/usr/local/lib/python2.7/dist-packages/whoosh/searching.pyc in fields(self)
1388
1389         if self._fields is None:
-> 1390             self._fields = self.searcher.stored_fields(self.docnum)
1391         return self._fields
1392

/usr/local/lib/python2.7/dist-packages/whoosh/reading.pyc in stored_fields(self, docnum)
1197     def stored_fields(self, docnum):
1198         segmentnum, segmentdoc = self._segment_and_docnum(docnum)
-> 1199         return self.readers[segmentnum].stored_fields(segmentdoc)
1200
1201     # Per doc methods

/usr/local/lib/python2.7/dist-packages/whoosh/reading.pyc in stored_fields(self, docnum)
    693     def stored_fields(self, docnum):
    694         if self.is_closed:
--> 695             raise ReaderClosed
    696         assert docnum >= 0
    697         schema = self.schema

ReaderClosed:

如何访问匹配的属性?

推荐答案

浏览whoosh的文档 http://whoosh.readthedocs.org/en/latest/quickstart.html#the-searcher-object 我已经理解了这个问题.留在这里,以防有人遇到同样的问题.

Browsing through whoosh's documents http://whoosh.readthedocs.org/en/latest/quickstart.html#the-searcher-object I've understood the problem. Leaving it here in case anyone gets stuck with same issue.

当"with"作用域结束时,与搜索相关的任何文件描述符都将关闭.因此,似乎应该将结果集复制到另一个数据结构中,例如"with"块中的词典列表,以在该块外使用.

Any file descriptor related to search is closed when "with" scope ended. Therefore it seems resultset should be copied into another data structure such as a list of dictionaries in the "with" block, to be used outside the block.

这篇关于糟糕-访问search_page结果项会引发ReaderClosed异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:15