使用python连接elasticsearch

有一个困惑了好久的问题,那就是从python里面连接elasticsearch总是报错。大致长这样

一开始我是看网上把es的安全功能关闭,也就是下面的内容,这个要进入到es的docker中去改config/elasticsearch.yml配置文件,但是这样改了以后kibana的界面会没有search功能。这种方式不可取...

shell 复制代码
xpack.security.enabled: false
xpack.security.http.ssl.enabled: false

不过这里介绍使用一种证书的方式连接,这应该是当前最合适的方式了。

具体操作如下

① 进入到elasticsearch的docker环境中

shell 复制代码
docker exec -it elasticsearch bash

②进入到证书目录下

shell 复制代码
cd /usr/share/elasticsearch/config/certs

有一个证书文件http_ca.crt

③执行一个命令

shell 复制代码
openssl x509 -fingerprint -sha256 -in ./http_ca.crt

会生成一个Fingerprint字符串,拿到这里就可以了。

④代码调用

py 复制代码
from elasticsearch import Elasticsearch
CERT_FINGERPRINT = ' ' #这里就是刚才获得的Fingerprint字符串
ELASTIC_PASSWORD = ' ' #你的用户名对应密码

client = Elasticsearch(
    "https://localhost:9200",
    ssl_assert_fingerprint=CERT_FINGERPRINT,
    basic_auth=("elastic", ELASTIC_PASSWORD)
)
client.info()

然后就出现了这个好看的东西哈哈哈

相关推荐
cup1130 分钟前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi003 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵4 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf5 小时前
Agent 流程编排
后端·python·agent
copyer_xyf6 小时前
Agent RAG
后端·python·agent
copyer_xyf6 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf6 小时前
Agent 记忆管理
后端·python·agent
星云穿梭21 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵21 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏