pythondjangoelasticsearchelasticsearch-dslelasticsearch-dsl-py

"Failed to establish a new connection" when trying to access Elasticsearch Cloud with elasticsearch-dsl Python package


I'm experimenting with Elasticsearch and indexing some Django data using the elasticsearch-dsl Python package.

I have created a relatively basic test, search.py, but receive a connection error when I try to index any data.

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()))

When I run bulk_indexing() I get the following error:

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)

I suspect the syntax is wrong or I am missing some credentials when creating a connection, but I cannot find any further information.

I am using Elasticsearch v7.4.0 deployed using Elastic Cloud. I can connect when I access the URL via my browser.


Solution

  • Why not simply using your Cloud ID (that you can find in your ES Cloud console) ?

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