尝试遵循Python Elasticsearch示例用法的“连接被拒绝”错误

Kur*_*eek 5 python elasticsearch

我正在尝试为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"])
Run Code Online (Sandbox Code Playgroud)

但是,出现以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

有人可以澄清为什么这行不通吗?我是否需要执行其他任何命令来“设置” Elasticsearch?所引用的网站未提供任何其他说明或澄清。

ajk*_*ajk 4

Python Elasticsearch 客户端只是这个难题的一部分,它需要与 Elasticsearch 服务器配合使用。当你这样调用时:

es = Elasticsearch()
Run Code Online (Sandbox Code Playgroud)

您正在设置与 Elasticsearch 主机的客户端连接。不带参数调用它使用默认值,尝试访问本地主机上的端口 9200。

要设置 Elasticsearch 服务器,访问Elasticsearch 站点并查看其有关在本地或云中运行的文档(无论您感兴趣)可能是最有用的。按照下载说明操作后,Elasticsearch 将在本地主机端口 9200 上运行,这将使您的示例正常运行。