使用python连接es数据库并进行操作

发布时间 2023-09-08 16:03:44作者: HarsonLee

一、linux服务器安装python(最好为python3)
1、首先要先安装依赖包:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
2、下载python源码包
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.12.tgz
3、解压
tar -zxvf Python-3.7.12.tgz
4、进入解压后的目录:
cd Python-3.7.12
./configure
make && make install
5、软连接
[root@server Python-3.7.12]# mv /usr/bin/python /usr/bin/python.bak
[root@server Python-3.7.12]# ln -s /usr/local/bin/python3 /usr/bin/python
6、更新pip(若没安装重新安装)
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip3 /usr/bin/pip
7、安装es客户端
pip install elasticsearch
8、编辑清除es索引脚本
vi delete_index.py
添加如下内容

from elasticsearch import Elasticsearch

# 指定Elasticsearch集群的主机和端口
es_host = '10.10.90.211'
es_port = 9200
es_user = 'elastic'
es_password = 'Xg6=M-n1'
# 创建Elasticsearch客户端连接
es = Elasticsearch([{'host': es_host, 'port': es_port, 'scheme': 'http'}], basic_auth=(es_user, es_password))

# 要删除的索引名称
index_name = 'access_log_1'

# 检查索引是否存在
if es.indices.exists(index=index_name):
    # 删除索引
    es.indices.delete(index=index_name)
    print(f"索引 '{index_name}' 已成功删除")
else:
    print(f"索引 '{index_name}' 不存在")
# 关闭Elasticsearch连接
es.transport.close()

9、删除索引
执行python delete_index.py
10、查询索引内字段的值