Django Haystack + Elasticsearch奇怪的间歇性bug

kuhbmx9i  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(102)

我在Elasticsearch中遇到了一个奇怪的问题。
一切都很好(索引,查询......),但有时(我无法确定根本原因),查询索引会出现以下错误:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
 35.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
 128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
 126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
 21.                 return view_func(request, *args, **kwargs)

File "/home/django/prod/mysite/forum/api.py" in get_topics
 66.     topics_list = SearchQuerySet().filter(**filter).models(Topic)

File "/usr/local/lib/python3.5/dist-packages/haystack/query.py" in __init__
 29.         self._determine_backend()

File "/usr/local/lib/python3.5/dist-packages/haystack/query.py" in _determine_backend
 55.         backend_alias = connection_router.for_read(**hints)

File "/usr/local/lib/python3.5/dist-packages/haystack/utils/loading.py" in for_read
 167.         return self._for_action('for_read', False, **hints)[0]

Exception Type: IndexError at /forum/api/get-topics/
Exception Value: list index out of range

字符串
问题似乎与_determine_backend函数有关。
我的后端在Django设置文件中是这样配置的:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch2_backend.Elasticsearch2SearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'myindex',
    },
}


在我的生产服务器(Python 3.5)和开发人员(Python 3.6)上也是如此。
Haystack版本:

django-haystack==2.8.1
  - Django [required: >=1.11, installed: 2.0.6]
    - pytz [required: Any, installed: 2018.4]
elasticsearch==2.4.1
  - urllib3 [required: <2.0,>=1.8, installed: 1.23]


你有过这样的经历吗?
在此先谢谢您!

llew8vvj

llew8vvj1#

你可能在你的应用服务器中使用线程工作者,而django-haystack默认情况下不是线程安全的。请参见警告here
警告
标准的SearchView不是线程安全的。使用search_view_factory函数,该函数返回SearchView的线程安全示例。
一种解决方案是在应用服务器中不使用线程工作者,例如在Gunicorn中,使用--workers X而不是--workers X --threads Y启动应用服务器。但这可能会影响您的应用程序性能。
另一种解决方案是强制SearchQuerySet使用default连接,如这里所建议的,但在代码中简化如下:

topics_list = SearchQuerySet(using='default').filter(**filter).models(Topic)

字符串

相关问题