接口定义
add_index() 接口用于在已有集合上,新增标量字段索引。
def add_index(self,database_name: str,collection_name: str,indexes: List[FilterIndex],build_existed_data: bool = True,timeout: Optional[float] = None) -> dict:
说明:
如果集合开启了动态 schema(即创建集合时配置了 FilterIndexConfig 参数),不支持创建标量索引。
当前不支持创建 json 类型的标量索引。
使用实例
import tcvectordbfrom tcvectordb.model.document import Filterfrom tcvectordb.model.enum import FieldType, IndexTypefrom tcvectordb.model.index import Index, FilterIndexres = client.add_index(database_name='db-test',collection_name='book-vector',indexes=[FilterIndex(name='bookInfo', field_type=FieldType.String, index_type=IndexType.FILTER),FilterIndex(name='page', field_type=FieldType.Uint64, index_type=IndexType.FILTER),],build_existed_data=True)print(res)
请求参数
参数 | 子参数 | 是否必选 | 参数含义 | 配置方法及要求 |
database_name | - | 是 | 配置需重建索引的 Database 名称。 | 获取集群中的数据库列表,复制需重建索引的集合所属的数据库名。 |
collection_name | - | 是 | 指定需重建索引的 Collection 名称。 | 获取指定数据库名下的 Collection 列表,复制需重建索引的集合名。 |
indexes | name | 是 | 指定新增标量索引的字段。 | 用户自定义字段名。命名规则为1-128字符,只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。 |
| field_type | | 指定字段数据类型。 | 指定自定义字段的数据类型。取值如下: String:字符型。 Uint64:指无符号整数(unsigned integer)。 Array:数组类型,数组元素为 string。 |
| index_type | | 指定索引类型。 | 仅支持设置为 FILTER。 |
build_existed_data | - | 是 | 用于指定新增的索引字段是否写入过数据。 | True:默认为 True,新增字段已写入过数据,需扫描历史数据并构建索引。 False:新增索引字段为新增字段,无需扫描历史数据,可以快速创建索引。 |
相关说明
调用add_index() 接口新增标量索引之后,使用 describe_collection() 接口查看 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": ""}}}