Python Requests: 删除 Elasticsearch 索引的函数
以下是一个使用 Python 的 requests 库的删除 Elasticsearch 索引的示例函数:
import requests
def delete_index(index_name, host='http://localhost:9200'):
url = f'{host}/{index_name}'
response = requests.delete(url)
if response.status_code == 200:
print(f'Index {index_name} deleted successfully.')
else:
print(f'Error deleting index {index_name}: {response.content}')
此函数使用 HTTP DELETE 方法从 Elasticsearch 服务器删除指定名称的索引。它将返回 HTTP 响应代码以指示操作是否成功,并在操作成功时打印一条成功消息。如果操作失败,它将打印错误消息并将响应内容打印到控制台。
要使用此函数,请传入要删除的索引名称作为第一个参数。如果您的 Elasticsearch 服务器不在本地主机上,请传入其 URL 作为第二个参数。如果未指定 URL,则将默认使用 'http://localhost:9200'。
原文地址: https://www.cveoy.top/t/topic/nuWU 著作权归作者所有。请勿转载和采集!