简介
本文档介绍如何使用对象存储 COS 的 Go SDK 获取指定向量存储桶中某个向量索引的详细信息。
功能说明
GetIndex 方法用于查询已存在的向量索引的详细配置和信息,如创建时间、向量维度、距离度量方式、数据类型以及元数据配置等。如果索引不存在,系统将返回对应的错误提示。方法原型
func (s *VectorService) GetIndex(ctx context.Context, opt *GetIndexOptions) (*GetIndexResult, *Response, error)
请求参数说明
GetIndexOptions 结构体字段说明:参数 | 描述 | 类型 | 是否必填 |
VectorBucketName | 向量桶名称,格式为 <BucketName-APPID>,例如 examplebucket-1250000000。 | string | 是 |
IndexName | 需要查询的索引名称。 | string | 是 |
返回结果说明
调用成功后,将返回
(*GetIndexResult, *Response, error) 三个值。GetIndexResult 结构体字段说明:参数 | 描述 | 类型 |
Index | 查询到的向量索引详细信息。 | *IndexInfo |
IndexInfo 结构体字段说明:参数 | 描述 | 类型 |
IndexQcs | 向量索引资源名称(QCS)。 | string |
IndexName | 索引名称。 | string |
VectorBucketName | 向量桶名称。 | string |
CreationTime | 创建时间的时间戳。 | int64 |
DataType | 向量数据类型,例如 float32。 | string |
Dimension | 向量维度。 | int |
DistanceMetric | 距离度量方式,例如 euclidean 或 cosine。 | string |
MetadataConfiguration | 元数据配置信息。 | *MetadataConfiguration |
MetadataConfiguration 结构体字段说明:参数 | 描述 | 类型 |
NonFilterableMetadataKeys | 不可用于过滤的元数据键列表,最少1个,最多10个。 | []string |
其他返回值说明:
返回值 | 描述 | 类型 |
resp | 响应头信息,包含 HTTP 响应的详细对象等。 | *cos.Response |
err | 错误信息。如果请求成功,返回 nil。 | error |
如果请求失败,将返回非
nil 的 error。向量检索相关的业务错误会封装为 *cos.VectorErrorResponse 结构,您可以通过 cos.IsVectorError() 辅助函数判断并获取详细错误信息。详细的错误处理方式、VectorErrorResponse 结构体说明及服务端错误码列表请参见 异常处理 文档。使用案例
package mainimport ("context""fmt""net/http""os"cos "github.com/tencentyun/cos-go-sdk-v5")func main() {// 生成 Vector 基础 URL,第二个参数控制是否使用 HTTPS,建议为 truevectorURL, _ := cos.NewVectorURL("ap-guangzhou", true)// 初始化 Client,设置 VectorURL 及鉴权client := cos.NewClient(&cos.BaseURL{VectorURL: vectorURL}, &http.Client{Transport: &cos.AuthorizationTransport{SecretID: os.Getenv("COS_VECTORS_SECRET_ID"),SecretKey: os.Getenv("COS_VECTORS_SECRET_KEY"),},})// 向量桶名称格式必须为:BucketName-APPIDbucketName := "examplebucket-1250000000"indexName := "my-index"// 构造查询索引的参数opt := &cos.GetIndexOptions{VectorBucketName: bucketName,IndexName: indexName,}// 调用查询索引方法res, _, err := client.Vector.GetIndex(context.Background(), opt)if err != nil {if vecErr, ok := cos.IsVectorError(err); ok {fmt.Printf("向量服务错误,错误码: %s, 错误信息: %s, 请求ID: %s\\n",vecErr.Code, vecErr.Message, vecErr.RequestID)} else {fmt.Printf("请求失败: %v\\n", err)}return}// 打印查询到的向量索引详细信息fmt.Printf("索引名称: %s\\n", res.Index.IndexName)fmt.Printf("资源名称 QCS: %s\\n", res.Index.IndexQcs)fmt.Printf("所属向量桶: %s\\n", res.Index.VectorBucketName)fmt.Printf("向量维度: %d\\n", res.Index.Dimension)fmt.Printf("数据类型: %s\\n", res.Index.DataType)fmt.Printf("距离度量: %s\\n", res.Index.DistanceMetric)fmt.Printf("创建时间戳: %d\\n", res.Index.CreationTime)}