Search code examples
pythondjangoelasticsearchdjango-rest-frameworkdocument

NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused)


I am trying to build a simple django elastice search which can search cars. But when I am trying to rebuid the indexes, it gives me the error above. I am following this doc -> quickstart elasticsearch...full traceback is given below->

elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused)

my models.py is simple Car model having name, color, description, type filds on which I add in document

class Car(models.Model):
    name = models.CharField(max_length=50)
    color = models.CharField(max_length=50)
    description = models.TextField()
    type = models.IntegerField(choices=[
        (1, "Sedan"),
        (2, "Truck"),
        (4, "SUV"),
    ])

my documents.py file->

from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry
from .models import Car


@registry.register_document
class CarDocument(Document):
    class Index:
        name = 'cars'
        settings = {'number_of_shards': 1,
                    'number_of_replicas': 0}

    class Django:
        model = Car

        fields = [
            'name',
            'color',
            'description',
            'type',
        ]

but when i am trying to rebuid the index, it gives the new connection error. The command I am using to rebuild the index is given below ->

python manage.py search_index --rebuild

Solution

  • First of all make sure you have installed and configured ElasticSearch service properly (you can use this document).This error is saying that your code could not connect to ElasticSearch service with your current configs. Check if your elastic service is up and running. In Linux based OS you can check it by $ sudo systemctl status elasticsearch.service and in the result page you should see the line Active: active (running). After this step check the url http://localhost:9200/ (if you did not change the default settings) and if everything is OK you should see the following results:

    {
      "name" : "some_name-System-Product-Name",
      "cluster_name" : "elasticsearch",
      "cluster_uuid" : "erP_jdhLRvuwqLkx1_tehw",
      "version" : {
        "number" : "7.9.0",
        "build_flavor" : "default",
        "build_type" : "deb",
        "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
        "build_date" : "2020-08-11T21:36:48.204330Z",
        "build_snapshot" : false,
        "lucene_version" : "8.6.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

    You can also start this service by using $ sudo systemctl start elasticsearch.service. Another possibility is that maybe you configured elastic variables in a wrong way. For example instead of default port (i.e. 9200) using wrong port will also raise the same error. In following example you can see an example of Elastic configs which will goes in your project's settings.py:

    # ElasticSearch configs
    ELK_BASE_URL = 'elasticsearch://{username}:{password}@{host_ip}:{host_port}'
    ELASTIC_SEARCH_URL = ELK_BASE_URL.format(
        username='ELASTICSEARCH_USER',
        password='ELASTICSEARCH_PASS',
        host_ip='ELASTICSEARCH_HOST',
        host_port='ELASTICSEARCH_PORT'
    )
    ELASTICSEARCH_DSL = {
        'default': {
            'hosts': [ELASTIC_SEARCH_URL]
        },
    }