You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Amazon OpenSearch Serverless中删除集合内的索引?

删除AWS OpenSearch Serverless集合内索引的方法

方法1:使用opensearch-py客户端(推荐)

AWS OpenSearch Serverless兼容标准OpenSearch的REST API,因此可以用官方维护的opensearch-py客户端执行索引删除操作,只需配置AWS签名认证即可。

  1. 先安装依赖包:
pip install opensearch-py requests-aws4auth
  1. 示例代码:
import boto3
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth

# 替换为你的实际配置
region = "us-east-1"
host = "your-collection-id.us-east-1.aoss.amazonaws.com"
target_index = "要删除的索引名称"

# 获取AWS凭证并生成认证对象
credentials = boto3.Session().get_credentials()
aws_auth = AWS4Auth(
    credentials.access_key,
    credentials.secret_key,
    region,
    "aoss",
    session_token=credentials.token
)

# 初始化OpenSearch客户端
client = OpenSearch(
    hosts=[{"host": host, "port": 443}],
    http_auth=aws_auth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)

# 执行索引删除
delete_response = client.indices.delete(index=target_index)
print(f"删除结果: {delete_response}")

方法2:通过requests直接调用REST API

如果不想引入专用客户端,也可以直接发送HTTP DELETE请求到索引端点,配合AWS签名完成认证。

示例代码:

import boto3
import requests
from requests_aws4auth import AWS4Auth

region = "us-east-1"
host = "https://your-collection-id.us-east-1.aoss.amazonaws.com"
target_index = "要删除的索引名称"

credentials = boto3.Session().get_credentials()
aws_auth = AWS4Auth(
    credentials.access_key,
    credentials.secret_key,
    region,
    "aoss",
    session_token=credentials.token
)

# 构造删除索引的请求URL
delete_url = f"{host}/{target_index}"
response = requests.delete(delete_url, auth=aws_auth)

print(f"响应状态码: {response.status_code}")
print(f"响应内容: {response.json()}")

关键注意事项

  • 确保你的IAM身份(用户/角色)拥有aoss:APIAccess权限,且权限策略明确允许对目标索引执行delete操作。
  • 索引名称大小写敏感,必须与创建时的名称完全匹配。
  • 若目标索引不存在,请求会返回404错误,可根据业务需求添加异常捕获逻辑。

内容的提问来源于stack exchange,提问作者Krishna Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 12:48:15