Waheeb Al-Abyadh
Waheeb Al-Abyadh

Reputation: 379

Failed to establish a new connection: [Errno 111] Connection refused(elasticsearch)

I have installed elasticsearch using this command : pip install elasticsearch After installation I executed the following commands:

>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
# by default we connect to localhost:9200
>>> es = Elasticsearch()
# create an index in elasticsearch, ignore status code 400 (index already exists)
# but when I run this instruction:
>>> es.indices.create(index='my-index', ignore=400) // HERE IS THE PROBLEM

I get this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "elasticsearch/client/indices.py", line 110, in create
params=params, body=body)
File "elasticsearch/transport.py", line 327, in perform_request
status, headers, data = connection.perform_request(\
    method, url, params, body, ignore=ignore, timeout=timeout)
File "elasticsearch/connection/http_urllib3.py", line 105, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError:
    ConnectionError(<urllib3.connection.HTTPConnection object at 0xM3M>:
        Failed to establish a new connection: [Errno 111] Connection refused) caused by:
            NewConnectionError(<urllib3.connection.HTTPConnection object at 0xM3M>:
                Failed to establish a new connection: [Errno 111] Connection refused)

Upvotes: 22

Views: 76706

Answers (2)

Gitau Harrison
Gitau Harrison

Reputation: 3517

Following the documentation is straight-forward and you should be able to get elasticsearch up and running. Just to break it down, using pip3 install elasticsearch does not create a connection to elasticsearch. Follow the steps below to set up, install and start connection:

Install Necessary Packages

Since Elasticsearch runs on top of Java, you need to install the Java Development Kit (JDK). You can check if Java is installed by running this command in your terminal:

$ java -version

If you do not have java, you can install the default JDK by:

$  sudo apt install openjdk-8-jre-headless 

Run java -version to check that java is installed

To allow access to your repositories via HTTPS, you need to install an APT transport package:

$ sudo apt install apt-transport-https

Download and install Elasticsearch (on Ubuntu)

First, update the GPG Key for the Elasticsearch repository using the wget command to pull the public key (from the documentation now):

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

Add the repository to your system:

$ echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

Now, install elasticsearch by first updating the package index then run the installation:

$ sudo apt update
$ sudo apt install elasticsearch

Start Elasticsearch

Elasticsearch does not run until you start it. Also, when you reboot your machine, you need to rerun the elasticsearch service since it does not start automatically.

To reload the systemd configuration, run:

$ sudo systemctl daemon-reload

Enable the elasticsearch service:

$ sudo systemctl enable elasticsearch.service

Now, you can start elasticsearch:

$ sudo systemctl start elasticsearch.service

At this point, elasticsearch will start everytime you reboot your system.

To test your set up, try running http://localhost:9200/ on your browser's url bar. You should see some JSON data dumped on your screen. Better still, on your terminal, try:

$ curl localhost:9200

This completes the setup, installation and how to start the elasticsearch service. Now you can try running your commands on the terminal and everything should work fine.

>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>> es.indices.create(index='my-index', ignore=400)
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'my_index'}

Upvotes: 3

Avihoo Mamka
Avihoo Mamka

Reputation: 4786

What you have installed is a Python client which is used for communication between your Python script and existing Elasticsearch cluster.

As mentioned from your comment, at the top of the page you started reading, it says:

Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in Python; because of this it tries to be opinion-free and very extendable.

You can configure the client the host(s) and port which the cluster is running on, and connect to it and execute commands on that cluster.

In your code, you configured the client to use the default settings which assumes the cluster is running on localhost with the default elasticsearch port 9200.

You need to Install Elasticsearch on a machine, configure it and run it, and then you'll be able to connect your client to the cluster and communicate with it.

Upvotes: 15

Related Questions