使用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()

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

相关推荐
starzy19902 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
wuyk5553 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
kyle~9 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享9 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
Elasticsearch10 小时前
Java4Shell 远程代码执行漏洞 - CVE-2026-114938
elasticsearch
梯度下降者10 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.11 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人11 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__11 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
Elasticsearch12 小时前
哈利波特问答 Agent Builder + Workflow 教程
elasticsearch