Elasticsearch python client connection timed out

36 views Asked by At

I have the following error:

elastic_transport.ConnectionTimeout: Connection timeout caused by:
ConnectionTimeout(Connection timeout caused by: 
ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): 
Read timed out. (read timeout=9.999005171936005)))
from elasticsearch import Elasticsearch
import os
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD')
client = Elasticsearch('http://localhost:9200', basic_auth=("elastic", ELASTIC_PASSWORD), max_retries=3, retry_on_timeout=True)
doc = {
    'text': 'Interesting content',
}
client.ping() # returns True!!
# the program reports error
client.indices.create(index="test-index", timeout="30s") 

Solutions I have tried:

  1. increasing timeout
  2. add localhost to config/elasticsearch.yml
  3. use the unauthenticated version, i.e.:
client = Elasticsearch('http://localhost:9200')
  1. use https (would produce connection error)

Also, note that I have been able to create the index once when I first installed Elasticsearch python client. It failed the second and third time I ran the script. Another behavior I observed was that http://localhost:9200 is always up even if I closed the program...

Installation:

  1. Elasticsearch - I wget and un-tar it
  2. python client: python -m pip install elasticsearch
1

There are 1 answers

0
Abraham Hou On

Self-answer: I did not work it through with local Elasticsearch client, but cloud client seems to work fine for me:

client = Elasticsearch(cloud_id="YOUR_CLOUD_ID", api_key="YOUR_API_KEY")