袁庭新ES系列15节|Elasticsearch客户端基础操作
from elasticsearch import Elasticsearch
# 连接到Elasticsearch
es = Elasticsearch("http://localhost:9200")
# 创建一个新的索引
response = es.indices.create(index='customer', ignore=400)
print(response)
# 获取所有索引
response = es.indices.get_alias("*")
print(response)
# 在索引中添加文档
doc = {
'name': 'John Doe',
'age': 30,
'about': 'I love to go rock climbing',
'interests': ['sports', 'music']
}
response = es.index(index="customer", id=1, document=doc)
print(response)
# 获取索引中的文档
response = es.get(index="customer", id=1)
print(response)
# 更新索引中的文档
doc = {
'name': 'Jane Doe',
'age': 25,
'about': 'I love to collect rock albums',
'interests': ['music']
}
response = es.update(index="customer", id=1, document=doc)
print(response)
# 删除索引中的文档
response = es.delete(index="customer", id=1)
print(response)
# 删除索引
response = es.indices.delete(index='customer', ignore=[400, 404])
print(response)
这段代码展示了如何使用Elasticsearch Python客户端库来执行基本的操作,包括创建索引、获取索引列表、添加、获取、更新和删除文档。这对于学习如何与Elasticsearch交互非常有帮助。
评论已关闭