以下是一些常用的 Elasticsearch API 示例代码,可以帮助您更好地理解和使用 Elasticsearch。

搜索示例

from elasticsearch import Elasticsearch

# 创建 Elasticsearch 客户端
es = Elasticsearch()

# 搜索索引
response = es.search(index="my_index", body={"query": {"match_all": {}}})
print(response['hits']['hits'])

文档操作示例

# 创建文档
es.index(index="my_index", id=1, body={"user": "kimchy"})

# 更新文档
es.update(index="my_index", id=1, body={"doc": {"first_name": "John"}})

# 获取文档
response = es.get(index="my_index", id=1)
print(response['_source'])

# 删除文档
es.delete(index="my_index", id=1)

索引操作示例

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

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

附加资源

更多关于 Elasticsearch API 的信息,请访问官方文档

返回首页