createIndex 用于为指定的数据集 Collection 创建索引 Index。
创建索引可以加速向量的相似度搜索,它根据指定的索引算法和数据结构将向量库中的原始数据进行分组排序,提高相似度搜索的效率和准确性,是驱动向量数据库在短时间内筛选出候选的核心所在。
对于索引的数据集只存在稠密向量(即 vector 类型字段)的情况,我们称这种索引为纯稠密索引;对于索引的数据集中存在稠密向量和稀疏向量(vector 和 sparse_vector 类型字段)的情况,我们称这种索引为混合索引。
public CreateVikingdbIndexResponse createVikingdbIndex(CreateVikingdbIndexRequest body) throws ApiException
请求参数是 CreateIndexParam,CreateIndexParam 类包括的参数如下表所示。
参数 | 类型 | 是否必选 | 参数说明 |
|---|---|---|---|
projectName | String | 否 | 项目名称 |
collectionName | String | 2选1 | 数据集名称 |
resourceId | String | 数据集资源ID。请求必须指定ResourceId和CollectionName其中之一。 | |
indexName | string | 是 | 指定创建的索引 Index 名称。
|
description | String | 否 | 索引的自定义描述。 |
scalarIndex | List | 否 | 标量字段列表,用于设置需要构建到标量索引的字段。 |
shardPolicy | ShardPolicyEnum | 否 | 索引分片类型
|
shardCount | Integer | 否 | 自定义分片数。 |
CpuQuota | Integer | 否 | 索引检索消耗的 CPU 配额,格式为正整数。 |
vectorIndex | VectorIndexForCreateVikingdbIndexInput | 是 | 见下 |
参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
|---|---|---|---|---|
indexType | IndexTypeEnum | 是 | IndexType.HNSW | 向量索引类型。取值如下:
|
distance | DistanceEnum枚举 | 否 | DistanceType.IP | 距离类型,衡量向量之间距离的算法。取值如下:
对于hnsw_hybrid索引算法,距离类型选择只对稠密向量生效,稀疏向量仅支持内积。 |
quant | QuantEnum枚举 | 否 | QuantType.Int8 | 量化方式。量化方式是索引中对向量的压缩方式,可以降低向量间相似性计算的复杂度。基于向量的高维度和大规模特点,采用向量量化可以有效减少向量的存储和计算成本。取值如下:
|
hnswM | Integer | 否 | 20 | hnsw 索引参数,表示邻居节点个数。
|
hnswCef | Integer | 否 | 400 | hnsw 索引参数,表示构建图时搜索邻居节点的广度。
|
hnswSef | Integer | 否 | 800 | hnsw 索引参数,表示线上检索的搜索广度。
|
参数 | 类型 | 示例值 | 描述 |
|---|---|---|---|
message | String | success | 操作结果信息 |
package org.example.newsubproduct.console.index; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.sign.Credentials; import com.volcengine.vikingdb.VikingdbApi; import com.volcengine.vikingdb.model.*; import java.util.Arrays; public class CreateVikingdbIndexWithScalarIndex { public static void main(String[] args) { String ak = System.getenv("AK"); // ak String sk = System.getenv("SK"); // sk String endpoint = "vikingdb.cn-beijing.volcengineapi.com"; // 填写向量库控制面v2的域名 https://www.volcengine.com/docs/84313/1792715 String region = "cn-beijing"; // 服务区域 ApiClient apiClient = new ApiClient() .setEndpoint(endpoint) .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); VikingdbApi api = new VikingdbApi(apiClient); CreateVikingdbIndexRequest request = new CreateVikingdbIndexRequest() .collectionName("test_collection_for_product") .indexName("idx_with_scalar_index") .vectorIndex(new VectorIndexForCreateVikingdbIndexInput() .indexType(VectorIndexForCreateVikingdbIndexInput.IndexTypeEnum.HNSW_HYBRID) .distance(VectorIndexForCreateVikingdbIndexInput.DistanceEnum.IP) .quant(VectorIndexForCreateVikingdbIndexInput.QuantEnum.INT8) ) .scalarIndex(Arrays.asList("product_type", "brand_name", "price", "sales") ); try { CreateVikingdbIndexResponse response = api.createVikingdbIndex(request); System.out.println("response body: " + response); System.out.println(); System.out.println("response meta RequestId: " + response.getResponseMetadata().getRequestId()); System.out.println("response meta Service: " + response.getResponseMetadata().getService()); System.out.println("response meta Region: " + response.getResponseMetadata().getRegion()); System.out.println("response meta Action: " + response.getResponseMetadata().getAction()); System.out.println("response meta Version: " + response.getResponseMetadata().getVersion()); } catch (ApiException e) { System.out.println("exception http code: " + e.getCode()); System.out.println("exception response body: " + e.getResponseBody()); System.out.println(); System.out.println("exception RequestId: " + e.getResponseMetadata().getRequestId()); System.out.println("exception Action: " + e.getResponseMetadata().getAction()); System.out.println("exception Region: " + e.getResponseMetadata().getRegion()); System.out.println("exception Service: " + e.getResponseMetadata().getService()); System.out.println("exception Error.Code: " + e.getResponseMetadata().getError().getCode()); System.out.println("exception Error.Message: " + e.getResponseMetadata().getError().getMessage()); } } }