更新索引时Python Whoosh AttributeError

时间:2014-10-12 08:54:12

标签: python whoosh

当我对数据库进行更改时,我正在尝试更新我的嗖嗖搜索索引,但仍然收到我无法弄清楚的错误。

我的一个观点:

from search import update_index

@view_config(route_name='update_effect_brief', permission='edit')
def update_effect_brief(request):
    name = request.matchdict['pagename']
    brief = request.params['effect_brief']
    effect = Effect.get_by_name(name) # This is an "effect" object
    effect.update_brief(brief)
    update_index(effect)
return HTTPFound(location=request.route_url('view_effect', pagename=name))

我的search.py​​文件:

from whoosh.index import create_in, open_dir

def update_index(obj):
    '''Update single ingredient, product or effect.'''
    index = open_dir('searchindex') # searchindex is the name of the directory

    with index.searcher as searcher: # crashes on this line!
        writer = index.writer()
        update_doc(writer, obj)

回溯:

Traceback (most recent call last):
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.9-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 152, in toolbar_tween
    response = _handler(request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.9-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
    result = handler(request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
    response = handler(request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 63, in tm_tween
    response = handler(request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 237, in _secured_view
    return view(context, request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 377, in viewresult_to_response
    result = view(context, request)
  File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 493, in _requestonly_view
    response = view(request)
  File "/home/home/SkinResearch/skinresearch/skinproject/views.py", line 544, in update_effect_brief
    update_index(effect)
  File "/home/home/SkinResearch/skinresearch/skinproject/search.py", line 37, in start_update
    update_index(obj)
  File "/home/home/SkinResearch/skinresearch/skinproject/search.py", line 92, in update_index
    with index.searcher as searcher:
AttributeError: __exit__

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要调用 index.searcher()方法来创建上下文管理器:

with index.searcher() as searcher:

请参阅Whoosh快速入门中的Searcher object sectionThe Searcher object文档。

我并不完全清楚为什么要创建搜索器,然后在块中创建编写器并更新索引。也许你想在这里使用 writer 作为上下文管理器:

with index.writer() as writer:
    update_doc(writer, obj)

离开搜索者进行搜索操作。