gpt4 book ai didi

python - 尝试遵循 Python Elasticsearch 示例用法时出现“连接被拒绝”错误

转载 作者:太空狗 更新时间:2023-10-30 01:20:16 33 4
gpt4 key购买 nike

我正在尝试为 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"])

但是,我收到以下错误:

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)

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

最佳答案

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

es = Elasticsearch()

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

对于设置 Elasticsearch 服务器,点击 Elasticsearch site 可能是最有用的并查看他们关于在本地或云端运行的文档(以您喜欢的为准)。关注download说明将使您在本地主机端口 9200 上运行 Elasticsearch,这应该可以让您的示例正常工作。

关于python - 尝试遵循 Python Elasticsearch 示例用法时出现“连接被拒绝”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917549/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com