尝试遵循Python Elasticsearch示例usag时出现“Connection declined”错误

2024-04-24 14:51:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但是,我得到了以下错误:

^{pr2}$

有人能解释一下为什么这不起作用吗?我还需要执行其他命令来“设置”Elasticsearch吗?参考网站不提供任何附加说明或澄清。在


Tags: textfromtestimportdatetimeindexdoces
1条回答
网友
1楼 · 发布于 2024-04-24 14:51:24

Python Elasticsearch客户端只是其中的一部分,它需要与Elasticsearch服务器一起工作。当你这样称呼:

es = Elasticsearch()

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

对于设置一个Elasticsearch服务器,点击Elasticsearch site并查看他们关于在本地或云中运行的文档(无论哪一个对您有吸引力)。按照download的说明,您可以在本地主机端口9200上运行Elasticsearch,这将使您的示例正常工作。在

相关问题 更多 >