Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Connection refused' error trying to follow the Python Elasticsearch example usage

I'm trying to run the 'example usage' script for Python Elasticsearch:

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])

res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])

es.indices.refresh(index="test-index")

res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

However, I get the following error:

Traceback (most recent call last):
  File "elasticsearch_example_usage.py", line 10, in <module>
    res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 279, in index
    _make_path(index, doc_type, id), params=params, body=body)
  File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 327, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/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 0x7f0be9c14890>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f0be9c14890>: Failed to establish a new connection: [Errno 111] Connection refused)

Can someone clarify why this doesn't work? Are there any additional commands I have to do to 'set up' Elasticsearch? The referenced website does not give any additional instruction or clarification.

like image 323
Kurt Peek Avatar asked Dec 01 '16 18:12

Kurt Peek


1 Answers

The Python Elasticsearch client is just one piece of the puzzle, it needs to work with an Elasticsearch server. When you call this:

es = Elasticsearch()

You're setting up a client connection to an Elasticsearch host. Calling it without arguments uses default values, attempting to hit port 9200 on localhost.

For setting up an Elasticsearch server, it's probably most useful to hit the Elasticsearch site and look at their documentation about running locally or in the cloud (whichever appeals to you). Following the download instructions will leave you with Elasticsearch running on localhost port 9200, which should get your example working.

like image 187
ajk Avatar answered Sep 20 '22 16:09

ajk