Haystack:elasticsearch_backend模块中的KeyError

时间:2013-09-23 14:28:30

标签: django elasticsearch django-haystack

我正在使用Django + Haystack + Elasticsearch。

当我向此视图发送请求时

from haystack.views import FacetedSearchView

from .models import Object


class ObjectView(FacetedSearchView):

    def extra_context(self):
        extra = super(ObjectView, self).extra_context()

        if not self.results:
            extra['objects'] = Object.objects.all()
        else:
            searchqueryset = self.form.search()
            results = [ result.pk for result in searchqueryset ]
            extra['facets'] = self.results.facet_counts()
            extra['objects'] = Object.objects.filter(pk__in=results)
            extra['results'] = self.results
        return extra

引发了这个错误:

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 49, in __call__
    return self.create_response()

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 129, in create_response
    (paginator, page) = self.build_page()

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/views.py", line 106, in build_page
    self.results[start_offset:start_offset + self.results_per_page]

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/query.py", line 266, in __getitem__
    self._fill_cache(start, bound)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/query.py", line 164, in _fill_cache
    results = self.query.get_results(**kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/__init__.py", line 485, in get_results
    self.run(**kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 942, in run
    results = self.backend.search(final_query, **search_kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/__init__.py", line 26, in wrapper
    return func(obj, query_string, *args, **kwargs)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 521, in search
    distance_point=kwargs.get('distance_point'), geo_sort=geo_sort)

  File "/home/deploy/.virtualenvs/deploy/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 571, in _process_results
    raw_suggest = raw_results['suggest']['suggest']

KeyError: 'suggest'

一个奇怪的事实:只有当项目处于生产设置时才会出现问题,即使我没有在settings_production模块中更改涉及Haystack或Elasticsearch的任何事情(URL键除外)。

项目/ settings_production.py

'URL': 'http://0.0.0.0:9200/'

在制作中,我只使用简单的FastCGI。

这就是让我烦恼的事情:有时候我对这个观点没有错误,一切都运转得很好......

请有人知道发生了什么事吗?

非常感谢!

更新

所以,我将我的整个项目安装在另一台计算机上。经过一些测试后,我确认了:

  1. 此问题与我的生产设置无关 如上所述;

  2. 当弹性搜索服务停止时,不会引发错误;

  3. 如果该服务正在运行

    • 当方法Object.objects.all()返回一些QuerySet结果时,我没有错误;
    • 当方法Object.objects.all()返回 QuerySet时,问题仍然存在;
  4. 我想这是Haystack的elasticsearch_backend模块中的某种错误。

    仍然,我不确定。

1 个答案:

答案 0 :(得分:2)

是的,这是大海捞针中的一个错误。我已经输入了pull request,但与此同时,选项可以运行:

  1. 将大海捞针设置中的INCLUDE_SPELLING设置为False
  2. 使用我们的分叉:https://github.com/greenkahuna/django-haystack