Index 操作
更新时间:2025-01-14
创建索引
功能介绍
为指定表和指定字段新建索引,当前仅支持新建向量索引。
请求示例
Java
1import java.util.Arrays;
2
3import com.baidu.mochow.auth.Credentials;
4import com.baidu.mochow.client.ClientConfiguration;
5import com.baidu.mochow.client.MochowClient;
6import com.baidu.mochow.model.CreateIndexRequest;
7import com.baidu.mochow.model.entity.HNSWParams;
8import com.baidu.mochow.model.entity.SecondaryIndex;
9import com.baidu.mochow.model.entity.VectorIndex;
10import com.baidu.mochow.model.enums.IndexType;
11import com.baidu.mochow.model.enums.MetricType;
12public class Main {
13 public static void main(String[] args) {
14 String account = "root";
15 String apiKey = "*********";
16 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
17 ClientConfiguration clientConfiguration = new ClientConfiguration();
18 clientConfiguration.setCredentials(new Credentials(account, apiKey));
19 clientConfiguration.setEndpoint(endpoint);
20 MochowClient mochowClient = new MochowClient(clientConfiguration);
21 String databaseName = "test";
22 String tableName = "test";
23
24 CreateIndexRequest createIndexRequest = CreateIndexRequest.builder()
25 .database(databaseName)
26 .table(tableName)
27 .indexes(Arrays.asList(
28 new SecondaryIndex("book_name_idx", "bookName"),
29 VectorIndex.builder()
30 .indexName("vector_idx")
31 .indexType(IndexType.HNSW)
32 .fieldName("vector")
33 .params(new HNSWParams(32, 200))
34 .metricType(MetricType.L2)
35 .autoBuild(false).build()
36 ))
37 .build();
38 mochowClient.createIndex(createIndexRequest);
39 }
40}
相关类说明
CreateIndexRequest
创建索引请求,用于向量数据库创建索引,拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明
Java
1import com.baidu.mochow.model.CreateIndexRequest; //导入包
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
database | String | 是 | 建索引的库名 |
table | String | 是 | 建索引的表名 |
indexes | List<IndexField> | 是 | 插入的记录列表 |
IndexField
拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明。为VectorIndex和SecondaryIndex的基类,正常情况下不使用
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
indexName | String | 是 | 索引的名字 |
field | String | 是 | 索引作用于的字段名称 |
indexType | IndexType | 是 | 插入的记录列表 |
metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
params | IndexParams | 否 | 向量索引构建参数 |
autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
state | IndexState | 否 | 索引状态,,建索引时不需要使用。取值如下: |
SecondaryIndex
使用构造函数传入索引名称和对应的字段名称即可
Java
1SecondaryIndex(String indexName, String fieldName)
VectorIndex
使用构造函数传入对应参数即可,对应参数也拥有对应的builder构造函数
Java
1public VectorIndex(String indexName, String fieldName, IndexType indexType, IndexState state,MetricType metricType, IndexParams params, Boolean autoBuild, AutoBuildPolicy autoBuildPolicy)
删除向量索引
功能介绍
删除指定索引,当前不支持删除构建中的向量索引。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4public class Main {
5 public static void main(String[] args) {
6 String account = "root";
7 String apiKey = "*********";
8 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
9 ClientConfiguration clientConfiguration = new ClientConfiguration();
10 clientConfiguration.setCredentials(new Credentials(account, apiKey));
11 clientConfiguration.setEndpoint(endpoint);
12 MochowClient mochowClient = new MochowClient(clientConfiguration);
13 String databaseName = "test";
14 String tableName = "test";
15 mochowClient.dropIndex(databaseName, tableName, "test_index");
16 }
17}
请求参数
参数 | 参数类型 | 是否必选 | 参数含义 |
---|---|---|---|
databaseName | String | 是 | 库名 |
tableName | String | 是 | 表名 |
indexName | String | 是 | 索引名 |
重建向量索引
功能介绍
重建指定索引,当前仅支持重建向量索引。
请求示例
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4public class Main {
5 public static void main(String[] args) {
6 String account = "root";
7 String apiKey = "*********";
8 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
9 ClientConfiguration clientConfiguration = new ClientConfiguration();
10 clientConfiguration.setCredentials(new Credentials(account, apiKey));
11 clientConfiguration.setEndpoint(endpoint);
12 MochowClient mochowClient = new MochowClient(clientConfiguration);
13 String databaseName = "test";
14 String tableName = "test";
15 mochowClient.rebuildIndex(databaseName, tableName, "test_index");
16 }
17}
请求参数
参数 | 参数类型 | 是否必选 | 参数含义 |
---|---|---|---|
databaseName | String | 是 | 库名 |
tableName | String | 是 | 表名 |
indexName | String | 是 | 索引名 |
查询索引详情
功能介绍
查询指定索引的详情。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.DescribeIndexResponse;
5import com.baidu.mochow.util.JsonUtils;
6public class Main {
7 public static void main(String[] args) {
8 String account = "root";
9 String apiKey = "*********";
10 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
11 ClientConfiguration clientConfiguration = new ClientConfiguration();
12 clientConfiguration.setCredentials(new Credentials(account, apiKey));
13 clientConfiguration.setEndpoint(endpoint);
14 MochowClient mochowClient = new MochowClient(clientConfiguration);
15 String databaseName = "test";
16 String tableName = "test";
17 DescribeIndexResponse response = mochowClient.describeIndex(databaseName, tableName, "test_index");
18 System.out.printf("Describe table response: %s\n", JsonUtils.toJsonString(response));
19 }
20}
请求参数
参数 | 类型 | 是否必填 | 参数含义 |
---|---|---|---|
databaseName | String | 是 | 库名 |
tableName | String | 是 | 表名 |
indexName | String | 是 | 索引名 |
DescribeIndexResponse
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
indexField | IndexField | 是 | 索引详细信息 |
IndexField
拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明。为VectorIndex和SecondaryIndex的基类,正常情况下不使用
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
indexName | String | 是 | 索引的名字 |
field | String | 是 | 索引作用于的字段名称 |
indexType | IndexType | 是 | 插入的记录列表 |
metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
params | IndexParams | 否 | 向量索引构建参数 |
autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
state | IndexState | 否 | 索引状态,,建索引时不需要使用。取值如下: |
修改索引
功能介绍
修改向量索引信息,目前只支持修改autoBuild属性。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.ModifyIndexRequest;
5import com.baidu.mochow.model.entity.HNSWParams;
6import com.baidu.mochow.model.entity.VectorIndex;
7import com.baidu.mochow.model.enums.IndexType;
8import com.baidu.mochow.model.enums.MetricType;
9public class Main {
10 public static void main(String[] args) {
11 String account = "root";
12 String apiKey = "*********";
13 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
14 ClientConfiguration clientConfiguration = new ClientConfiguration();
15 clientConfiguration.setCredentials(new Credentials(account, apiKey));
16 clientConfiguration.setEndpoint(endpoint);
17 MochowClient mochowClient = new MochowClient(clientConfiguration);
18 String databaseName = "test";
19 String tableName = "test";
20
21 ModifyIndexRequest modifyIndexRequest = ModifyIndexRequest.builder()
22 .database(databaseName)
23 .table(tableName)
24 .index(VectorIndex.builder()
25 .indexName("vector_idx")
26 .indexType(IndexType.HNSW)
27 .fieldName("vector")
28 .params(new HNSWParams(32, 200))
29 .metricType(MetricType.L2)
30 .autoBuild(false).build()
31 )
32 .build();
33 mochowClient.modifyIndex(modifyIndexRequest);
34 }
35}
请求参数
参数 | 类型 | 是否必填 | 参数含义 |
---|---|---|---|
databaseName | String | 是 | 库名 |
tableName | String | 是 | 表名 |
index | IndexField | 是 | 索引详细信息 |
IndexField
拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明。为VectorIndex和SecondaryIndex的基类,正常情况下不使用
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
indexName | String | 是 | 索引的名字 |
field | String | 是 | 索引作用于的字段名称 |
indexType | IndexType | 是 | 插入的记录列表 |
metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
params | IndexParams | 否 | 向量索引构建参数 |
autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
state | IndexState | 否 | 索引状态,,建索引时不需要使用。取值如下: |