重建索引时,带有 Elasticsearch 的 Django haystack 找不到数据库
Posted
技术标签:
【中文标题】重建索引时,带有 Elasticsearch 的 Django haystack 找不到数据库【英文标题】:Django haystack with Elasticsearch cannot find database when rebuilding index 【发布时间】:2015-10-22 07:47:05 【问题描述】:我将 Haystack 添加到已成功部署到 AWS ElasticBeanstalk 实例的 Django 项目中。当我运行 rebuild_index
时,Haystack 在本地工作,但在 AWS 环境中工作。我收到此错误:
Failed to clear Elasticsearch index: ConnectionError(('Connection aborted.', error(111, 'Connection refused'))) caused by: ProtocolError(('Connection aborted.', error(111, 'Connection refused')))
All documents removed.
ERROR:root:Error updating api using default
Traceback (most recent call last):
File "/opt/python/run/venv/local/lib/python2.7/site-packages/haystack/management/commands/update_index.py", line 188, in handle_label
self.update_backend(label, using)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/haystack/management/commands/update_index.py", line 219, in update_backend
total = qs.count()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 318, in count
return self.query.get_count(using=self.db)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 464, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 445, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 838, in execute_sql
cursor = self.connection.cursor()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
看起来 Haystack 正在尝试连接到我的本地设置中指定的数据库,而不是我专门为我的 AWS ElasticBeanstalk 环境指定的 Postgres RDS,即使“数据库”设置在 AWS 上适用于 ./manage.py loaddata
。
if 'RDS_DB_NAME' in os.environ:
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
else:
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'hhwc',
'HOST': 'localhost',
'PORT': '5432',
这个 'DATABASE' 设置有什么问题吗,或者 Haystack 是否在其他地方寻找它应该连接到以生成索引的数据库的位置?
欢迎任何帮助解决此问题。提前致谢。
【问题讨论】:
【参考方案1】:来自answer to another SO question:
环境变量不是在 virtualenv 中设置的,而是由另一个脚本设置的。首先你必须激活virualenv。
来源 /opt/python/run/venv/bin/activate
然后您需要通过激活“当前”目录中的 env 脚本来加载变量。
来源 /opt/python/current/env
Beanstalk RDS 变量现已设置好,可供您在 SSH 中执行的任何脚本使用。'
【讨论】:
【参考方案2】:错误与数据库无关,而是与您的 haystack 配置有关。检查您在那里使用的 URL。确保在主机名后使用 :80,因为如果您没有明确给出 haystack 默认为端口 9200,AWS 将其设置在端口 80 中。
【讨论】:
【参考方案3】:错误基本上是,您的 django 项目无法连接到亚马逊弹性搜索实例。 这是与 aws elasticsearch 连接的一种方式
首先你需要安装 requests_aws4auth 使用
sudo pip install requests_aws4auth
现在你需要连接亚马逊 elasticsearch 实例
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
import elasticsearch
host = 'YOUR_HOST without putting port number'
awsauth = AWS4Auth('YOUT_ACCESS_KEY', 'YOUR_SECRET_KEY', 'REGION', 'es')
HAYSTACK_CONNECTIONS =
'default':
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': host,
'INDEX_NAME': 'haystack',
'KWARGS':
'port':443,
'http_auth': awsauth,
'use_ssl': True,
'verify_certs': True,
'connection_class': elasticsearch.RequestsHttpConnection,
,
对于那些仍然会遇到一些问题的人,您需要使用以下方法创建索引
curl -XPUT 'Your_AWS_ELASTICSEARCH_URL/haystack/'
【讨论】:
以上是关于重建索引时,带有 Elasticsearch 的 Django haystack 找不到数据库的主要内容,如果未能解决你的问题,请参考以下文章