我正在使用 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。
这就是真正困扰我的地方:有时我在这个视图上没有任何错误,而且一切都很好......
拜托,有人知道发生了什么吗?
非常感谢!
更新:
所以,我在另一台计算机上设置了我的整个项目。经过一些测试,我验证了:
这个问题与我上面描述的我的生产设置无关;
停止弹性搜索服务时不会引发错误;
如果服务正在运行:
- 当该方法
Object.objects.all()
返回一些 QuerySet 结果时,我没有收到任何错误; - 当方法
Object.objects.all()
返回一个空的QuerySet 时,问题仍然存在;
- 当该方法
我猜这是 Haystack 的 elasticsearch_backend 模块中的某种错误。
不过,我不确定。