Index 操作
创建索引
功能介绍
为指定表和指定字段新建索引,当前支持新建向量索引、FILTERING索引、二级索引、PERSISTENT_BITMAP索引、PERSISTENT_AGGREGATED_BITMAP索引与倒排索引(全文索引),支持一次请求同时创建多个索引。
请求示例
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.CreateIndexRequest;
5import com.baidu.mochow.model.entity.HNSWParams;
6import com.baidu.mochow.model.entity.SecondaryIndex;
7import com.baidu.mochow.model.entity.VectorIndex;
8import com.baidu.mochow.model.enums.IndexType;
9import com.baidu.mochow.model.enums.MetricType;
10public class Main {
11 public static void main(String[] args) {
12 String account = "root";
13 String apiKey = "*********";
14 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
15 ClientConfiguration clientConfiguration = new ClientConfiguration();
16 clientConfiguration.setCredentials(new Credentials(account, apiKey));
17 clientConfiguration.setEndpoint(endpoint);
18 MochowClient mochowClient = new MochowClient(clientConfiguration);
19 String databaseName = "test";
20 String tableName = "test";
21
22 CreateIndexRequest createIndexRequest = CreateIndexRequest.builder()
23 .database(databaseName)
24 .table(tableName)
25 .addIndex(new SecondaryIndex("book_name_idx", "bookName"))
26 .addIndex(VectorIndex.builder()
27 .indexName("vector_idx")
28 .indexType(IndexType.HNSW)
29 .fieldName("vector")
30 .params(new HNSWParams(32, 200))
31 .metricType(MetricType.L2)
32 .autoBuild(false).build())
33 .build();
34 mochowClient.createIndex(createIndexRequest);
35 }
36}
注意事项
- 创建向量索引时可以指定
truncationDimension,仅使用原始向量的一段维度子集构建索引,取值格式与限制请参见建表操作的IndexField说明;该成员与params同级,需要使用VectorIndex的对应构造函数传入,VectorIndex.builder()当前不包含该参数。 - 创建FILTERING索引时,
FilteringIndexField的indexStructureType支持DEFAULT、BITMAP和AGGREGATED_BITMAP。 - 创建PERSISTENT_AGGREGATED_BITMAP索引时必须填写
fanoutBits和maxDepth,取值范围分别为[1, 10]和[2, 30],且需要满足fanoutBits * (maxDepth - 1) <= 64。 - 创建倒排索引(全文索引)时,
fields支持TEXT、TEXT_GBK、TEXT_GB18030、ARRAY<TEXT>、JSON类型字段;InvertedIndexParams中的stopWords只能在创建索引时指定,创建后不支持原地修改,检索请求也不能临时覆盖。 - 倒排索引创建后会异步构建:构建完成前查询索引详情返回的
state为BUILDING,此时发起BM25/Hybrid检索会返回错误码95(Index Building);state变为NORMAL后方可用于检索。
全文索引创建示例
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.DescribeIndexResponse;
8import com.baidu.mochow.model.entity.InvertedIndex;
9import com.baidu.mochow.model.entity.InvertedIndexParams;
10import com.baidu.mochow.model.entity.StopWords;
11import com.baidu.mochow.model.enums.IndexState;
12import com.baidu.mochow.model.enums.InvertedIndexAnalyzer;
13import com.baidu.mochow.model.enums.InvertedIndexFieldAttribute;
14import com.baidu.mochow.model.enums.InvertedIndexParseMode;
15public class Main {
16 public static void main(String[] args) throws InterruptedException {
17 String account = "root";
18 String apiKey = "*********";
19 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
20 ClientConfiguration clientConfiguration = new ClientConfiguration();
21 clientConfiguration.setCredentials(new Credentials(account, apiKey));
22 clientConfiguration.setEndpoint(endpoint);
23 MochowClient mochowClient = new MochowClient(clientConfiguration);
24 String databaseName = "test";
25 String tableName = "test";
26 String indexName = "segment_inverted_idx";
27
28 CreateIndexRequest createIndexRequest = CreateIndexRequest.builder()
29 .database(databaseName)
30 .table(tableName)
31 .addIndex(new InvertedIndex(
32 indexName,
33 new String[]{"segment"},
34 new InvertedIndexParams(
35 InvertedIndexAnalyzer.DEFAULT_ANALYZER,
36 InvertedIndexParseMode.COARSE_MODE,
37 true,
38 new StopWords("CUSTOM", Arrays.asList("呀", "啊", "哦"))),
39 new InvertedIndexFieldAttribute[]{InvertedIndexFieldAttribute.ANALYZED}))
40 .build();
41 mochowClient.createIndex(createIndexRequest);
42
43 // 倒排索引异步构建,等待 state 变为 NORMAL 后才能发起 BM25/Hybrid 检索
44 while (true) {
45 Thread.sleep(2000);
46 DescribeIndexResponse describeIndexResponse =
47 mochowClient.describeIndex(databaseName, tableName, indexName);
48 if (describeIndexResponse.getIndex().getState() == IndexState.NORMAL) {
49 break;
50 }
51 }
52 }
53}
其他2.4新增索引类型的创建示例
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.CreateIndexRequest;
5import com.baidu.mochow.model.entity.FilteringIndex;
6import com.baidu.mochow.model.entity.FilteringIndexField;
7import com.baidu.mochow.model.entity.PersistentAggregatedBitmapIndex;
8import com.baidu.mochow.model.entity.PersistentBitmapIndex;
9import com.baidu.mochow.model.enums.IndexStructureType;
10public class Main {
11 public static void main(String[] args) {
12 String account = "root";
13 String apiKey = "*********";
14 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
15 ClientConfiguration clientConfiguration = new ClientConfiguration();
16 clientConfiguration.setCredentials(new Credentials(account, apiKey));
17 clientConfiguration.setEndpoint(endpoint);
18 MochowClient mochowClient = new MochowClient(clientConfiguration);
19 String databaseName = "test";
20 String tableName = "test";
21
22 CreateIndexRequest createIndexRequest = CreateIndexRequest.builder()
23 .database(databaseName)
24 .table(tableName)
25 // AGGREGATED_BITMAP 结构除等值查询外还可加速 >、>=、<、<= 范围查询
26 .addIndex(FilteringIndex.builder()
27 .name("filtering_idx")
28 .addField(new FilteringIndexField("status", IndexStructureType.BITMAP))
29 .addField(new FilteringIndexField("publishTime", IndexStructureType.AGGREGATED_BITMAP))
30 .build())
31 .addIndex(new PersistentBitmapIndex("status_bitmap_idx", "status"))
32 .addIndex(new PersistentAggregatedBitmapIndex(
33 "publish_time_bitmap_idx", "publishTime", 4, 10))
34 .build();
35 mochowClient.createIndex(createIndexRequest);
36 }
37}
相关类说明
CreateIndexRequest
创建索引请求,用于向量数据库创建索引,拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明
1import com.baidu.mochow.model.CreateIndexRequest; //导入包
| 成员名 | 类型 | 是否必填 | 成员含义 |
|---|---|---|---|
| database | String | 是 | 建索引的库名 |
| table | String | 是 | 建索引的表名 |
| indexes | List<IndexField> | 是 | 待创建的索引列表,builder中通过addIndex逐个追加 |
IndexField
拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明。为VectorIndex、SecondaryIndex、FilteringIndex、InvertedIndex、PersistentBitmapIndex、PersistentAggregatedBitmapIndex的基类,正常情况下不使用
| 成员名 | 类型 | 是否必填 | 成员含义 |
|---|---|---|---|
| indexName | String | 是 | 索引的名字 |
| field | String | 是 | 索引作用于的字段名称 |
| indexType | IndexType | 是 | 索引类型。取值如下: 非向量索引类型: 向量索引类型: |
| metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
| params | IndexParams | 否 | 向量索引或倒排索引的构建参数 |
| truncationDimension | List<Integer> | 否 | 构建向量索引时使用的原始向量维度子集,格式为[begin, end),需满足0 <= begin < end <= dimension,实际构建维度为end - begin,不填写时使用完整向量。该成员与params同级,不在params内部。仅支持FLOAT_VECTOR字段,且仅支持HNSW、HNSWRABITQ、HNSWPQ、HNSWSQ、IVF、IVFPQ、IVFSQ索引类型;HNSWPQ、IVFPQ还要求end - begin能被params中的NSQ整除。 |
| autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
| autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
| state | IndexState | 否 | 索引状态,建索引时不需要使用。取值如下: |
SecondaryIndex
使用构造函数传入索引名称和对应的字段名称即可
1SecondaryIndex(String indexName, String fieldName)
VectorIndex
使用构造函数传入对应参数即可,对应参数也拥有对应的builder构造函数
1public VectorIndex(String indexName, String fieldName, IndexType indexType, IndexState state,MetricType metricType, IndexParams params, Boolean autoBuild, AutoBuildPolicy autoBuildPolicy)
如需指定truncationDimension,使用如下构造函数(builder方法当前不包含truncationDimension):
1public VectorIndex(String indexName, String fieldName, IndexType indexType, MetricType metricType, IndexParams params, List<Integer> truncationDimension)
FilteringIndex、PersistentBitmapIndex、PersistentAggregatedBitmapIndex、InvertedIndex
参数结构与建表操作中的同名类一致,详情请参见建表操作的相关类说明。
删除索引
功能介绍
删除指定索引,支持删除向量索引、标量索引(含PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP)、FILTERING索引与倒排索引(含2.4之前版本创建的全文索引)。当前不支持删除构建中(state为BUILDING)的向量索引与倒排索引。
请求示例
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 | 是 | 索引名 |
重建向量索引
功能介绍
重建指定索引,当前仅支持重建向量索引。
请求示例
请求示例
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 | 是 | 索引名 |
查询索引详情
功能介绍
查询指定索引的详情。
请求示例
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、FilteringIndex、InvertedIndex、PersistentBitmapIndex、PersistentAggregatedBitmapIndex的基类,正常情况下不使用
| 成员名 | 类型 | 是否必填 | 成员含义 |
|---|---|---|---|
| indexName | String | 是 | 索引的名字 |
| field | String | 是 | 索引作用于的字段名称 |
| indexType | IndexType | 是 | 索引类型。取值如下: 非向量索引类型: 向量索引类型: |
| metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
| params | IndexParams | 否 | 向量索引或倒排索引的构建参数 |
| truncationDimension | List<Integer> | 否 | 构建向量索引时使用的原始向量维度子集,格式为[begin, end),需满足0 <= begin < end <= dimension,实际构建维度为end - begin,不填写时使用完整向量。该成员与params同级,不在params内部。仅支持FLOAT_VECTOR字段,且仅支持HNSW、HNSWRABITQ、HNSWPQ、HNSWSQ、IVF、IVFPQ、IVFSQ索引类型;HNSWPQ、IVFPQ还要求end - begin能被params中的NSQ整除。 |
| autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
| autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
| state | IndexState | 否 | 索引状态,建索引时不需要使用。取值如下: |
不同索引类型的典型返回成员
| 索引类型 | 主要返回成员 |
|---|---|
| 向量索引 | field、metricType、params、autoBuild、autoBuildPolicy、state;如创建时配置了truncationDimension,还会返回truncationDimension |
| 二级索引、PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP | field |
| FILTERING索引 | fields,其中每个元素包含field和indexStructureType |
| 倒排索引(全文索引) | fields、fieldAttributes,以及params中的analyzer、parseMode、analyzerCaseSensitive、stopWords |
倒排索引的params.stopWords始终返回mode。CUSTOM模式同时返回保持创建顺序的words;DEFAULT和NONE模式不返回words;未显式配置停用词的已有索引返回mode为DEFAULT。
修改索引
功能介绍
修改向量索引信息,目前只支持修改autoBuild属性。
请求示例
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、FilteringIndex、InvertedIndex、PersistentBitmapIndex、PersistentAggregatedBitmapIndex的基类,正常情况下不使用
| 成员名 | 类型 | 是否必填 | 成员含义 |
|---|---|---|---|
| indexName | String | 是 | 索引的名字 |
| field | String | 是 | 索引作用于的字段名称 |
| indexType | IndexType | 是 | 索引类型。取值如下: 非向量索引类型: 向量索引类型: |
| metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
| params | IndexParams | 否 | 向量索引或倒排索引的构建参数 |
| truncationDimension | List<Integer> | 否 | 构建向量索引时使用的原始向量维度子集,格式为[begin, end),需满足0 <= begin < end <= dimension,实际构建维度为end - begin,不填写时使用完整向量。该成员与params同级,不在params内部。仅支持FLOAT_VECTOR字段,且仅支持HNSW、HNSWRABITQ、HNSWPQ、HNSWSQ、IVF、IVFPQ、IVFSQ索引类型;HNSWPQ、IVFPQ还要求end - begin能被params中的NSQ整除。 |
| autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
| autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
| state | IndexState | 否 | 索引状态,建索引时不需要使用。取值如下: |
评价此篇文章
