尝试使用elasticsearch-dsl Python包访问Elasticsearch云时出现“无法建立新连接”

hzbexzde  于 2023-01-08  发布在  ElasticSearch
关注(0)|答案(1)|浏览(139)

我正在使用elasticsearch-dsl Python包尝试Elasticsearch和索引一些Django数据。
我已经创建了一个相对基本的测试search.py,但是当我尝试索引任何数据时收到一个连接错误。

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Document, Text
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch

from . import models

connections.create_connection(hosts=['ELASTICSEARCH_ENDPOINT_URL'],
                              http_auth='USERNAME:PASSWORD')

class MyIndex(Document):
    title = Text()
    description = Text()

    class Index:
        name = 'my-index'

def bulk_indexing():
    MyIndex.init()
    es = Elasticsearch()
    bulk(client=es, actions=(a.indexing() for a in models.MyModel.objects.all().iterator()))

当我运行bulk_indexing()时,我得到以下错误:
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x1282266d8>: Failed to establish a new connection: [Errno 61] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x1282266d8>: Failed to establish a new connection: [Errno 61] Connection refused)
我怀疑语法错误或在创建连接时丢失了一些凭据,但我找不到任何进一步的信息。
我使用的是使用弹性云部署的Elasticsearch v7.4.0。当我通过浏览器访问URL时,我可以连接。

92dk7w1h

92dk7w1h1#

为什么不简单地使用您的云ID(您可以在ES Cloud控制台中找到)?

from elasticsearch import Elasticsearch
es = Elasticsearch(cloud_id="<some_long_cloud_id>", http_auth=('USERNAME','PASSWORD'))

相关问题