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

Question:

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.

Asked By: alstr

||

Answers:

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'))
Answered By: Val

I too have an issue related to it. Someone please help.

This is my code-

from elasticsearch import Elasticsearch

# Password for the 'elastic' user generated by Elasticsearch
ELASTIC_PASSWORD = "MyPassword"

# Found in the 'Manage Deployment' page
CLOUD_ID = "My_deployment:blahblahblah"

# Create the client instance
client = Elasticsearch(cloud_id=CLOUD_ID,
basic_auth=("enterprise_search", ELASTIC_PASSWORD) #not sure if my 
#username is 'elastic' or 'enterprise_search'
)

# Successful response!
client.info()
# {'name': 'instance-0000000000', 'cluster_name': ...}

And I get an error i.e.

NewConnectionError(<urllib3.connection.HTTPConnection object at 0x000001B33FE91AB0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it)

Answered By: DEATH ADDER