在aws中的弹性搜索中进行索引时出现连接错误

时间:2018-03-10 09:02:22

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

我想从aws中使用弹性搜索来实现我的用法。以下代码在我的本地弹性搜索中完全正常,但在尝试连接到aws elasticsearch服务时总是会出错。我使用的是python 2.7,django 1.10和弹性搜索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引起('< urllib3.connection.VerifiedHTTPSConnection对象位于0x7f1e2e5e2090>:无法建立新连接:[Errno -2]名称或服务未知',()))由:ConnectionError(HTTPSConnectionPool( host ='https',port = 443):使用url://search-test-abc-jlpfzhi64qcrhhqxycov5rzgcq.ap-south-1.es.amazonaws.com/:443/test-index/tweet/1超出了最大重试次数(由NewConnectionError引起('< urllib3.connection.VerifiedHTTPSConnection对象位于0x7f1e2e5e2090>:无法建立新连接:[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)

在最后一行给出错误。此外,我已完全访问弹性搜索网址。

1 个答案:

答案 0 :(得分:1)

我终于明白了。在我的情况下,我正在写主机网址为" 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)