新增索引

最近更新时间:2025-08-26 15:07:31

我的收藏

接口定义

AddIndex()接口用于在已有集合上,新增标量字段索引。
AddIndex(ctx context.Context, databaseName, collectionName string, params ...*AddIndexParams) (err error)
说明:
如果集合开启了动态 schema(即创建集合时配置了 filterIndexConfig 参数),不支持创建标量索引。
当前不支持创建 json 类型的标量索引。

使用示例

var (
ctx = context.Background()
database = "db-test"
collectionName = "go-sdk-test"
)
buildExistedData := true
addFilterIndexs := []tcvectordb.FilterIndex{
{FieldName: "bookInfo", FieldType: tcvectordb.String, IndexType: tcvectordb.FILTER},
{FieldName: "page", FieldType: tcvectordb.Uint64, IndexType: tcvectordb.FILTER}}
err := cli.AddIndex(ctx, database, collectionName, &tcvectordb.AddIndexParams{
FilterIndexs: addFilterIndexs, BuildExistedData: &buildExistedData})
printErr(err)

请求参数

参数
子参数(一)
子参数(二)
是否必选
参数含义
配置方法及要求
databaseName
-
-
配置需重建索引的 Database 名称。
使用 /database/list 获取集群中的数据库列表,复制需重建索引的集合所属的数据库名。
collectionName
-
-
指定需重建索引的 Collection 名称。
使用 /collection/list 获取指定数据库名下的 Collection 列表,复制需重建索引的集合名。
AddIndexParams
FilterIndex
FieldName
指定新增标量索引的字段。
用户自定义字段名。命名规则为1-128字符,只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。
FieldType
指定字段数据类型。
指定自定义字段的数据类型。取值如下:
string:字符型。
uint64:指无符号整数(unsigned integer)。
array:数组类型,数组元素为 string。
IndexType
指定索引类型。
仅支持设置为 filter
BuildExistedData
-
指定新增的索引字段是否写入过数据。
true:默认为true,新增字段已写入过数据,需扫描历史数据并构建索引。
false:新增索引字段为新增字段,无需扫描历史数据,可以快速创建索引。

相关说明

调用 AddIndex() 新增标量索引之后,使用 DescribeCollection() 接口查看 Collection 的索引状态,当返回参数 indexStatus 中的 status 显示为 ready 时,表明新增的标量索引重建已完成,可正常写入数据。
initial:索引已创建但尚未构建,处于初始状态。
ready:索引构建成功且已就绪,可正常执行操作。
training:正在训练机器学习模型以生成向量数据。
building:正在构建向量索引结构并存储向量数据。
building_scalar:正在构建标量索引。
building_sparse:正在构建稀疏向量索引。
failed:索引构建失败,需修复后方可正常操作。
{
"code": 0,
"msg": "operation success",
"collection": {
"database": "db-test",
"collection": "coll-test",
"documentCount": 4,
"indexes": [
......
],
"indexStatus": {
"status": "ready",
"startTime": ""
}
}
}