弹性搜索HTTPConnectionPool(host ='127.0.0.1',port = 9200):超出最大重试次数

时间:2014-01-09 08:32:05

标签: python django elasticsearch python-requests pyelasticsearch

我有一个弹性搜索服务器,我从实时网站和Django管理命令查询。管理命令在格林威治标准时间凌晨3点使用celery beat来同步来自外部服务的数据。有时(但不是每次)运行此命令弹性搜索似乎崩溃,我在错误日志中收到以下错误。

    [09/Jan/2014 08:03:46] ERROR [django.request:212] Internal Server Error: /
    Traceback (most recent call last):
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/django/core/handlers/base.py",
line 115, in get_response
        response = callback(request, *callback_args, **callback_kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/django/views/generic/base.py",
line 68, in view
        return self.dispatch(request, *args, **kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/django/views/generic/base.py",
line 86, in dispatch
        return handler(request, *args, **kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/django/views/generic/base.py",
line 153, in get
        context = self.get_context_data(**kwargs)
      File
"/srv/www/site.co.uk/clothes_comparison/clothes_comparison/views.py",
line 56, in get_context_data
        fields=['id', 'name', 'price', 'images', 'advertiser']
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/pyelasticsearch/client.py",
line 96, in decorate
        return func(*args, query_params=query_params, **kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/pyelasticsearch/client.py",
line 512, in multi_get
        'GET', ['_mget'], {'docs': docs}, query_params=query_params)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/pyelasticsearch/client.py",
line 238, in send_request
        **({'data': request_body} if body else {}))
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/requests/sessions.py",
line 347, in get
        return self.request('GET', url, **kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/requests/sessions.py",
line 335, in request
        resp = self.send(prep, **send_kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/requests/sessions.py",
line 438, in send
        r = adapter.send(request, **kwargs)
      File
"/srv/www/site.co.uk/env/local/lib/python2.7/site-packages/requests/adapters.py",
line 327, in send
        raise ConnectionError(e)
    ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=9200): Max
retries exceeded with url: /_mget (Caused by <class 'socket.error'>: [Errno 111]
Connection refused)

我正在使用pyelasticsearch在我的settings.py文件中使用以下代码连接到Elastic Search:

try:
    ES_CON
except NameError:
    ES_CON = None

if not ES_CON:
    ES_CON = ElasticSearch(ELASTICSEARCH_URI)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我建议使用官方的Elasticsearch python客户端:elasticsearch-py具有可靠的连接处理,线程安全等。它也更快(根据作者,他是Elasticsearch团队的一员)。

然后,您可以将es = Elasticsearch()放在tasks.py的顶部,或者例如core.helperses并从那里导入{{1}}。