CreateVikingdbIndex 接口用于为指定的数据集 Collection 创建索引,以提升向量检索性能。
创建索引可以加速向量的相似度搜索,它根据指定的索引算法和数据结构将向量库中的原始数据进行分组排序,提高相似度搜索的效率和准确性,是驱动向量数据库在短时间内筛选出候选的核心所在。
对于索引的数据集只存在稠密向量(即 vector 类型字段)的情况,我们称这种索引为纯稠密索引;对于索引的数据集中存在稠密向量和稀疏向量(vector 和 sparse_vector 类型字段)的情况,我们称这种索引为混合索引。
Python SDK 通过 VIKINGDBApi().create_vikingdb_index(request) 发起调用,request 类型为 volcenginesdkvikingdb.CreateVikingdbIndexRequest。
下表列出了 CreateVikingdbIndexRequest 可配置的字段,字段含义可结合 examples/vikingdb_examples.py 中的示例理解。
参数 | 类型 | 是否必选 | 描述 |
|---|---|---|---|
project_name | str | 否 | 项目名称,对应 API 字段 |
collection_name | str | 二选一 | 数据集名称,对应 API 字段 |
resource_id | str | Collection 的资源 ID,对应 API 字段 | |
index_name | str | 是 | 要创建的索引名,对应 API 字段
|
description | str | 否 | 索引描述,对应 API 字段 |
cpu_quota | int | 否 | 创建/检索索引使用的 CPU 配额,对应 API 字段 |
scalar_index | list[str] | 否 | 要建立标量索引的字段名列表,对应 API 字段 |
shard_policy | str | 否 | 分片策略,对应 API 字段 |
shard_count | int | 否 | 索引分片数,对应 API 字段 |
vector_index | VectorIndexForCreateVikingdbIndexInput | 否 | 向量索引配置,对应 API 字段 |
参数 | 类型 | 是否必选 | 默认值 | 描述 |
|---|---|---|---|---|
index_type | str | 否 | 索引类型,对应 API 字段 | |
distance | str | 否 | 向量距离类型,对应 API 字段 | |
hnsw_m | int | 否 | HNSW 图的最大邻居数,对应 API 字段 | |
hnsw_cef | int | 否 | HNSW CEF(构建图时的搜索广度),对应 API 字段 | |
hnsw_sef | int | 否 | HNSW SEF(在线检索的搜索广度),对应 API 字段 | |
diskann_m | int | 否 | DiskANN 图每个节点的出度,对应 API 字段 | |
diskann_cef | int | 否 | DiskANN 构建/查询时的扩展因子,对应 API 字段 | |
cache_ratio | float | 否 | 磁盘索引的热点缓存比例,对应 API 字段 | |
pq_code_ratio | float | 否 | PQ 压缩比例,对应 API 字段 | |
quant | str | 否 | 量化类型,对应 API 字段 |
返回值类型为 CreateVikingdbIndexResponse。
参数 | 类型 | 示例值 | 描述 |
|---|---|---|---|
message | str | success | 请求状态描述,对应 API 字段 |
import os import volcenginesdkcore import volcenginesdkvikingdb as vdb from volcenginesdkvikingdb.api.vikingdb_api import VIKINGDBApi configuration = volcenginesdkcore.Configuration() configuration.ak = os.environ["VIKINGDB_AK"] configuration.sk = os.environ["VIKINGDB_SK"] configuration.region = os.environ["VIKINGDB_REGION"] configuration.host = os.environ["VIKINGDB_HOST"] configuration.scheme = "https" volcenginesdkcore.Configuration.set_default(configuration) client = VIKINGDBApi() request = vdb.CreateVikingdbIndexRequest( project_name="default", collection_name="sdk_demo_collection", index_name="sdk_demo_index", description="Demo HNSW index", shard_policy="auto", vector_index=vdb.VectorIndexForCreateVikingdbIndexInput( index_type="hnsw", distance="cosine", hnsw_m=32, hnsw_cef=200, ), ) response = client.create_vikingdb_index(request) print("message:", response.message)