Index 操作
更新时间:2025-02-25
创建索引
功能介绍
为指定数据表和指定字段新建索引,当前仅支持新建向量索引。
请求示例
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 |
是 | 索引定义。 |
删除索引
功能介绍
删除指定索引。
请求示例
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 | 索引类型。当前支持的类型如下: SECONDARY:标量二级索引 HNSW: HNSW向量索引 HNSWPQ:HNSWPQ FLAT:FLAT向量索引(暴力检索) FILTERING:FILTERING索引 INVERTED:倒排索引 |
MetricType | String | 向量索引的距离度量算法。支持的类型如下: L2:欧几里得距离 IP:内积距离 COSINE:余弦距离 |
Params | VectorIndexParam | 向量构建索引所需参数。 1. m:表示每个节点在检索构图中可以连接多少个邻居节点。取值为[4, 128]; 2. efconstruction:搜索时,指定寻找节点邻居遍历的范围。数值越大构图效果越好,构图时间越长。取值为[8, 1024]。 1. coarseClusterCount:索引中粗聚类中心的个数; 2. fineClusterCount:每个粗聚类中心下细聚类中心个数。 1. m:表示每个节点在检索构图中可以连接多少个邻居节点。取值为[4, 128]; 2. efconstruction:搜索时,指定寻找节点邻居遍历的范围。数值越大构图效果越好,构图时间越长。取值为[8, 1024]; 3. NSQ:表示量化子空间个数,取值为[1, dim],并且要求NSQ | dim; 4. sampleRate:kmeans训练原始数据的抽样比率,取值为[0.0, 1.0],抽样总数 10000 + (rowCount - 10000)*sampleRate |
AutoBuild | Bool | 是否自动构建索引 |
AutoBuildPolicy | AutoBuildPolicy | 自动构建索引策略,当前支持如下策略: |
Field | String | 索引作用于的目标字段名称。 |
InvertedIndexFields | List |
倒排索引作用于的目标字段名称。 |
InvertedIndexFieldAttributes | List |
指定建立倒排索引的列是否需要分词(默认是会分词),参数顺序应与'fields'里列名一一对应。目前支持以下选项: |
FilterIndexFields | List |
Filtering索引作用于的目标字段名称。 |
State | IndexState | 索引状态,返回值如下: |