django - 在AWS的Elastic Search中建立索引时出现连接错误

标签 django python-2.7 amazon-web-services elasticsearch django-haystack

我想使用aws的 flex 搜索作为我的用法。以下代码在我的本地 flex 搜索中完全可以正常工作,但是在尝试连接到aws elasticsearch服务时始终会出错。我正在使用python 2.7,django 1.10和 flex 搜索5.1.1。
以下是错误

ConnectionError(HTTPSConnectionPool(host ='https',port = 443):URL超过最大重试次数://search-test-abc-jlpfzhi64qcrhhqxycov5rzgcq.ap-south-1.es.amazonaws.com/:443/test-index/ tweet / 1(由NewConnectionError('造成:无法建立新的连接:[Errno -2]名称或服务未知”))由以下原因引起:ConnectionError(HTTPSConnectionPool(host ='https',端口= 443):网址超出了最大重试次数://search-test-abc-jlpfzhi64qcrhhqxycov5rzgcq.ap-south-1.es.amazonaws.com /::443 / test-index / tweet / 1(引起通过NewConnectionError(':无法建立新连接:[Errno -2]名称或服务未知”)))

另外,这是我正在使用的代码

host = AWS_ELASTIC_SEARCH_URL
awsauth = AWS4Auth(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ELASTIC_SEARCH_REGION, 'es')

es = Elasticsearch(
    hosts=[{'host': host, 'port': 443}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=elasticsearch.RequestsHttpConnection
)

doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)

最后一行给出错误。另外,我已经完全访问了 flex 搜索网址。

最佳答案

我终于弄明白了。就我而言,我将主机网址写为“https://example.com/”,但应仅将其指定为“example.com”。花了很多时间才想到这一点。以下是我使用python 2.7和django 1.9连接到AWS ElasticSearch(5.1)的工作代码。

def bulk_indexing():

     host = 'example.com'  #not https://example.com"
     awsauth = AWS4Auth('access key', 'secret', region, 'es')
     es = Elasticsearch(
         hosts=[{'host': host, 'port': 443}],
         http_auth=awsauth,
         use_ssl=True,
         verify_certs=True,
         connection_class=RequestsHttpConnection
     )
     payload = {'abc' : 'def'}
     es.index('abc-index', 'doc', payload)

关于django - 在AWS的Elastic Search中建立索引时出现连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207274/

相关文章:

python - 如何使用 GenericRelation 的逆函数

python - 使用 zipfile.ZipFile 即时打开 urllib2.urlopen() 的响应

c# - 亚马逊视频点播 API

python - Django 有足够的脚手架吗? (à la Ruby on Rails)

sql - 如果不为null,则为唯一SQLAlchemy和Django

python - 找不到目录时停止python程序关闭

python - ElasticBeanstalk 突然开始无法部署 Django 应用程序,并出现 "Cannot use ImageField because Pillow is not installed"异常

amazon-web-services - Dynamodb 自动缩放 terraform 导入

django - 你如何使 Django Rest Framework 序列化器双向

python-2.7 - 属性错误 : 'module' object has no attribute 'RLock' `