ElasticSearch的Python Client测试
一、Python环境准备
1、下载Python安装包并安装
https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe
2、安装 SDK
参考ES官方文档: https://www.elastic.co/guide/en/elasticsearch/client/index.html
python -m pip install elasticsearch
一、Client 代码案例
1、 索引文档操作
from elasticsearch import Elasticsearch
# 定义ES的地址和端口,用户名和密码
host = "http://es-cn-************.public.elasticsearch.aliyuncs.com:9200"
username = "elastic"
password = "******"
client = Elasticsearch(host,basic_auth=(username, password))
# 使用es对象来与ES集群进行交互
# 创建索引
client.indices.create(index="heqiang")
print("创建索引成功")
# 插入文档
client.index(
index="heqiang",
id="1",
document={
"name": "jiabei",
"age": 28,
"description": "用代码改变世界的程序员"
}
)
print("插入文档成功")
client.index(
index="heqiang",
id="2",
document={
"name": "jsck",
"age": 20,
"description": "一名优秀的大学生"
}
)
print("插入文档成功")
# 查询文档
getResultData = client.get(index="heqiang", id="1")
print("查询文档结果:" + str(getResultData.body))
# 搜索文档
searchResultData = client.search(index="heqiang", query={"match": {"name": "jiabei"}})
print("搜索文档结果:" + str(searchResultData.body))
# 更新文档
client.update(index="heqiang", id="1", doc={
"name": "jiabei",
"age": 29,
"description": "用代码改变世界的程序员兼工程师"
})
print("更新文档成功")
# 删除文档
client.delete(index="heqiang", id="2")
print("删除文档成功")
# 删除索引
# client.indices.delete(index="heqiang")
# print("删除索引成功")
原文地址:https://blog.csdn.net/mrhs_dhls/article/details/143656351
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!