[Errno 111] Connection refused)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "for_bulk_insert.py", line 61, in
es.indices.delete(index=index_name, ignore=[400, 404])
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/client/utils.py", line 152, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/client/indices.py", line 286, in delete
"DELETE", _make_path(index), params=params, headers=headers
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/transport.py", line 390, in perform_request
raise e
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/transport.py", line 365, in perform_request
timeout=timeout,
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 258, in perform_request
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f68bba86358>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f68bba86358>: Failed to establish a new connection: [Errno 111] Connection refused)

1 Like
  1. Please format your code as such for easier readability.
  2. "During handling of the above exception, another exception occurred:" What is "above" and what is "another" here? Also adding the relevant code snippet might help to figure out the problem.

HIi.. DEar..

#importing Dictionary
import psycopg2
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import datetime
import pandas as pd
from pandas import DataFrame


#Establishing the connection with ELK
es= Elasticsearch(['    '], http_auth=('    ',  '   '))

#es.cluster.health(wait_for_status='yellow', request_timeout=2)


#establishing the connection
conn = psycopg2.connect( database='  ', user=' ', password=' ', host=' ', port= ' ' )#configured database 


if conn:
    print ("Connected Successfully")
else:
    print ("Connection Not Established")


mydict = create_dict()
query="select * from emr.patient_summarized_test_data_trans Limit 1000" 
cursor = conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
df = DataFrame(result,columns=["summarized_test_data_id","package_id","panel_id",
                            "test_id","loinc_nm","test_type","test_result","patient_id","hos_patient_id"])



json_str = df.to_json(orient='records')

json_records = json.loads(json_str)

es = Elasticsearch()
index_name = 'in-dev-patient-summary-research6'
doctype = 'summary-research6'
es.indices.delete(index=index_name, ignore=[400, 404])
es.indices.create(index=index_name, ignore=400)
action_list = []
for row in json_records:
    record ={
        '_op_type': 'index',
        '_index': index_name,
        '_type' : doctype,
        '_source': row
    }
    action_list.append(record)
helpers.bulk(es, action_list,request_timeout=3600)

Output;
File "/home/subham/subhamenv/lib64/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 258, in perform_request
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f68bba86358>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f68bba86358>: Failed to establish a new connection: [Errno 111] Connection refused)

What happens if you try a cURL request (curl -u <user>:<password> <host>) from the machine where you are running the Python code against Elasticsearch? This looks like a connection problem — wrong port, firewall, Elasticsearch not bound to the interface you're trying to access,...

Dear xeraa... I got this response

(subhamenv) [user@abc ~]$ `curl -u <user>:<password> <host>`
{
  "name" : "elk.abc.care",
  "cluster_name" : "elk-dev",
  "cluster_uuid" : "DUUGqBupTmyBar4fN4-tiQ",
  "version" : {
    "number" : "7.9.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "083627f112ba94dffc1232e8b42b73492789ef91",
    "build_date" : "2020-09-01T21:22:21.964974Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}```

That looks good. Can you try a minimal connection example (with security enabled): https://elasticsearch-py.readthedocs.io/en/7.9.1/#ssl-and-authentication?

Not sure if you have set up TLS, but maybe you are missing scheme="https"?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.