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

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

相关推荐
花酒锄作田4 小时前
Pydantic校验配置文件
python
hboot4 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi15 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi16 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽16 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户83580861879117 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python