You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
向量索引
CreateIndex(TOS Vectors Node.js SDK)
复制全文
下载 pdf
CreateIndex(TOS Vectors Node.js SDK)
CreateIndex 接口用于创建向量索引,本文介绍如何通过 TOS Vectors Node.js SDK 的 CreateIndex 接口完成该操作。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见配置长期访问凭证。​
  • TOS Vectors Node.js SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见常见配置场景。​
  • 向量索引名需要在向量桶中唯一。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 CreateIndex 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:CreateIndex​
代码示例​
创建向量索引​
​
// 创建向量索引​
​
import {​
TosVectorsClient,​
TosClientError,​
TosServerError,​
DataType,​
DistanceMetricType,​
} from "@volcengine/tos-sdk";​
​
// 从环境变量中获取认证信息和配置​
const accessKeyId = process.env["TOS_ACCESS_KEY"] as string;​
const accessKeySecret = process.env["TOS_SECRET_KEY"] as string;​
const region = 'Provide your region';​
const endpoint = 'Provide your vector endpoint';​
​
const vectorBucketName = 'Provide your vector bucket name';​
const accountId = process.env["TOS_ACCOUNT_ID"];​
​
// 初始化 TosVectorsClient 客户端​
const client = new TosVectorsClient({​
accessKeyId,​
accessKeySecret,​
region,​
endpoint,​
});​
​
/**​
* 处理 SDK 调用过程中可能出现的错误。​
* @param error 捕获到的错误对象​
*/​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
// 客户端异常,通常是参数错误或网络问题​
console.log("Client Error:");​
console.log(" Message:", error.message);​
if (error.stack) {​
console.log(" Stack:", error.stack);​
}​
} else if (error instanceof TosServerError) {​
// 服务端异常,表示请求已送达但处理失败​
console.log("Server Error:");​
console.log(" Request ID:", error.requestId);​
console.log(" Status Code:", error.statusCode);​
console.log(" Code:", error.code);​
console.log(" Message:", error.message);​
console.log(" Headers:", error.headers);​
} else {​
// 其他未知异常​
console.log("Unexpected Error:");​
console.log(" Message:", error.message);​
}​
}​
​
async function main() {​
// 为当前示例准备索引名称和配置信息​
const indexName = `example-index-${Date.now()}`; // 示例索引名称,实际使用时请根据业务命名​
const dimension = 128; // 向量维度,例如 128 维​
​
// 可选的元数据配置:配置不可用于过滤的元数据字段​
const metadataConfiguration = {​
nonFilterableMetadataKeys: ["timestamp", "source"],​
};​
​
try {​
console.log(`开始创建向量索引: ${indexName} ...`);​
​
// 1. 调用 createIndex 创建向量索引​
const createResult = await client.createIndex({​
vectorBucketName,​
accountID: accountId,​
indexName,​
dataType: DataType.DataTypeFloat32, // 向量数据类型,当前支持 float32​
dimension, // 向量维度​
distanceMetric: DistanceMetricType.DistanceMetricEuclidean, // 向量相似度度量方式,这里示例使用欧式距离​
metadataConfiguration, // 可选元数据配置​
});​
​
// 2. 打印通用返回信息​
console.log("CreateIndex Request Succeeded:");​
console.log(" Request ID:", createResult.requestId);​
console.log(" Status Code:", createResult.statusCode);​
console.log(" Headers:", createResult.headers);​
​
// createIndex 的业务返回体为空对象 {},此处仅打印占位信息​
console.log(" Business Data (CreateIndex):", createResult.data);​
​
// 3. 调用 getIndex 获取刚刚创建的索引详情,以展示业务数据​
console.log("开始查询刚创建的向量索引详情...");​
​
const getResult = await client.getIndex({​
vectorBucketName,​
accountID: accountId,​
indexName,​
});​
​
// 打印 getIndex 的通用返回信息​
console.log("GetIndex Request Succeeded:");​
console.log(" Request ID:", getResult.requestId);​
console.log(" Status Code:", getResult.statusCode);​
console.log(" Headers:", getResult.headers);​
​
// 打印索引的关键信息​
const index = getResult.data.index;​
console.log(" Index Detail:");​
console.log(` - Index Name: ${index.indexName}`);​
console.log(` - Data Type: ${index.dataType}`);​
console.log(` - Dimension: ${index.dimension}`);​
console.log(` - Distance Metric: ${index.distanceMetric}`);​
console.log(` - Creation Time: ${index.creationTime}`);​
console.log(` - Index TRN: ${index.indexTrn}`);​
console.log(` - Vector Bucket Name: ${index.vectorBucketName}`);​
​
// 打印元数据配置(如存在)​
if (​
index.metadataConfiguration &&​
index.metadataConfiguration.nonFilterableMetadataKeys &&​
index.metadataConfiguration.nonFilterableMetadataKeys.length > 0​
) {​
console.log(​
" - Non-filterable Metadata Keys:",​
index.metadataConfiguration.nonFilterableMetadataKeys.join(", "),​
);​
} else {​
console.log(" - Non-filterable Metadata Keys: <none>");​
}​
} catch (error: any) {​
handleError(error);​
}​
}​
​
main();​
​
​
​
最近更新时间:2026.08.03 11:05:44
这个页面对您有帮助吗?
有用
有用
无用
无用