3

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.

alstr
  • 1,358
  • 18
  • 34

1 Answers1

4

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'))
Val
  • 207,596
  • 13
  • 358
  • 360
  • The `MyIndex` class inherits from `elasticsearch_dsl.Document` which expects a connection to be created via `create_connection()`, otherwise I get a `KeyError`. That said, I added in the approach you mentioned to `bulk_indexing()` and it worked (thanks). However, I am still confused why the initial approach didn't work. And I now define my endpoint and credentials twice, only one of which works. Ideally I would like to clean that up. :/ – alstr Oct 17 '19 at 11:26
  • 1
    You can pass the same parameters to the `create_connection` call and ditch `es = Elasticsearch()` in your `bulk_indexing` function – Val Oct 17 '19 at 11:35
  • Yep, that worked. I had tried that previously but not without removing the other bit. Thanks. – alstr Oct 17 '19 at 11:42