首页 > 解决方案 > 使用 python elasticsearch 时出现连接错误

问题描述

我试图执行下面的python代码

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.create(index='my-index', ignore=400)

通过以下连接错误的最后一条语句。

ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 
0x0000020DC5EEF6D8>: Failed to establish a new connection: [WinError 10061] No 
connection could be made because the target machine actively refused it) caused 
by: NewConnectionError(<urllib3.connection.HTTPConnection object at 
0x0000020DC5EEF6D8>: Failed to establish a new connection: [WinError 10061] No 
connection could be made because the target machine actively refused it)

标签: pythonelasticsearch

解决方案


看来您已经安装了唯一的elasticsearch 客户端库,而没有在计算机中安装实际的 Elasticsearch。因此首先按如下方式安装 Elasticsearch。下载并安装

在你下载了一个 zip 文件(如果你使用的是 windows)之后,进入类似c:\elasticsearch-7.5.2

转到类似的路径C:\elasticsearch-7.5.2\bin并运行elasticsearch.bat

然后转到您的浏览器并运行http://localhost:9200/,您应该得到这样的响应在此处输入图像描述这意味着您的弹性实例正在运行,因此您可以继续

es.indices.create(index='my-index', ignore=400)

如果您想要自定义实例,您可以在此处阅读教程

如果您在使用 python elasticsearch 时遇到连接错误错误,则已在此处回答


推荐阅读