表空间操作

http://${VEARCH_URL}代表master服务, $db_name是创建的库名, $space_name是创建的表空间名

创建表空间

1. curl -XPOST -H "content-type: application/json" -d'
2. {
3. "name": "space1",
4. "partition_num": 1,
5. "replica_num": 3,
6. "fields": [
7. {
8. "name": "field_string",
9. "type": "string"
10. },
11. {
12. "name": "field_int",
13. "type": "integer"
14. },
15. {
16. "name": "field_float",
17. "type": "float",
18. "index": {
19. "name": "field_float",
20. "type": "SCALAR",
21. },
22. },
23. {
24. "name": "field_string_array",
25. "type": "stringArray",
26. "index": {
27. "name": "field_string_array",
28. "type": "SCALAR",
29. },
30. },
31. {
32. "name": "field_int_index",
33. "type": "integer",
34. "index": {
35. "name": "field_int_index",
36. "type": "SCALAR",
37. },
38. },
39. {
40. "name": "field_vector",
41. "type": "vector",
42. "dimension": 128,
43. "index": {
44. "name": "gamma",
45. "type": "IVFPQ",
46. "params": {
47. "metric_type": "InnerProduct",
48. "ncentroids": 2048,
49. "nlinks": 32,
50. "efConstruction": 40,
51. },
52. },
53. }
54. ]
55. }
56. ' http://${VEARCH_URL}/dbs/$db_name/spaces

参数说明:

字段标识 字段含义 类型 是否必填 备注
name 空间名称 string 是
partition_num 分片数量 int 是 单节点数据承载有效, 多个分片提升集群数据承载量
replica_num 副本数量 int 是 通常设置为3来实现高可用
fields 空间配置 json 是 定义表字段及类型,是否创建索引

1、name 不能为空, 不能以数字或下划线开头, 尽量不使用特殊字符等。

2、partition_num 指定表空间数据分片数量, 不同的分片可分布在不同的机器, 来避免单台机器的资源限制。

3、replica_num 副本数量, 建议设置成3, 表示每个分片数据有两个备份, 保证数据高可用。

index配置:

字段标识 字段含义 类型 是否必填 备注
name 索引名称 string 是
type 索引类型 string 是
params 索引参数配置 json 否

1、index type 索引类型, 目前支持二大类共七种类型, 标量索引: SCALAR; 向量索引: IVFPQ, HNSW, GPU, IVFFLAT, BINARYIVF, FLAT, 详细可看链接 <https://github.com/vearch/vearch/wiki/Vearch%E7%B4%A2%E5%BC%95%E4%BB%8B%E7%BB%8D%E5%92%8C%E5%8F%82%E6%95%B0%E9%80%89%E6%8B%A9> 。

标量索引只需设置name和type即可。

不同的向量索引类型需要的参数配置及默认值如下:

IVFPQ:

IVFPQ可以与HNSW和OPQ组合使用。 如果要使用HNSW, 建议将ncentroids设置为较大的值。而在组合使用OPQ时, 训练占用的内存为 2 * training_threshold * dimension * sizeof(float), 因此对于HNSW和OPQ的组合使用, 训练将占用更多的内存并花费较长时间, 故要特别注意training_threshold的设置, 防止使用的太多内存。

training_threshold: 对于IVFPQ, 在建立索引之前需要训练, 因此需要将training_threshold设置为合适的值, training_threshold可以是 ncentroids * 39 到 ncentroids * 256 之间的值。

如何组合使用HNSW和OPQ由params控制。如果同时设置HNSW和OPQ, 则将使用OPQ + IVF + HNSW + PQ, 建议将OPQ的nsubvector设置为与PQ的nsubvector相同。如果只想使用IVF + HNSW + PQ, 则只需要设置HNSW。如果您只想使用IVFPQ, 则无需在params中设置HNSW或OPQ。

1. "type": "IVFPQ",
2. "params": {
3. "metric_type": "InnerProduct",
4. "ncentroids": 2048,
5. "nsubvector": 64
6. }

您可以这样设置hnsw或opq:

1. "type": "IVFPQ",
2. "params": {
3. "metric_type": "InnerProduct",
4. "ncentroids": 65536,
5. "nsubvector": 64,
6. "hnsw" : {
7. "nlinks": 32,
8. "efConstruction": 200,
9. "efSearch": 64
10. }
11. }

HNSW:

字段标识 字段含义 类型 是否必填 备注
metric_type 计算方式 string 是 L2或者InnerProduct
nlinks 节点邻居数量 int 是 默认32
efConstruction 构图时寻找节点邻居过程中在图中遍历的深度 int 是 默认40
efSearch 检索时寻找节点邻居过程中在图中遍历的深度 int 否 默认40
1. "type": "HNSW",
2. "params": {
3. "metric_type": "InnerProduct",
4. "nlinks": 32,
5. "efConstruction": 100
6. }

8. 注意: 1、向量存储只支持MemoryOnly

GPU(针对GPU编译版本):

字段标识 字段含义 类型 是否必填 备注
metric_type 计算方式 string 是 L2或者InnerProduct
ncentroids 聚类中心数量 int 是 默认2048
nsubvector PQ拆分子向量大小 int 否 默认64
training_threshold 训练的数据量 int 否 默认training_threshold * 39,是每个分片训练需要的数据量, 不是space表的数据量
nprobe 检索时查找的聚类中心数量 int 否 默认80
1. "type": "GPU",
2. "params": {
3. "metric_type": "InnerProduct",
4. "ncentroids": 2048,
5. "nsubvector": 64
6. }

SCANN(针对SCANN编译版本):

字段标识 字段含义 类型 是否必填 备注
metric_type 计算方式 string 是 L2或者InnerProduct
ncentroids 聚类中心数量 int 是 默认2048
nsubvector PQ拆分子向量大小 int 是 默认128, 量化为4bit, 建议使用ivfpq模型nsubvector的2倍
thread_num 线程池线程数 int 否 可以不使用, 如果使用建议为cpu核数
training_threshold 训练的数据量 int 否 默认training_threshold * 39,是每个分片训练需要的数据量, 不是space表的数据量
1. "type": "VEARCH",
2. "params": {
3. "metric_type": "InnerProduct",
4. "ncentroids": 2048,
5. "nsubvector": 64,
6. "thread_num": 8
7. }

9. 注意: 1、目前scann模型, 索引不支持dump/load; 不支持update。

IVFFLAT:

字段标识 字段含义 类型 是否必填 备注
metric_type 计算方式 string 是 L2或者InnerProduct
ncentroids 聚类中心数量 int 是 默认2048
training_threshold 训练的数据量 int 否 默认training_threshold * 39,是每个分片训练需要的数据量, 不是space表的数据量
nprobe 检索时查找的聚类中心数量 int 否 默认80
1. "type": "IVFFLAT",
2. "params": {
3. "metric_type": "InnerProduct",
4. "ncentroids": 2048
5. }

7. 注意: 1、向量存储方式只支持RocksDB

BINARYIVF:

字段标识 字段含义 类型 是否必填 备注
ncentroids 聚类中心数量 int 是 默认2048
training_threshold 训练的数据量 int 否 默认training_threshold * 39,是每个分片训练需要的数据量, 不是space表的数据量
nprobe 检索时查找的聚类中心数量 int 否 默认80
1. "type": "BINARYIVF",
2. "params": {
3. "ncentroids": 2048
4. }

6. 注意: 1、向量长度是8的倍数

FLAT:

字段标识 字段含义 类型 是否必填 备注
metric_type 计算方式 string 是 L2或者InnerProduct
1. "type": "FLAT",
2. "params": {
3. "metric_type": "InnerProduct"
4. }

6. 注意: 1、向量存储方式只支持MemoryOnly

fields配置:

1、表空间结构定义字段支持的类型(即type的值)有7种: string(keyword), stringArray, integer, long, float, double, vector。

2、string类型或者stringArray字段支持index属性, index定义是否创建索引, 创建索引后支持term过滤。

3、integer, long, float, double类型的字段支持index属性, index设为true创建索引后支持数值范围过滤查询(range)。

4、vector 类型字段为特征字段, 一个表空间中支持多个特征字段, vector类型的字段支持的属性如下:

字段标识 字段含义 类型 是否必填 备注
dimension 特征维数 int 是
format 归一化处理 string 否 设置为normalization对添加的特征向量归一化处理
store_type 特征存储类型 string 否 支持MemoryOnly、RocksDB, 不同索引默认值不一样
store_param 存储参数设置 json 否 针对不同store_type的存储参数
model_id 特征插件模型 string 否 使用特征插件服务时指定

5、dimension 定义type是vector的字段, 指定特征维数大小。

6、store_type 特征向量存储类型, 有以下几个选项:

“MemoryOnly”: 原始向量都存储在内存中, 存储数量的多少受内存限制, 适用于数据量不大(千万级), 对性能要求高的场景

“RocksDB”: 原始向量存储在RockDB(磁盘)中, 存储数量受磁盘大小限制, 适用单机数据量巨大(亿级以上), 对性能要求不高的场景

7、store_param 针对不同store_type的存储参数, 其包含以下两个子参数。

cache_size: 数值类型, 单位是M bytes, 默认1024。store_type=”RocksDB”时, 表示RocksDB的读缓冲大小, 值越大读向量的性能越好, 一般设置1024、2048、4096和6144即可; store_type=”MemoryOnly”, cache_size不生效。

标量索引

标量索引提供对标量数据的过滤功能, 开启方式参考“fields配置”中的第2条和第3条, 检索方式参考“查询”中的“filter json结构说明”

查看表空间

1. curl -XGET http://${VEARCH_URL}/dbs/$db_name/spaces/$space_name

返回数据详细格式:

字段标识 字段含义 类型 是否一定返回 备注
code 返回码 int 是
msg 返回信息 string 否
data 返回数据 json 否

data字段详细信息:

字段标识 字段含义 类型 是否一定返回 备注
space_name 表名 string 是
db_name 库名 string 是
doc_num 表文档数量 uint64 是
partition_num 分片数量 int 是 对表所有数据进行分片
replica_num 副本数量 int 是 通常设置为3来实现高可用
schema 表结构 json 是
status 表状态 string 是 red表示表有异常,green正常, yellow预警
partitions 表分片详细信息 json 是 参数控制是否返回更多详细信息
errors 表异常信息 string数组 否

返回值格式如下:

1. {
2. "code": 0,
3. "data": {
4. "space_name": "ts_space",
5. "db_name": "ts_db",
6. "doc_num": 0,
7. "partition_num": 1,
8. "replica_num": 3,
9. "schema": {
10. "fields": [
11. {
12. "name": "field_string",
13. "type": "string"
14. },
15. {
16. "name": "field_int",
17. "type": "integer"
18. },
19. {
20. "name": "field_float",
21. "type": "float",
22. "index": {
23. "name": "field_float",
24. "type": "SCALAR"
25. }
26. },
27. {
28. "name": "field_string_array",
29. "type": "stringArray",
30. "index": {
31. "name": "field_string_array",
32. "type": "SCALAR"
33. }
34. },
35. {
36. "name": "field_int_index",
37. "type": "integer",
38. "index": {
39. "name": "field_int_index",
40. "type": "SCALAR"
41. }
42. },
43. {
44. "name": "field_vector",
45. "type": "vector",
46. "dimension": 128,
47. "index": {
48. "name": "gamma",
49. "type": "IVFPQ",
50. "params": {
51. "metric_type": "InnerProduct",
52. "ncentroids": 2048,
53. "nlinks": 32,
54. "efConstruction": 40
55. }
56. }
57. }
58. ]
59. },
60. "status": "green",
61. "partitions": [
62. {
63. "pid": 1,
64. "replica_num": 1,
65. "status": 4,
66. "color": "green",
67. "ip": "x.x.x.x",
68. "node_id": 1,
69. "index_status": 0,
70. "index_num": 0,
71. "max_docid": -1
72. },
73. {
74. "pid": 2,
75. "replica_num": 1,
76. "status": 4,
77. "color": "green",
78. "ip": "x.x.x.x",
79. "node_id": 2,
80. "index_status": 0,
81. "index_num": 0,
82. "max_docid": -1
83. },
84. {
85. "pid": 3,
86. "replica_num": 1,
87. "status": 4,
88. "color": "green",
89. "ip": "x.x.x.x",
90. "node_id": 3,
91. "index_status": 0,
92. "index_num": 0,
93. "max_docid": -1
94. }
95. ],
96. }
97. }

查看表partitions更多详细信息

1. curl -XGET http://${VEARCH_URL}/dbs/$db_name/spaces/$space_name?detail=true

返回值格式如下:

1. {
2. "code": 0,
3. "data": {
4. "space_name": "ts_space",
5. "db_name": "ts_db",
6. "doc_num": 0,
7. "partition_num": 1,
8. "replica_num": 3,
9. "schema": {
10. "fields": [
11. {
12. "name": "field_string",
13. "type": "string"
14. },
15. {
16. "name": "field_int",
17. "type": "integer"
18. },
19. {
20. "name": "field_float",
21. "type": "float",
22. "index": {
23. "name": "field_float",
24. "type": "SCALAR"
25. }
26. },
27. {
28. "name": "field_string_array",
29. "type": "stringArray",
30. "index": {
31. "name": "field_string_array",
32. "type": "SCALAR"
33. }
34. },
35. {
36. "name": "field_int_index",
37. "type": "integer",
38. "index": {
39. "name": "field_int_index",
40. "type": "SCALAR"
41. }
42. },
43. {
44. "name": "field_vector",
45. "type": "vector",
46. "dimension": 128,
47. "index": {
48. "name": "gamma",
49. "type": "IVFPQ",
50. "params": {
51. "metric_type": "InnerProduct",
52. "ncentroids": 2048,
53. "nlinks": 32,
54. "efConstruction": 40
55. }
56. }
57. }
58. ]
59. },
60. "status": "green",
61. "partitions": [
62. {
63. "pid": 1,
64. "replica_num": 1,
65. "path": "/export/Data/datas/",
66. "status": 4,
67. "color": "green",
68. "ip": "x.x.x.x",
69. "node_id": 1,
70. "raft_status": {
71. "ID": 1,
72. "NodeID": 1,
73. "Leader": 1,
74. "Term": 1,
75. "Index": 1,
76. "Commit": 1,
77. "Applied": 1,
78. "Vote": 1,
79. "PendQueue": 0,
80. "RecvQueue": 0,
81. "AppQueue": 0,
82. "Stopped": false,
83. "RestoringSnapshot": false,
84. "State": "StateLeader",
85. "Replicas": {
86. "1": {
87. "Match": 1,
88. "Commit": 1,
89. "Next": 2,
90. "State": "ReplicaStateProbe",
91. "Snapshoting": false,
92. "Paused": false,
93. "Active": true,
94. "LastActive": "2024-03-18T09: 59: 17.095112556+08: 00",
95. "Inflight": 0
96. }
97. }
98. },
99. "index_status": 0,
100. "index_num": 0,
101. "max_docid": -1
102. },
103. {
104. "pid": 2,
105. "replica_num": 1,
106. "path": "/export/Data/datas/",
107. "status": 4,
108. "color": "green",
109. "ip": "x.x.x.x",
110. "node_id": 2,
111. "raft_status": {
112. "ID": 2,
113. "NodeID": 1,
114. "Leader": 1,
115. "Term": 1,
116. "Index": 1,
117. "Commit": 1,
118. "Applied": 1,
119. "Vote": 1,
120. "PendQueue": 0,
121. "RecvQueue": 0,
122. "AppQueue": 0,
123. "Stopped": false,
124. "RestoringSnapshot": false,
125. "State": "StateLeader",
126. "Replicas": {
127. "1": {
128. "Match": 1,
129. "Commit": 1,
130. "Next": 2,
131. "State": "ReplicaStateProbe",
132. "Snapshoting": false,
133. "Paused": false,
134. "Active": true,
135. "LastActive": "2024-03-18T09: 59: 17.095112556+08: 00",
136. "Inflight": 0
137. }
138. }
139. },
140. "index_status": 0,
141. "index_num": 0,
142. "max_docid": -1
143. },
144. {
145. "pid": 3,
146. "replica_num": 1,
147. "path": "/export/Data/datas/",
148. "status": 4,
149. "color": "green",
150. "ip": "x.x.x.x",
151. "node_id": 3,
152. "raft_status": {
153. "ID": 3,
154. "NodeID": 1,
155. "Leader": 1,
156. "Term": 1,
157. "Index": 1,
158. "Commit": 1,
159. "Applied": 1,
160. "Vote": 1,
161. "PendQueue": 0,
162. "RecvQueue": 0,
163. "AppQueue": 0,
164. "Stopped": false,
165. "RestoringSnapshot": false,
166. "State": "StateLeader",
167. "Replicas": {
168. "1": {
169. "Match": 1,
170. "Commit": 1,
171. "Next": 2,
172. "State": "ReplicaStateProbe",
173. "Snapshoting": false,
174. "Paused": false,
175. "Active": true,
176. "LastActive": "2024-03-18T09: 59: 17.095112556+08: 00",
177. "Inflight": 0
178. }
179. }
180. },
181. "index_status": 0,
182. "index_num": 0,
183. "max_docid": -1
184. }
185. ]
186. }
187. }

删除表空间

1. curl -XDELETE http://${VEARCH_URL}/dbs/$db_name/spaces/$space_name