Index 操作
创建索引
功能介绍
为指定表和指定字段新建索引。Table.create_indexes() 支持一次创建多个向量索引、标量二级索引、FILTERING 索引、PERSISTENT_BITMAP 索引、PERSISTENT_AGGREGATED_BITMAP 索引或倒排索引。
请求示例
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4from pymochow.model.enum import IndexType, MetricType
5from pymochow.model.schema import (
6 AutoBuildPeriodical,
7 HNSWParams,
8 SecondaryIndex,
9 VectorIndex,
10)
11
12account = "root"
13api_key = "您的账户API密钥"
14endpoint = "您的实例访问端点" # 例如:"http://127.0.0.1:5287"
15
16config = Configuration(
17 credentials=BceCredentials(account, api_key),
18 endpoint=endpoint,
19)
20client = pymochow.MochowClient(config)
21db = client.database("db_test")
22table = db.table("book_vector")
23
24indexes = [
25 VectorIndex(
26 index_name="vector_idx",
27 index_type=IndexType.HNSW,
28 field="vector",
29 metric_type=MetricType.L2,
30 params=HNSWParams(m=32, efconstruction=200),
31 auto_build=True,
32 auto_build_index_policy=AutoBuildPeriodical(
33 5000, "2026-01-01 12:00:00"
34 ),
35 truncation_dimension=[0, 4],
36 ),
37 SecondaryIndex(index_name="book_name_idx", field="bookName"),
38]
39table.create_indexes(indexes)
40
41client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| indexes | List<IndexField> | 是 | 索引列表。支持 VectorIndex、SecondaryIndex、FilteringIndex、PersistentBitmapIndex、PersistentAggregatedBitmapIndex 和 InvertedIndex。 |
| config | Configuration | 否 | 本次请求使用的配置。 |
Index 参数
索引对象、字段类型和构建参数的完整说明请参见“Table 操作”的“创建表”章节。当前 SDK 的向量索引类型包括 FLAT、HNSW、HNSWPQ、HNSWSQ、HNSWRABITQ、PUCK、IVF、IVFSQ、IVFPQ、IVFRABITQ、DISKANN 和 SPARSE_OPTIMIZED_FLAT。
向量索引注意事项
- HNSWRABITQ 使用
IndexType.HNSWRABITQ和HNSWRABITQParams(m, efconstruction)。 - IVFSQ 使用
IVFSQParams(nlist, qtBits),qtBits支持 4、8、16。 VectorIndex.truncation_dimension是与params同级的顶层参数,格式为[begin, end);需满足0 <= begin < end <= dimension,有效维度为end - begin。truncation_dimension仅支持 FLOAT_VECTOR 字段和 HNSW、HNSWRABITQ、HNSWPQ、HNSWSQ、IVF、IVFPQ、IVFSQ;HNSWPQ、IVFPQ 还要求有效维度能被NSQ整除。
FILTERING 与持久化 BITMAP 索引示例
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4from pymochow.model.enum import IndexStructureType
5from pymochow.model.schema import (
6 FilteringIndex,
7 PersistentAggregatedBitmapIndex,
8 PersistentBitmapIndex,
9)
10
11account = "root"
12api_key = "您的账户API密钥"
13endpoint = "您的实例访问端点"
14
15client = pymochow.MochowClient(
16 Configuration(
17 credentials=BceCredentials(account, api_key),
18 endpoint=endpoint,
19 )
20)
21table = client.database("db_test").table("book_vector")
22table.create_indexes(
23 [
24 FilteringIndex.from_dict_list(
25 index_name="filtering_idx",
26 fields=[
27 {
28 "field": "status",
29 "indexStructureType": IndexStructureType.BITMAP,
30 },
31 {
32 "field": "publishTime",
33 "indexStructureType": IndexStructureType.AGGREGATED_BITMAP,
34 },
35 ],
36 ),
37 PersistentBitmapIndex(
38 index_name="status_persistent_bitmap_idx",
39 field="status",
40 ),
41 PersistentAggregatedBitmapIndex(
42 index_name="publish_time_persistent_aggregated_bitmap_idx",
43 field="publishTime",
44 fanout_bits=4,
45 max_depth=10,
46 ),
47 ]
48)
49
50client.close()
| 索引 | 参数与约束 |
|---|---|
| FILTERING DEFAULT / BITMAP | FILTERING 的内存结构;BITMAP 适用于值种类较少的列。 |
| FILTERING AGGREGATED_BITMAP | 内存结构,除等值过滤外还可加速 >、>=、<、<=;仅支持 INT8、UINT8、INT16、UINT16、INT32、UINT32、INT64、UINT64、FLOAT、DOUBLE、DATE、DATETIME、TIMESTAMP。 |
| PERSISTENT_BITMAP | 独立的持久化索引类型,不是 FILTERING 的结构选项,适用于等值过滤。 |
| PERSISTENT_AGGREGATED_BITMAP | 独立的持久化聚合索引类型,不是 FILTERING 的结构选项,支持等值和范围过滤。必须指定 field、fanout_bits、max_depth;后二者范围分别为 [1, 10]、[2, 30],且满足 fanout_bits * (max_depth - 1) <= 64。 |
创建倒排索引
倒排索引用于 BM25 全文检索和 Hybrid 混合检索。
1import time
2
3import pymochow
4from pymochow.auth.bce_credentials import BceCredentials
5from pymochow.configuration import Configuration
6from pymochow.model.enum import (
7 IndexState,
8 InvertedIndexAnalyzer,
9 InvertedIndexFieldAttribute,
10 InvertedIndexParseMode,
11 StopWordsMode,
12)
13from pymochow.model.schema import (
14 InvertedIndex,
15 InvertedIndexParams,
16 StopWordsParams,
17)
18
19account = "root"
20api_key = "您的账户API密钥"
21endpoint = "您的实例访问端点"
22
23client = pymochow.MochowClient(
24 Configuration(
25 credentials=BceCredentials(account, api_key),
26 endpoint=endpoint,
27 )
28)
29table = client.database("db_test").table("book_vector")
30index_name = "segment_inverted_idx"
31
32table.create_indexes(
33 [
34 InvertedIndex(
35 index_name=index_name,
36 fields=["segment"],
37 params=InvertedIndexParams(
38 analyzer=InvertedIndexAnalyzer.DEFAULT_ANALYZER,
39 parse_mode=InvertedIndexParseMode.COARSE_MODE,
40 case_sensitive=True,
41 stop_words=StopWordsParams(
42 mode=StopWordsMode.CUSTOM,
43 words=["呀", "啊", "哦"],
44 ),
45 ),
46 field_attributes=[InvertedIndexFieldAttribute.ANALYZED],
47 )
48 ]
49)
50
51# 倒排索引异步构建完成后才能进行 BM25/Hybrid 检索。
52while True:
53 index = table.describe_index(index_name)
54 if index.state == IndexState.NORMAL:
55 break
56 time.sleep(2)
57
58client.close()
InvertedIndex 参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| index_name | String | 是 | 索引名称。 |
| fields | List<String> | 是 | 目标字段。协议支持 TEXT、TEXT_GBK、TEXT_GB18030、ARRAY<TEXT> 和 JSON;当前 SDK 的属性枚举仅暴露文本字段属性。 |
| params | InvertedIndexParams | 是 | 分词器、分词模式、大小写和停用词配置。 |
| field_attributes | List<InvertedIndexFieldAttribute> | 否 | 与 fields 一一对应;支持 ANALYZED、NOT_ANALYZED。 |
InvertedIndexParams 参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| analyzer | InvertedIndexAnalyzer | 否 | ENGLISH_ANALYZER、CHINESE_ANALYZER 或 DEFAULT_ANALYZER。 |
| parse_mode | InvertedIndexParseMode | 否 | COARSE_MODE 或 FINE_MODE。 |
| case_sensitive | Boolean | 否 | 是否大小写敏感,默认值为 True。 |
| stop_words | StopWordsParams | 否 | 停用词配置。 |
StopWordsParams.mode 支持 DEFAULT、CUSTOM、NONE。CUSTOM 必须提供非空 words,每项为单个 term;未配置等价于 DEFAULT。停用词只能在建表或创建索引时设置,不能原地修改,检索时也不能临时覆盖。
倒排索引异步构建:
IndexState.BUILDING:不能用于 BM25/Hybrid,检索返回错误码 95(Index Building),并且索引不可删除。IndexState.NORMAL:构建完成,可以用于 BM25/Hybrid。
删除索引
功能介绍
删除指定索引。支持向量索引、SECONDARY、FILTERING、PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP 和倒排索引,也支持删除 2.4 之前创建的旧全文索引。BUILDING 状态的索引不能删除。
请求示例
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户API密钥"
7endpoint = "您的实例访问端点" # 例如:"http://127.0.0.1:5287"
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15db = client.database("db_test")
16table = db.table("book_vector")
17table.drop_index("vector_idx")
18
19client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| index_name | String | 是 | 指定索引的名称。 |
| config | Configuration | 否 | 本次请求使用的配置。 |
重建向量索引
功能介绍
重建指定索引,当前仅支持重建向量索引。
请求示例
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户API密钥"
7endpoint = "您的实例访问端点" # 例如:"http://127.0.0.1:5287"
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15db = client.database("db_test")
16table = db.table("book_vector")
17table.rebuild_index(index_name="vector_idx")
18
19client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| index_name | String | 是 | 向量索引的名称。 |
| config | Configuration | 否 | 本次请求使用的配置。 |
查询索引详情
功能介绍
查询指定索引的详情,返回与索引类型对应的 SDK 对象。
请求示例
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户API密钥"
7endpoint = "您的实例访问端点" # 例如:"http://127.0.0.1:5287"
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15db = client.database("db_test")
16table = db.table("book_vector")
17index = table.describe_index(index_name="vector_idx")
18print(index.index_name, index.index_type)
19
20client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| index_name | String | 是 | 指定索引的名称。 |
| config | Configuration | 否 | 本次请求使用的配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index | IndexField | 与索引类型对应的 SDK 索引对象。 |
Index 参数
| 索引类型 | 返回对象 | 当前 SDK 实际解析的参数 |
|---|---|---|
| 向量索引 | VectorIndex | index_name、index_type、field、metric_type、params、auto_build、auto_build_index_policy、state。 |
| SECONDARY | SecondaryIndex | index_name、index_type、field。 |
| FILTERING | FilteringIndex | index_name、index_type、字段定义;字段可通过 to_dict()["fields"] 查看。 |
| PERSISTENT_BITMAP | PersistentBitmapIndex | index_name、index_type、field。 |
| PERSISTENT_AGGREGATED_BITMAP | PersistentAggregatedBitmapIndex | index_name、index_type、field,以及服务端返回的 fanoutBits、maxDepth(可通过 to_dict() 查看)。 |
| INVERTED | InvertedIndex | index_name、index_type、字段、基础 analyzer / parse_mode 参数对象和 state。 |
VectorIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| index_type | IndexType | 索引类型。 |
| field | String | 索引作用于的字段名称。 |
| metric_type | MetricType | 向量距离度量类型:L2、IP 或 COSINE。 |
| params | Params | 向量索引构建参数。 |
| auto_build | Boolean | 是否启用自动构建。 |
| auto_build_index_policy | AutoBuildPolicy | 自动构建索引策略。 |
| state | IndexState | BUILDING 表示正在构建;NORMAL 表示构建完成并处于正常状态。 |
SecondaryIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| field | String | 索引作用于的字段名称。 |
FilteringIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| fields | List<Dict> | 索引字段及其 indexStructureType;通过 to_dict()["fields"] 查看。 |
PersistentBitmapIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| field | String | 索引作用于的字段名称。 |
PersistentAggregatedBitmapIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| field | String | 索引作用于的字段名称。 |
| fanoutBits | Int | 服务端返回的扇出位数;通过 to_dict() 查看。 |
| maxDepth | Int | 服务端返回的最大深度;通过 to_dict() 查看。 |
InvertedIndex 参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| index_name | String | 索引名称。 |
| fields | List<String> | 索引作用于的字段名称;包含在对象序列化结果中。 |
| params | InvertedIndexParams | 当前 SDK 仅从返回值解析基础 analyzer 和 parse_mode。 |
| state | IndexState | BUILDING 或 NORMAL。 |
当前 Python SDK 的 describe_index() 不会恢复倒排索引的 stop_words、case_sensitive 和 field_attributes,请勿依赖该方法读取这些创建参数。它在构造向量索引对象时也不会恢复 truncation_dimension;该参数可用于创建索引,但不能通过当前返回对象读取。
修改索引
功能介绍
修改向量索引信息,目前支持修改 auto_build 属性和自动构建策略。
请求示例
1import time
2
3import pymochow
4from pymochow.auth.bce_credentials import BceCredentials
5from pymochow.configuration import Configuration
6from pymochow.model.enum import (
7 FieldType,
8 IndexType,
9 MetricType,
10 TableState,
11)
12from pymochow.model.schema import (
13 AutoBuildTiming,
14 Field,
15 HNSWParams,
16 Schema,
17 VectorIndex,
18)
19from pymochow.model.table import Partition
20
21account = "root"
22api_key = "您的账户API密钥"
23endpoint = "您的实例访问端点" # 例如:"http://127.0.0.1:5287"
24
25config = Configuration(
26 credentials=BceCredentials(account, api_key),
27 endpoint=endpoint,
28)
29client = pymochow.MochowClient(config)
30
31database_name = "book"
32table_name = "book_segments"
33db = client.create_database(database_name)
34
35fields = [
36 Field(
37 "id",
38 FieldType.STRING,
39 primary_key=True,
40 partition_key=True,
41 auto_increment=False,
42 not_null=True,
43 ),
44 Field("bookName", FieldType.STRING, not_null=True),
45 Field("vector", FieldType.FLOAT_VECTOR, not_null=True, dimension=3),
46]
47db.create_table(
48 table_name=table_name,
49 replication=2,
50 partition=Partition(partition_num=3),
51 schema=Schema(fields=fields, indexes=[]),
52)
53
54while True:
55 time.sleep(2)
56 table = db.describe_table(table_name)
57 if table.state == TableState.NORMAL:
58 break
59
60table = db.table(table_name)
61indexes = [
62 VectorIndex(
63 index_name="vector_idx",
64 index_type=IndexType.HNSW,
65 field="vector",
66 metric_type=MetricType.L2,
67 params=HNSWParams(m=32, efconstruction=200),
68 )
69]
70table.create_indexes(indexes)
71table.modify_index(
72 index_name="vector_idx",
73 auto_build=True,
74 auto_build_index_policy=AutoBuildTiming("2026-09-11 23:07:00"),
75)
76
77client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| index_name | String | 是 | 向量索引名称。 |
| auto_build | Boolean | 是 | 是否自动构建索引。 |
| auto_build_index_policy | AutoBuildPolicy | 否 | 自动构建策略。支持 AutoBuildTiming、AutoBuildPeriodical 和 AutoBuildRowCountIncrement;auto_build=True 时发送。未传时使用 SDK 默认的 24 小时周期策略。 |
| config | Configuration | 否 | 本次请求使用的配置。 |
评价此篇文章
