es:python访问elasticsearch

一,官方文档

https://www.elastic.co/docs/reference/elasticsearch/clients/python

二,安装第三方库

$ pip install elasticsearch

三,代码

from elasticsearch import Elasticsearch

# 连接到 Elasticsearch,替换为实际的 IP 地址和密码
# , basic_auth=('elastic', 'Elastic_j625sz')
es = Elasticsearch('http://127.0.0.1:9200')

# 检查连接
if es.ping():
    print('连接成功')
else:
    print('连接失败')


# 创建索引
es.indices.create(index="my_index")

# 检查索引是否存在
is_exist = es.indices.exists(index="my_index")
print("索引my_index是否存在:"+str(is_exist))

# 删除索引
es.indices.delete(index="my_index")

# 检查索引是否存在
is_exist = es.indices.exists(index="my_index")
print("索引my_index是否存在:"+str(is_exist))

四,测试 效果 :

$ python3 seek.py 
连接成功
索引my_index是否存在:True
索引my_index是否存在:False

 

posted @ 2025-12-16 22:47  刘宏缔的架构森林  阅读(8)  评论(0)    收藏  举报