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

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

相关推荐
花酒锄作田6 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪10 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽11 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战11 小时前
Pydantic配置管理最佳实践(一)
python
Elasticsearch16 小时前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
阿尔的代码屋17 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅2 天前
Python函数入门详解(定义+调用+参数)
python