以下是一个使用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

用python的requests写一个对elasticsearch的删除索引函数

原文地址: https://www.cveoy.top/t/topic/co0d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录