Index 操作
更新时间:2026-09-01
创建索引
功能介绍
为指定数据表和指定字段新建索引,当前支持新建向量索引、FILTERING索引、二级索引(SECONDARY)、PERSISTENT_BITMAP索引、PERSISTENT_AGGREGATED_BITMAP索引与倒排索引(全文索引,INVERTED),支持一次请求同时创建多个索引。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow/api"
7 "github.com/baidu/mochow-sdk-go/mochow"
8)
9
10func main() {
11 clientConfig := &mochow.ClientConfiguration{
12 Account: "root",
13 APIKey: "您的账户API密钥",
14 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
15 }
16
17 // create mochow client
18 client, err := mochow.NewClientWithConfig(clientConfig)
19 if err != nil {
20 log.Fatalf("Fail to init mochow client due to error:%v", err)
21 return
22 }
23
24 createIndexArgs := &api.CreateIndexArgs{
25 Database: "db_test",
26 Table: "table_test",
27 Indexes: []api.IndexSchema{
28 {
29 IndexName: "vector_idx",
30 Field: "vector",
31 IndexType: api.HNSW,
32 MetricType: api.L2,
33 Params: api.VectorIndexParams{
34 "M": 16,
35 "efConstruction": 200,
36 },
37 },
38 },
39 }
40
41 if err := client.CreateIndex(createIndexArgs); err != nil {
42 log.Fatalf("Fail to create index due to error: %v", err)
43 return
44 }
45}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Database | String | 是 | 库名。 |
| Table | String | 是 | 表名。 |
| Indexes | List |
是 | 索引定义。IndexSchema的完整字段说明请参见建表操作的IndexSchema。IndexType支持向量索引类型(api.HNSW、api.HNSWPQ、api.HNSWSQ、api.HNSWRABITQ、api.FLAT、api.PUCK、api.IVF、api.IVFSQ、api.IVFPQ、api.IVFRABITQ、api.DISKANN、api.SPARSE)以及api.FilteringIndex、api.SecondaryIndex、api.PersistentBitmapIndex、api.PersistentAggregatedBitmapIndex、api.InvertedIndex。 |
注意事项
- 创建FILTERING索引时,
FilterIndexFields中的IndexStructureType支持DEFAULT、BITMAP和AGGREGATED_BITMAP;其中AGGREGATED_BITMAP除等值查询外还可加速>、>=、<、<=范围查询,常见场景为timestamp > {某个时间}。 - 创建PERSISTENT_AGGREGATED_BITMAP索引时必须填写
Field、FanoutBits和MaxDepth,取值范围分别为[1, 10]和[2, 30],且需要满足FanoutBits * (MaxDepth - 1) <= 64。 - 创建倒排索引(全文索引)时,
Params中的StopWords只能在创建索引时指定,创建后不支持原地修改,检索请求也不能临时覆盖;未配置时等价于DEFAULT模式。 - 倒排索引创建后会异步构建:构建完成前查询索引详情返回的
State为BUILDING,此时发起BM25/Hybrid检索会返回错误码95(Index Building);State变为NORMAL后方可用于检索。
全文索引创建示例
Go
1package main
2
3import (
4 "log"
5 "time"
6
7 "github.com/baidu/mochow-sdk-go/mochow"
8 "github.com/baidu/mochow-sdk-go/mochow/api"
9)
10
11func main() {
12 clientConfig := &mochow.ClientConfiguration{
13 Account: "root",
14 APIKey: "您的账户API密钥",
15 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
16 }
17
18 // create mochow client
19 client, err := mochow.NewClientWithConfig(clientConfig)
20 if err != nil {
21 log.Fatalf("Fail to init mochow client due to error:%v", err)
22 return
23 }
24
25 createIndexArgs := &api.CreateIndexArgs{
26 Database: "db_test",
27 Table: "table_test",
28 Indexes: []api.IndexSchema{
29 {
30 IndexName: "segment_inverted_idx",
31 IndexType: api.InvertedIndex,
32 InvertedIndexFields: []string{"segment"},
33 InvertedIndexFieldAttributes: []api.InvertedIndexFieldAttribute{api.Analyzed},
34 Params: api.NewInvertedIndexParams().
35 Analyzer(api.DefaultAnalyzer).
36 ParseMode(api.CoarseMode).
37 AnalyzerCaseSensitive(true).
38 StopWords(
39 api.StopWordsParams{}.New(api.CustomStopWords).
40 Words([]string{"呀", "啊", "哦"}),
41 ),
42 },
43 },
44 }
45
46 if err := client.CreateIndex(createIndexArgs); err != nil {
47 log.Fatalf("Fail to create inverted index due to error: %v", err)
48 return
49 }
50
51 // 倒排索引异步构建,等待 State 变为 NORMAL 后才能发起 BM25/Hybrid 检索
52 for {
53 descIndexResult, err := client.DescIndex("db_test", "table_test", "segment_inverted_idx")
54 if err != nil {
55 log.Fatalf("Fail to describe inverted index due to error: %v", err)
56 return
57 }
58 if descIndexResult.Index.State == api.IndexStateNormal {
59 log.Println("Inverted index build finished")
60 break
61 }
62 if descIndexResult.Index.State == api.IndexStateInvalid {
63 log.Fatalf("Inverted index entered invalid state")
64 return
65 }
66 time.Sleep(time.Second)
67 }
68}
持久化BITMAP索引创建示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7 "github.com/baidu/mochow-sdk-go/mochow/api"
8)
9
10func main() {
11 clientConfig := &mochow.ClientConfiguration{
12 Account: "root",
13 APIKey: "您的账户API密钥",
14 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
15 }
16
17 // create mochow client
18 client, err := mochow.NewClientWithConfig(clientConfig)
19 if err != nil {
20 log.Fatalf("Fail to init mochow client due to error:%v", err)
21 return
22 }
23
24 createIndexArgs := &api.CreateIndexArgs{
25 Database: "db_test",
26 Table: "table_test",
27 Indexes: []api.IndexSchema{
28 {
29 // 等值过滤加速,索引数据基于磁盘存储
30 IndexName: "page_persistent_bitmap_idx",
31 IndexType: api.PersistentBitmapIndex,
32 Field: "page",
33 },
34 {
35 // 等值与范围过滤加速,FanoutBits与MaxDepth为必填参数
36 IndexName: "page_persistent_aggregated_bitmap_idx",
37 IndexType: api.PersistentAggregatedBitmapIndex,
38 Field: "page",
39 FanoutBits: 2,
40 MaxDepth: 10,
41 },
42 },
43 }
44
45 if err := client.CreateIndex(createIndexArgs); err != nil {
46 log.Fatalf("Fail to create persistent bitmap index due to error: %v", err)
47 return
48 }
49}
删除索引
功能介绍
删除指定索引,支持删除向量索引、标量索引(SECONDARY、FILTERING、PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP)与倒排索引(含2.4之前版本创建的旧版全文索引)。当前不支持删除构建中(State为BUILDING)的索引。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 if err := client.DropIndex("db_test", "table_test", "vector_idx"); err != nil {
24 log.Fatalf("Fail to drop index due to error: %v", err)
25 return
26 }
27}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Database | String | 是 | 库名。 |
| Table | String | 是 | 表名。 |
| IndexName | String | 是 | 索引名称。 |
重建索引
功能介绍
重建指定索引,当前仅支持重建向量索引。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 if err := client.RebuildIndex("db_test", "table_test", "vector_idx"); err != nil {
24 log.Fatalf("Fail to drop index due to error: %v", err)
25 return
26 }
27}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Database | String | 是 | 库名。 |
| Table | String | 是 | 表名。 |
| IndexName | String | 是 | 索引名称。 |
查询索引详情
功能介绍
查询指定索引的详情。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 descIndexResult, err := client.DescIndex("db_test", "table_test", "vector_idx")
24 if err != nil {
25 log.Fatalf("Fail to drop index due to error: %v", err)
26 return
27 }
28
29 log.Printf("describe index response: %v", descIndexResult)
30}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Database | String | 是 | 库名。 |
| Table | String | 是 | 表名。 |
| IndexName | String | 是 | 索引名称。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| Index | IndexSchema | Index对象。 |
IndexSchema
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| IndexName | String | 索引名称,要求表内唯一。 索引命名要求如下: 仅支持大小写字母、数字以及下划线(_),必须以字母开头; 长度限制为1~255。 |
| IndexType | String | 索引类型。当前支持的类型如下: 非向量索引类型: 向量索引类型: |
| MetricType | String | 向量索引的距离度量算法。支持的类型如下: L2:欧几里得距离 IP:内积距离 COSINE:余弦距离 |
| Params | IndexParams | 索引构建参数。向量索引的构建参数取值范围请参见建表操作的IndexSchema;倒排索引返回analyzer、parseMode、analyzerCaseSensitive、stopWords等参数。 |
| AutoBuild | Bool | 是否自动构建索引 |
| AutoBuildPolicy | AutoBuildPolicy | 自动构建索引策略,当前支持如下策略: |
| Field | String | 索引作用于的目标字段名称。向量索引、SECONDARY、PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP索引返回该字段。 |
| FanoutBits | Uint32 | 聚合BITMAP树每层用于分叉的位数,取值范围为[1, 10]。仅PERSISTENT_AGGREGATED_BITMAP索引返回。 |
| MaxDepth | Uint32 | 聚合BITMAP树的最大深度,取值范围为[2, 30]。仅PERSISTENT_AGGREGATED_BITMAP索引返回。 |
| InvertedIndexFields | List |
倒排索引作用于的目标字段名称。 |
| InvertedIndexFieldAttributes | List |
指定建立倒排索引的列是否需要分词(默认是会分词),参数顺序应与'fields'里列名一一对应。目前支持以下选项: |
| FilterIndexFields | List |
Filtering索引作用于的目标字段名称。FILTERING、PERSISTENT_BITMAP、PERSISTENT_AGGREGATED_BITMAP索引返回该字段。 |
| State | IndexState | 索引状态,返回值如下: |
评价此篇文章
