@zilliz/milvus2-sdk-node 2.2.9 → 2.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/README.md +76 -12
  2. package/dist/milvus/MilvusClient.d.ts +21 -2
  3. package/dist/milvus/MilvusClient.js +35 -29
  4. package/dist/milvus/MilvusClient.js.map +1 -1
  5. package/dist/milvus/OrmClient.d.ts +19 -0
  6. package/dist/milvus/OrmClient.js +77 -0
  7. package/dist/milvus/OrmClient.js.map +1 -0
  8. package/dist/milvus/const/ErrorReason.d.ts +36 -31
  9. package/dist/milvus/const/ErrorReason.js +39 -32
  10. package/dist/milvus/const/ErrorReason.js.map +1 -1
  11. package/dist/milvus/const/Milvus.d.ts +2 -9
  12. package/dist/milvus/const/Milvus.js +16 -33
  13. package/dist/milvus/const/Milvus.js.map +1 -1
  14. package/dist/milvus/const/client.d.ts +5 -0
  15. package/dist/milvus/const/client.js +10 -0
  16. package/dist/milvus/const/client.js.map +1 -0
  17. package/dist/milvus/const/defaults.d.ts +11 -0
  18. package/dist/milvus/const/defaults.js +16 -0
  19. package/dist/milvus/const/defaults.js.map +1 -0
  20. package/dist/milvus/const/highLevel.d.ts +23 -0
  21. package/dist/milvus/const/highLevel.js +26 -0
  22. package/dist/milvus/const/highLevel.js.map +1 -0
  23. package/dist/milvus/const/index.d.ts +5 -0
  24. package/dist/milvus/{types.js → const/index.js} +7 -10
  25. package/dist/milvus/const/index.js.map +1 -0
  26. package/dist/milvus/const/limits.d.ts +2 -0
  27. package/dist/milvus/const/limits.js +7 -0
  28. package/dist/milvus/const/limits.js.map +1 -0
  29. package/dist/milvus/{BaseClient.d.ts → grpc/BaseClient.d.ts} +20 -8
  30. package/dist/milvus/grpc/BaseClient.js +83 -0
  31. package/dist/milvus/grpc/BaseClient.js.map +1 -0
  32. package/dist/milvus/{Collection.d.ts → grpc/Collection.d.ts} +32 -3
  33. package/dist/milvus/grpc/Collection.js +786 -0
  34. package/dist/milvus/grpc/Collection.js.map +1 -0
  35. package/dist/milvus/{Data.d.ts → grpc/Data.d.ts} +68 -10
  36. package/dist/milvus/grpc/Data.js +880 -0
  37. package/dist/milvus/grpc/Data.js.map +1 -0
  38. package/dist/milvus/grpc/Database.d.ts +69 -0
  39. package/dist/milvus/grpc/Database.js +98 -0
  40. package/dist/milvus/grpc/Database.js.map +1 -0
  41. package/dist/milvus/grpc/GrpcClient.d.ts +46 -0
  42. package/dist/milvus/grpc/GrpcClient.js +150 -0
  43. package/dist/milvus/grpc/GrpcClient.js.map +1 -0
  44. package/dist/milvus/{MilvusIndex.d.ts → grpc/MilvusIndex.d.ts} +1 -1
  45. package/dist/milvus/grpc/MilvusIndex.js +203 -0
  46. package/dist/milvus/grpc/MilvusIndex.js.map +1 -0
  47. package/dist/milvus/{Partition.d.ts → grpc/Partition.d.ts} +1 -1
  48. package/dist/milvus/grpc/Partition.js +265 -0
  49. package/dist/milvus/grpc/Partition.js.map +1 -0
  50. package/dist/milvus/{Resource.d.ts → grpc/Resource.d.ts} +2 -2
  51. package/dist/milvus/grpc/Resource.js +250 -0
  52. package/dist/milvus/grpc/Resource.js.map +1 -0
  53. package/dist/milvus/{User.d.ts → grpc/User.d.ts} +1 -1
  54. package/dist/milvus/grpc/User.js +619 -0
  55. package/dist/milvus/grpc/User.js.map +1 -0
  56. package/dist/milvus/index.d.ts +4 -13
  57. package/dist/milvus/index.js +6 -14
  58. package/dist/milvus/index.js.map +1 -1
  59. package/dist/milvus/orm/Collection.d.ts +45 -0
  60. package/dist/milvus/orm/Collection.js +172 -0
  61. package/dist/milvus/orm/Collection.js.map +1 -0
  62. package/dist/milvus/orm/index.d.ts +2 -0
  63. package/dist/milvus/orm/index.js +19 -0
  64. package/dist/milvus/orm/index.js.map +1 -0
  65. package/dist/milvus/orm/utils.d.ts +27 -0
  66. package/dist/milvus/orm/utils.js +27 -0
  67. package/dist/milvus/orm/utils.js.map +1 -0
  68. package/dist/milvus/types/Client.d.ts +12 -1
  69. package/dist/milvus/types/Collection.d.ts +18 -4
  70. package/dist/milvus/types/Common.d.ts +1 -1
  71. package/dist/milvus/types/Data.d.ts +41 -5
  72. package/dist/milvus/types/Database.d.ts +12 -0
  73. package/dist/milvus/types/{Index.js → Database.js} +1 -1
  74. package/dist/milvus/types/Database.js.map +1 -0
  75. package/dist/milvus/types/HighLevel.d.ts +13 -0
  76. package/dist/milvus/types/HighLevel.js +3 -0
  77. package/dist/milvus/types/HighLevel.js.map +1 -0
  78. package/dist/milvus/types/{Index.d.ts → MilvusIndex.d.ts} +3 -2
  79. package/dist/milvus/types/MilvusIndex.js +3 -0
  80. package/dist/milvus/types/MilvusIndex.js.map +1 -0
  81. package/dist/milvus/types/Resource.d.ts +1 -1
  82. package/dist/milvus/types/User.d.ts +1 -1
  83. package/dist/milvus/types/index.d.ts +12 -0
  84. package/dist/milvus/types/index.js +30 -0
  85. package/dist/milvus/types/index.js.map +1 -0
  86. package/dist/{utils → milvus/utils}/Blob.js +4 -4
  87. package/dist/milvus/utils/Blob.js.map +1 -0
  88. package/dist/{utils → milvus/utils}/Format.d.ts +66 -2
  89. package/dist/milvus/utils/Format.js +416 -0
  90. package/dist/milvus/utils/Format.js.map +1 -0
  91. package/dist/{utils → milvus/utils}/Function.d.ts +1 -1
  92. package/dist/{utils → milvus/utils}/Function.js +7 -7
  93. package/dist/milvus/utils/Function.js.map +1 -0
  94. package/dist/{utils → milvus/utils}/Grpc.d.ts +9 -8
  95. package/dist/milvus/utils/Grpc.js +190 -0
  96. package/dist/milvus/utils/Grpc.js.map +1 -0
  97. package/dist/{utils → milvus/utils}/Validate.d.ts +7 -1
  98. package/dist/milvus/utils/Validate.js +140 -0
  99. package/dist/milvus/utils/Validate.js.map +1 -0
  100. package/dist/{utils → milvus/utils}/index.d.ts +2 -0
  101. package/dist/{utils → milvus/utils}/index.js +2 -0
  102. package/dist/milvus/utils/index.js.map +1 -0
  103. package/dist/milvus/utils/logger.d.ts +1 -0
  104. package/dist/milvus/utils/logger.js +18 -0
  105. package/dist/milvus/utils/logger.js.map +1 -0
  106. package/dist/proto/proto/common.proto +38 -14
  107. package/dist/proto/proto/google/protobuf/descriptor.proto +8 -4
  108. package/dist/proto/proto/milvus.proto +86 -83
  109. package/dist/proto/proto/schema.proto +5 -0
  110. package/dist/sdk.json +1 -1
  111. package/package.json +14 -11
  112. package/dist/milvus/BaseClient.js +0 -62
  113. package/dist/milvus/BaseClient.js.map +0 -1
  114. package/dist/milvus/Collection.js +0 -960
  115. package/dist/milvus/Collection.js.map +0 -1
  116. package/dist/milvus/Data.js +0 -909
  117. package/dist/milvus/Data.js.map +0 -1
  118. package/dist/milvus/GrpcClient.d.ts +0 -32
  119. package/dist/milvus/GrpcClient.js +0 -236
  120. package/dist/milvus/GrpcClient.js.map +0 -1
  121. package/dist/milvus/MilvusIndex.js +0 -299
  122. package/dist/milvus/MilvusIndex.js.map +0 -1
  123. package/dist/milvus/Partition.js +0 -369
  124. package/dist/milvus/Partition.js.map +0 -1
  125. package/dist/milvus/Resource.js +0 -356
  126. package/dist/milvus/Resource.js.map +0 -1
  127. package/dist/milvus/User.js +0 -806
  128. package/dist/milvus/User.js.map +0 -1
  129. package/dist/milvus/Utils.d.ts +0 -1
  130. package/dist/milvus/Utils.js +0 -8
  131. package/dist/milvus/Utils.js.map +0 -1
  132. package/dist/milvus/types/Index.js.map +0 -1
  133. package/dist/milvus/types.d.ts +0 -9
  134. package/dist/milvus/types.js.map +0 -1
  135. package/dist/proto/proto/feder.proto +0 -40
  136. package/dist/proto/proto/msg.proto +0 -107
  137. package/dist/utils/Blob.js.map +0 -1
  138. package/dist/utils/Format.js +0 -252
  139. package/dist/utils/Format.js.map +0 -1
  140. package/dist/utils/Function.js.map +0 -1
  141. package/dist/utils/Grpc.js +0 -170
  142. package/dist/utils/Grpc.js.map +0 -1
  143. package/dist/utils/Validate.js +0 -124
  144. package/dist/utils/Validate.js.map +0 -1
  145. package/dist/utils/index.js.map +0 -1
  146. package/dist/utils/test.d.ts +0 -15
  147. package/dist/utils/test.js +0 -98
  148. package/dist/utils/test.js.map +0 -1
  149. /package/dist/{utils → milvus/utils}/Blob.d.ts +0 -0
@@ -0,0 +1,880 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Data = void 0;
13
+ const __1 = require("../");
14
+ const Collection_1 = require("./Collection");
15
+ class Data extends Collection_1.Collection {
16
+ constructor() {
17
+ super(...arguments);
18
+ // vectorTypes
19
+ this.vectorTypes = [__1.DataType.BinaryVector, __1.DataType.FloatVector];
20
+ }
21
+ /**
22
+ * Insert data into Milvus.
23
+ *
24
+ * @param data
25
+ * | Property | Type | Description |
26
+ * | :--- | :-- | :-- |
27
+ * | collection_name | String | Collection name |
28
+ * | partition_name(optional)| String | Partition name |
29
+ * | fields_data or data | { [x: string]: any }[] | If the field type is binary, the vector data length needs to be dimension / 8 |
30
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
31
+
32
+ *
33
+ * @returns
34
+ * | Property | Description |
35
+ * | :-- | :-- |
36
+ * | status | { error_code: number, reason: string } |
37
+ * | succ_index | Index array of the successfully inserted data |
38
+ * | err_index | Index array of the unsuccessfully inserted data |
39
+ * | IDs | ID array of the successfully inserted data |
40
+ *
41
+ *
42
+ * #### Example
43
+ *
44
+ * ```
45
+ * new milvusClient(MILUVS_ADDRESS).insert({
46
+ * collection_name: COLLECTION_NAME,
47
+ * fields_data: [{
48
+ * vector_field: [1,2,2,4],
49
+ * scalar_field: 1
50
+ * }]
51
+ * });
52
+ * ```
53
+ */
54
+ insert(data) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ (0, __1.checkCollectionName)(data);
57
+ // ensure fields data available
58
+ data.fields_data = data.fields_data || data.data;
59
+ if (!data.fields_data ||
60
+ !Array.isArray(data.fields_data) ||
61
+ !data.fields_data.length) {
62
+ throw new Error(__1.ERROR_REASONS.INSERT_CHECK_FIELD_DATA_IS_REQUIRED);
63
+ }
64
+ const { collection_name } = data;
65
+ const collectionInfo = yield this.describeCollection({
66
+ collection_name,
67
+ });
68
+ if (collectionInfo.status.error_code !== __1.ErrorCode.SUCCESS) {
69
+ throw new Error(collectionInfo.status.reason);
70
+ }
71
+ // Tip: The field data sequence needs to be set same as `collectionInfo.schema.fields`.
72
+ // If primarykey is set `autoid = true`, you cannot insert the data.
73
+ const fieldsDataMap = new Map(collectionInfo.schema.fields
74
+ .filter(v => !v.is_primary_key || !v.autoID)
75
+ .map(v => [
76
+ v.name,
77
+ {
78
+ name: v.name,
79
+ type: v.data_type,
80
+ dim: Number((0, __1.findKeyValue)(v.type_params, 'dim')),
81
+ value: [], // value container
82
+ },
83
+ ]));
84
+ // The actual data we pass to Milvus gRPC.
85
+ const params = Object.assign(Object.assign({}, data), { num_rows: data.fields_data.length });
86
+ // dynamic field is enabled, create $meta field
87
+ const isDynamic = collectionInfo.schema.enable_dynamic_field;
88
+ if (isDynamic) {
89
+ fieldsDataMap.set(__1.DEFAULT_DYNAMIC_FIELD, {
90
+ name: __1.DEFAULT_DYNAMIC_FIELD,
91
+ type: 'JSON',
92
+ value: [], // value container
93
+ });
94
+ }
95
+ // Loop through each row and set the corresponding field values in the Map.
96
+ data.fields_data.forEach((v, i) => {
97
+ // if support dynamic field, all field not in the schema would be grouped to a dynamic field
98
+ v = isDynamic
99
+ ? (0, __1.generateDynamicRow)(v, fieldsDataMap, __1.DEFAULT_DYNAMIC_FIELD)
100
+ : v;
101
+ // get each fieldname in the data object
102
+ const fieldNames = Object.keys(v);
103
+ // go through each fieldname and encode or format data
104
+ fieldNames.forEach(name => {
105
+ const target = fieldsDataMap.get(name);
106
+ if (!target) {
107
+ throw new Error(`${__1.ERROR_REASONS.INSERT_CHECK_WRONG_FIELD} ${i}`);
108
+ }
109
+ if (__1.DataTypeMap[target.type] === __1.DataType.BinaryVector &&
110
+ v[name].length !== target.dim / 8) {
111
+ throw new Error(__1.ERROR_REASONS.INSERT_CHECK_WRONG_DIM);
112
+ }
113
+ // encode data
114
+ switch (__1.DataTypeMap[target.type]) {
115
+ case __1.DataType.BinaryVector:
116
+ case __1.DataType.FloatVector:
117
+ for (let val of v[name]) {
118
+ target.value.push(val);
119
+ }
120
+ break;
121
+ case __1.DataType.JSON:
122
+ // ensure empty string
123
+ target.value[i] = Buffer.from(JSON.stringify(v[name] || {}));
124
+ break;
125
+ default:
126
+ target.value[i] = v[name];
127
+ break;
128
+ }
129
+ });
130
+ });
131
+ // transform data from map to array, milvus grpc params
132
+ params.fields_data = Array.from(fieldsDataMap.values()).map(v => {
133
+ // milvus return string for field type, so we define the DataTypeMap to the value we need.
134
+ // but if milvus change the string, may cause we cant find value.
135
+ const type = __1.DataTypeMap[v.type];
136
+ const key = this.vectorTypes.includes(type) ? 'vectors' : 'scalars';
137
+ let dataKey = 'float_vector';
138
+ switch (type) {
139
+ case __1.DataType.FloatVector:
140
+ dataKey = 'float_vector';
141
+ break;
142
+ case __1.DataType.BinaryVector:
143
+ dataKey = 'binary_vector';
144
+ break;
145
+ case __1.DataType.Double:
146
+ dataKey = 'double_data';
147
+ break;
148
+ case __1.DataType.Float:
149
+ dataKey = 'float_data';
150
+ break;
151
+ case __1.DataType.Int64:
152
+ dataKey = 'long_data';
153
+ break;
154
+ case __1.DataType.Int32:
155
+ case __1.DataType.Int16:
156
+ case __1.DataType.Int8:
157
+ dataKey = 'int_data';
158
+ break;
159
+ case __1.DataType.Bool:
160
+ dataKey = 'bool_data';
161
+ break;
162
+ case __1.DataType.VarChar:
163
+ dataKey = 'string_data';
164
+ break;
165
+ case __1.DataType.JSON:
166
+ dataKey = 'json_data';
167
+ break;
168
+ default:
169
+ throw new Error(`${__1.ERROR_REASONS.INSERT_CHECK_WRONG_DATA_TYPE} "${v.type}."`);
170
+ }
171
+ return {
172
+ type,
173
+ field_name: v.name,
174
+ is_dynamic: v.name === __1.DEFAULT_DYNAMIC_FIELD,
175
+ [key]: type === __1.DataType.FloatVector
176
+ ? {
177
+ dim: v.dim,
178
+ [dataKey]: {
179
+ data: v.value,
180
+ },
181
+ }
182
+ : type === __1.DataType.BinaryVector
183
+ ? {
184
+ dim: v.dim,
185
+ [dataKey]: (0, __1.parseBinaryVectorToBytes)(v.value),
186
+ }
187
+ : {
188
+ [dataKey]: {
189
+ data: v.value,
190
+ },
191
+ },
192
+ };
193
+ });
194
+ const promise = yield (0, __1.promisify)(this.client, 'Insert', params, data.timeout || this.timeout);
195
+ return promise;
196
+ });
197
+ }
198
+ /**
199
+ * Delete entities in Milvus
200
+ *
201
+ * @param data
202
+ * | Property | Type | Description |
203
+ * | :--- | :-- | :-- |
204
+ * | collection_name | String | Collection name |
205
+ * | partition_name(optional)| String | Partition name |
206
+ * | expr or filter | String | Boolean expression used to filter attribute. |
207
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
208
+
209
+ *
210
+ * @returns
211
+ * | Property | Description |
212
+ * | :-- | :-- |
213
+ * | status | { error_code: number, reason: string } |
214
+ * | IDs | ID array of the successfully deleted data |
215
+ *
216
+ *
217
+ * #### Example
218
+ *
219
+ * ```
220
+ * new milvusClient(MILUVS_ADDRESS).deleteEntities({
221
+ * collection_name: COLLECTION_NAME,
222
+ * expr: 'id in [1,2,3,4]'
223
+ * });
224
+ * ```
225
+ */
226
+ deleteEntities(data) {
227
+ return __awaiter(this, void 0, void 0, function* () {
228
+ if (!data || !data.collection_name) {
229
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
230
+ }
231
+ // check expr or filter
232
+ if (!data.filter && !data.expr) {
233
+ throw new Error(__1.ERROR_REASONS.FILTER_EXPR_REQUIRED);
234
+ }
235
+ // filter > expr
236
+ data.expr = data.filter || data.expr;
237
+ const promise = yield (0, __1.promisify)(this.client, 'Delete', data, data.timeout || this.timeout);
238
+ return promise;
239
+ });
240
+ }
241
+ /**
242
+ * Delete entities in Milvus
243
+ *
244
+ * @param data
245
+ * | Property | Type | Description |
246
+ * | :--- | :-- | :-- |
247
+ * | collection_name | String | Collection name |
248
+ * | partition_name(optional)| String | Partition name |
249
+ * | ids | String[] or Number[] | ids to delete |
250
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
251
+
252
+ *
253
+ * @returns
254
+ * | Property | Description |
255
+ * | :-- | :-- |
256
+ * | status | { error_code: number, reason: string } |
257
+ * | IDs | ID array of the successfully deleted data |
258
+ *
259
+ *
260
+ * #### Example
261
+ *
262
+ * ```
263
+ * new milvusClient(MILUVS_ADDRESS).deleteEntities({
264
+ * collection_name: COLLECTION_NAME,
265
+ * expr: 'id in [1,2,3,4]'
266
+ * });
267
+ * ```
268
+ */
269
+ delete(data) {
270
+ return __awaiter(this, void 0, void 0, function* () {
271
+ if (!data || !data.collection_name) {
272
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
273
+ }
274
+ if (!data.ids || data.ids.length === 0) {
275
+ throw new Error(__1.ERROR_REASONS.IDS_REQUIRED);
276
+ }
277
+ const pkField = yield this.getPkFieldName(data);
278
+ const req = Object.assign(Object.assign({}, data), { expr: `${pkField} in [${data.ids.join(',')}]` });
279
+ return this.deleteEntities(req);
280
+ });
281
+ }
282
+ /**
283
+ * Perform vector similarity search.
284
+ *
285
+ * @param data
286
+ * | Property | Type | Description |
287
+ * | :--- | :-- | :-- |
288
+ * | collection_name | String | Collection name |
289
+ * | vectors or data or (vector) | Number[][] or Number[] | Original vector to search with |
290
+ * | partition_names(optional)| String[] | Array of partition names |
291
+ * | limit(optional) | number | topk alias |
292
+ * | topk(optional) | number | topk |
293
+ * | offset(optional) | number | offset |
294
+ * | filter(optional) | String | Scalar field filter expression |
295
+ * | expr(optional) | String | filter alias |
296
+ * | output_fields(optional) | String[] | Support scalar field |
297
+ * | metric_type(optional) | String | similarity metric |
298
+ * | params(optional) | key value object | search params |
299
+ *
300
+ * @returns
301
+ * | Property | Description |
302
+ * | :-- | :-- |
303
+ * | status | { error_code: number, reason: string } |
304
+ * | results | {score:number,id:string}[]; |
305
+ *
306
+ * #### Example
307
+ *
308
+ * ```
309
+ * new milvusClient(MILUVS_ADDRESS).search({
310
+ * collection_name: COLLECTION_NAME,
311
+ * vector: [1, 2, 3, 4],
312
+ * });
313
+ * ```
314
+ */
315
+ search(data) {
316
+ var _a, _b, _c, _d;
317
+ return __awaiter(this, void 0, void 0, function* () {
318
+ // params check
319
+ (0, __1.checkSearchParams)(data);
320
+ try {
321
+ // get collection info
322
+ const collectionInfo = yield this.describeCollection({
323
+ collection_name: data.collection_name,
324
+ });
325
+ // get information from collection info
326
+ let vectorType;
327
+ let defaultOutputFields = [];
328
+ let anns_field;
329
+ for (let i = 0; i < collectionInfo.schema.fields.length; i++) {
330
+ const f = collectionInfo.schema.fields[i];
331
+ const type = __1.DataTypeMap[f.data_type];
332
+ // filter vector field
333
+ if (type === __1.DataType.FloatVector || type === __1.DataType.BinaryVector) {
334
+ // anns field
335
+ anns_field = f.name;
336
+ // vector type
337
+ vectorType = type;
338
+ }
339
+ else {
340
+ // save field name
341
+ defaultOutputFields.push(f.name);
342
+ }
343
+ }
344
+ // create search params
345
+ const search_params = data.search_params || {
346
+ anns_field: anns_field,
347
+ topk: data.limit ||
348
+ data.topk ||
349
+ __1.DEFAULT_TOPK,
350
+ offset: data.offset || 0,
351
+ metric_type: data.metric_type || __1.DEFAULT_METRIC_TYPE,
352
+ params: JSON.stringify(data.params || {}),
353
+ };
354
+ // create search vectors
355
+ let searchVectors = data.vectors ||
356
+ data.data ||
357
+ data.vector;
358
+ // make sure the searchVectors format is correct
359
+ if (!Array.isArray(searchVectors[0])) {
360
+ searchVectors = [searchVectors];
361
+ }
362
+ /**
363
+ * It will decide the score precision.
364
+ * If round_decimal is 3, need return like 3.142
365
+ * And if Milvus return like 3.142, Node will add more number after this like 3.142000047683716.
366
+ * So the score need to slice by round_decimal
367
+ */
368
+ const round_decimal = (_b = (_a = data.search_params) === null || _a === void 0 ? void 0 : _a.round_decimal) !== null && _b !== void 0 ? _b : (_c = data.params) === null || _c === void 0 ? void 0 : _c.round_decimal;
369
+ // create placeholder_group
370
+ const PlaceholderGroup = this.milvusProto.lookupType('milvus.proto.common.PlaceholderGroup');
371
+ // tag $0 is hard code in milvus, when dsltype is expr
372
+ const placeholderGroupBytes = PlaceholderGroup.encode(PlaceholderGroup.create({
373
+ placeholders: [
374
+ {
375
+ tag: '$0',
376
+ type: vectorType,
377
+ values: searchVectors.map(v => vectorType === __1.DataType.BinaryVector
378
+ ? (0, __1.parseBinaryVectorToBytes)(v)
379
+ : (0, __1.parseFloatVectorToBytes)(v)),
380
+ },
381
+ ],
382
+ })).finish();
383
+ const promise = yield (0, __1.promisify)(this.client, 'Search', {
384
+ collection_name: data.collection_name,
385
+ partition_names: data.partition_names,
386
+ output_fields: data.output_fields || defaultOutputFields,
387
+ nq: data.nq || searchVectors.length,
388
+ dsl: data.expr || data.filter || '',
389
+ dsl_type: __1.DslType.BoolExprV1,
390
+ placeholder_group: placeholderGroupBytes,
391
+ search_params: (0, __1.parseToKeyValue)(search_params),
392
+ consistency_level: data.consistency_level,
393
+ }, data.timeout || this.timeout);
394
+ // if search failed, return empty with status
395
+ if (promise.status.error_code !== __1.ErrorCode.SUCCESS) {
396
+ return {
397
+ status: promise.status,
398
+ results: [],
399
+ };
400
+ }
401
+ // build final results array
402
+ const results = [];
403
+ const { topks, scores, fields_data, ids } = promise.results;
404
+ // build fields data map
405
+ const fieldsDataMap = (0, __1.getFieldDataMap)(fields_data);
406
+ // build output name array
407
+ const output_fields = [
408
+ 'id',
409
+ ...(promise.results.output_fields ||
410
+ fields_data.map(f => f.field_name)),
411
+ ];
412
+ // vector id support int / str id.
413
+ const idData = ids ? (_d = ids[ids.id_field]) === null || _d === void 0 ? void 0 : _d.data : undefined;
414
+ // add id column
415
+ fieldsDataMap.set('id', idData);
416
+ // fieldsDataMap.set('score', scores); TODO: fieldDataMap to support formatter
417
+ /**
418
+ * This code block formats the search results returned by Milvus into row data for easier use.
419
+ * Milvus supports multiple queries to search and returns all columns data, so we need to splice the data for each search result using the `topk` variable.
420
+ * The `topk` variable is the key we use to splice data for every search result.
421
+ * The `scores` array is spliced using the `topk` value, and the resulting scores are formatted to the specified precision using the `formatNumberPrecision` function. The resulting row data is then pushed to the `results` array.
422
+ */
423
+ topks.forEach((v, index) => {
424
+ const topk = Number(v);
425
+ scores.splice(0, topk).forEach((score, scoreIndex) => {
426
+ // get correct index
427
+ const i = index === 0 ? scoreIndex : scoreIndex + topk;
428
+ // fix round_decimal
429
+ const fixedScore = typeof round_decimal === 'undefined' || round_decimal === -1
430
+ ? score
431
+ : (0, __1.formatNumberPrecision)(score, round_decimal);
432
+ // init result object
433
+ const result = { score: fixedScore };
434
+ // build result,
435
+ output_fields.forEach(field_name => {
436
+ // Check if the field_name exists in the fieldsDataMap
437
+ const isFixedSchema = fieldsDataMap.has(field_name);
438
+ // Get the data for the field_name from the fieldsDataMap
439
+ // If the field_name is not in the fieldsDataMap, use the DEFAULT_DYNAMIC_FIELD
440
+ const data = fieldsDataMap.get(isFixedSchema ? field_name : __1.DEFAULT_DYNAMIC_FIELD);
441
+ // make data[i] safe
442
+ data[i] = data[i] || {};
443
+ // extract dynamic info from dynamic field if necessary
444
+ result[field_name] = isFixedSchema ? data[i] : data[i][field_name];
445
+ });
446
+ // init result slot
447
+ results[index] = results[index] || [];
448
+ // push result data
449
+ results[index].push(result);
450
+ });
451
+ });
452
+ return {
453
+ status: promise.status,
454
+ // if only searching 1 vector, return the first object of results array
455
+ results: searchVectors.length === 1 ? results[0] : results,
456
+ };
457
+ }
458
+ catch (err) {
459
+ /* istanbul ignore next */
460
+ throw new Error(err);
461
+ }
462
+ });
463
+ }
464
+ /**
465
+ * Milvus temporarily buffers the newly inserted vectors in the cache. Call `flush()` to persist them to the object storage.
466
+ * It's async function, so it's will take some times to execute.
467
+ * @param data
468
+ * | Property | Type | Description |
469
+ * | :--- | :-- | :-- |
470
+ * | collection_names | String[] | Array of collection names |
471
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
472
+
473
+ *
474
+ * @returns
475
+ * | Property | Description |
476
+ * | :-- | :-- |
477
+ * | status | { error_code: number, reason: string } |
478
+ *
479
+ * #### Example
480
+ *
481
+ * ```
482
+ * new milvusClient(MILUVS_ADDRESS).flush({
483
+ * collection_names: ['my_collection'],
484
+ * });
485
+ * ```
486
+ */
487
+ flush(data) {
488
+ return __awaiter(this, void 0, void 0, function* () {
489
+ if (!data ||
490
+ !Array.isArray(data.collection_names) ||
491
+ !data.collection_names.length) {
492
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
493
+ }
494
+ const res = yield (0, __1.promisify)(this.client, 'Flush', data, data.timeout || this.timeout);
495
+ return res;
496
+ });
497
+ }
498
+ /**
499
+ * It's same function as flush. But flushSync is sync function.
500
+ * So you can ensure it's flushed after function return the result.
501
+ *
502
+ * @param data
503
+ * | Property | Type | Description |
504
+ * | :--- | :-- | :-- |
505
+ * | collection_names | String[] | Array of collection names |
506
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
507
+
508
+ *
509
+ * @returns
510
+ * | Property | Description |
511
+ * | :-- | :-- |
512
+ * | status | { error_code: number, reason: string } |
513
+ *
514
+ * #### Example
515
+ *
516
+ * ```
517
+ * new milvusClient(MILUVS_ADDRESS).flushSync({
518
+ * collection_names: ['my_collection'],
519
+ * });
520
+ * ```
521
+ */
522
+ flushSync(data) {
523
+ return __awaiter(this, void 0, void 0, function* () {
524
+ if (!data ||
525
+ !Array.isArray(data.collection_names) ||
526
+ !data.collection_names.length) {
527
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
528
+ }
529
+ // copy flushed collection names
530
+ const res = yield (0, __1.promisify)(this.client, 'Flush', data, data.timeout || this.timeout);
531
+ // After flush will return collection segment ids, need use GetPersistentSegmentInfo to check segment flush status.
532
+ const segIDs = Object.keys(res.coll_segIDs)
533
+ .map(v => res.coll_segIDs[v].data)
534
+ .reduce((pre, cur) => [...pre, ...cur], []);
535
+ let isFlushed = false;
536
+ let flushRes = null;
537
+ while (!isFlushed) {
538
+ flushRes = yield this.getFlushState({ segmentIDs: segIDs });
539
+ yield (0, __1.sleep)(100);
540
+ isFlushed = flushRes.flushed;
541
+ }
542
+ // Before Milvus pre-GA will throw error
543
+ return flushRes;
544
+ });
545
+ }
546
+ /**
547
+ * Query vector data in Milvus. Current release of Milvus only supports expression as fieldname in [id1,id2,id3]
548
+ *
549
+ * @param data
550
+ * | Property | Type | Description |
551
+ * | :--- | :-- | :-- |
552
+ * | collection_name | String | Collection name |
553
+ * | expr or filter | String | Scalar field filter expression |
554
+ * | partitions_names(optional) | String[] | Array of partition names |
555
+ * | output_fields | String[] | Vector or scalar field to be returned |
556
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
557
+
558
+ * | params | {key: value}[] | An optional key pair json array
559
+ *
560
+ * @returns
561
+ * | Property | Description |
562
+ * | :-- | :-- |
563
+ * | status | { error_code: number,reason:string } |
564
+ * | data | Data of all fields that you defined in `output_fields`, {field_name: value}[] |
565
+ *
566
+ *
567
+ * #### Example
568
+ *
569
+ * ```
570
+ * new milvusClient(MILUVS_ADDRESS).query({
571
+ * collection_name: 'my_collection',
572
+ * expr: "age in [1,2,3,4,5,6,7,8]",
573
+ * output_fields: ["age"],
574
+ * });
575
+ * ```
576
+ */
577
+ query(data) {
578
+ return __awaiter(this, void 0, void 0, function* () {
579
+ (0, __1.checkCollectionName)(data);
580
+ // Set up limits and offset for the query
581
+ let limits;
582
+ let offset;
583
+ if (typeof data.limit === 'number') {
584
+ limits = { limit: data.limit };
585
+ }
586
+ if (typeof data.offset === 'number') {
587
+ offset = { offset: data.offset };
588
+ }
589
+ // check expr or filter
590
+ if (!data.filter && !data.expr) {
591
+ throw new Error(__1.ERROR_REASONS.FILTER_EXPR_REQUIRED);
592
+ }
593
+ // filter > expr
594
+ data.expr = data.filter || data.expr;
595
+ // Execute the query and get the results
596
+ const promise = yield (0, __1.promisify)(this.client, 'Query', Object.assign(Object.assign({}, data), { query_params: (0, __1.parseToKeyValue)(Object.assign(Object.assign({}, limits), offset)) }), data.timeout || this.timeout);
597
+ // compatible with milvus before v2.2.9
598
+ const output_fields = promise.output_fields || promise.fields_data.map(f => f.field_name);
599
+ // Initialize an array to hold the query results
600
+ const results = [];
601
+ const fieldsDataMap = (0, __1.getFieldDataMap)(promise.fields_data);
602
+ // For each output field, check if it has a fixed schema or not
603
+ const fieldData = output_fields.map(field_name => {
604
+ // Check if the field_name exists in the fieldsDataMap
605
+ const isFixedSchema = fieldsDataMap.has(field_name);
606
+ // Get the data for the field_name from the fieldsDataMap
607
+ // If the field_name is not in the fieldsDataMap, use the DEFAULT_DYNAMIC_FIELD
608
+ const data = fieldsDataMap.get(isFixedSchema ? field_name : __1.DEFAULT_DYNAMIC_FIELD);
609
+ // Return an object containing the field_name and its corresponding data
610
+ // If the schema is fixed, use the data directly
611
+ // If the schema is not fixed, map the data to extract the field_name values
612
+ return {
613
+ data: isFixedSchema ? data : data.map((d) => d[field_name]),
614
+ field_name,
615
+ };
616
+ });
617
+ // parse column data to [{fieldname:value}]
618
+ fieldData.forEach((v) => {
619
+ v.data.forEach((d, i) => {
620
+ if (!results[i]) {
621
+ results[i] = {
622
+ [v.field_name]: d,
623
+ };
624
+ }
625
+ else {
626
+ results[i] = Object.assign(Object.assign({}, results[i]), { [v.field_name]: d });
627
+ }
628
+ });
629
+ });
630
+ return {
631
+ status: promise.status,
632
+ data: results,
633
+ };
634
+ });
635
+ }
636
+ /**
637
+ * get vector data by providing ids in Milvus
638
+ *
639
+ * @param data
640
+ * | Property | Type | Description |
641
+ * | :--- | :-- | :-- |
642
+ * | collection_name | String | Collection name |
643
+ * | ids | String[] | ids to get |
644
+ * | partitions_names(optional) | String[] | Array of partition names |
645
+ * | output_fields | String[] | Vector or scalar field to be returned |
646
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
647
+
648
+ * | params | {key: value}[] | An optional key pair json array
649
+ *
650
+ * @returns
651
+ * | Property | Description |
652
+ * | :-- | :-- |
653
+ * | status | { error_code: number,reason:string } |
654
+ * | data | Data of all fields that you defined in `output_fields`, {field_name: value}[] |
655
+ *
656
+ *
657
+ * #### Example
658
+ *
659
+ * ```
660
+ * new milvusClient(MILUVS_ADDRESS).get({
661
+ * collection_name: 'my_collection',
662
+ * ids: [1,2,3,4,5,6,7,8],
663
+ * output_fields: ["age"],
664
+ * });
665
+ * ```
666
+ */
667
+ get(data) {
668
+ return __awaiter(this, void 0, void 0, function* () {
669
+ (0, __1.checkCollectionName)(data);
670
+ const pkField = yield this.getPkFieldName(data);
671
+ if (!data.ids || data.ids.length === 0) {
672
+ throw new Error(__1.ERROR_REASONS.IDS_REQUIRED);
673
+ }
674
+ // build query req
675
+ const req = Object.assign(Object.assign({}, data), { expr: `${pkField} in [${data.ids.join(',')}]` });
676
+ return this.query(req);
677
+ });
678
+ }
679
+ /**
680
+ * @ignore
681
+ * @param data
682
+ * | Property | Type | Description |
683
+ * | :--- | :-- | :-- |
684
+ * | request | object | Only allow "system_info" for now |
685
+ */
686
+ getMetric(data) {
687
+ return __awaiter(this, void 0, void 0, function* () {
688
+ if (!data || !data.request || !data.request.metric_type) {
689
+ throw new Error(__1.ERROR_REASONS.GET_METRIC_CHECK_PARAMS);
690
+ }
691
+ const res = yield (0, __1.promisify)(this.client, 'GetMetrics', {
692
+ request: JSON.stringify(data.request),
693
+ }, data.timeout || this.timeout);
694
+ return Object.assign(Object.assign({}, res), { response: JSON.parse(res.response) });
695
+ });
696
+ }
697
+ /**
698
+ * Get flush state by segment ids
699
+ *
700
+ * @param data
701
+ * | Property | Type | Description |
702
+ * | :--- | :-- | :-- |
703
+ * | segmentIDs | Array | The segment ids |
704
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
705
+ *
706
+ * @returns
707
+ * | Property | Description |
708
+ * | :--- | :-- |
709
+ * | status | { error_code: number,reason:string } |
710
+ * | flushed | segments flushed or not |
711
+ *
712
+ *
713
+ * #### Example
714
+ *
715
+ * ```
716
+ * const res = await milvusClient.getFlushState({
717
+ * segmentIDs: segIds,
718
+ * });
719
+ * ```
720
+ */
721
+ getFlushState(data) {
722
+ return __awaiter(this, void 0, void 0, function* () {
723
+ if (!data || !data.segmentIDs) {
724
+ throw new Error(__1.ERROR_REASONS.GET_FLUSH_STATE_CHECK_PARAMS);
725
+ }
726
+ const res = yield (0, __1.promisify)(this.client, 'GetFlushState', data, data.timeout || this.timeout);
727
+ return res;
728
+ });
729
+ }
730
+ /**
731
+ * Do load balancing operation from source query node to destination query node.
732
+ * Only work in cluster milvus.
733
+ *
734
+ * @param data
735
+ * | Property | Type | Description |
736
+ * | :--- | :-- | :-- |
737
+ * | src_nodeID | number | The source query node id to balance. |
738
+ * | dst_nodeIDs | number[] | The destination query node ids to balance.(optional) |
739
+ * | sealed_segmentIDs | number[] | Sealed segment ids to balance.(optional) |
740
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
741
+ *
742
+ * @returns
743
+ * | Property | Description |
744
+ * | :--- | :-- |
745
+ * | status | { error_code: number,reason:string } |
746
+ * | infos | segments information |
747
+ *
748
+ *
749
+ * #### Example
750
+ *
751
+ * ```
752
+ * const res = await loadBalance({
753
+ * src_nodeID: 31,
754
+ * });
755
+ * ```
756
+ */
757
+ loadBalance(data) {
758
+ return __awaiter(this, void 0, void 0, function* () {
759
+ if (!data || !data.src_nodeID) {
760
+ throw new Error(__1.ERROR_REASONS.LOAD_BALANCE_CHECK_PARAMS);
761
+ }
762
+ const res = yield (0, __1.promisify)(this.client, 'LoadBalance', data, data.timeout || this.timeout);
763
+ return res;
764
+ });
765
+ }
766
+ /**
767
+ * Notifies Proxy to return segments information from query nodes.
768
+ *
769
+ * @param data
770
+ * | Property | Type | Description |
771
+ * | :--- | :-- | :-- |
772
+ * | collectionName | String | The name of the collection to get segments info. |
773
+ * | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
774
+ *
775
+ *
776
+ * @returns
777
+ * | Property | Description |
778
+ * | :--- | :-- |
779
+ * | status | { error_code: number,reason:string } |
780
+ * | infos | QuerySegmentInfo is the growing segments's information in query cluster. |
781
+ *
782
+ *
783
+ * #### Example
784
+ *
785
+ * ```
786
+ * const res = await getQuerySegmentInfo({
787
+ * collectionName: COLLECTION,
788
+ * });
789
+ * ```
790
+ */
791
+ getQuerySegmentInfo(data) {
792
+ return __awaiter(this, void 0, void 0, function* () {
793
+ if (!data || !data.collectionName) {
794
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
795
+ }
796
+ const res = yield (0, __1.promisify)(this.client, 'GetQuerySegmentInfo', data, data.timeout || this.timeout);
797
+ return res;
798
+ });
799
+ }
800
+ /**
801
+ * Import data from files
802
+ *
803
+ * @param data
804
+ * | Property | Type | Description |
805
+ * | :--- | :-- | :-- |
806
+ * | collection_name | String | The name of the collection |
807
+ * | files | string[] | File path array |
808
+ *
809
+ *
810
+ * @returns
811
+ * | Property | Description |
812
+ * | :--- | :-- |
813
+ * | status | { error_code: number,reason:string } |
814
+ * | tasks | taskId array |
815
+ *
816
+ *
817
+ * #### Example
818
+ *
819
+ * ```
820
+ * const res = await bulkInsert({
821
+ * collection_name: COLLECTION,
822
+ * files: [`path-to-data-file.json`]
823
+ * });
824
+ * ```
825
+ */
826
+ /* istanbul ignore next */
827
+ bulkInsert(data) {
828
+ return __awaiter(this, void 0, void 0, function* () {
829
+ if (!data || !data.collection_name) {
830
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
831
+ }
832
+ if (!data || !data.files) {
833
+ throw new Error(__1.ERROR_REASONS.IMPORT_FILE_CHECK);
834
+ }
835
+ const res = yield (0, __1.promisify)(this.client, 'Import', Object.assign(Object.assign({}, data), { options: data.options || [] }), data.timeout || this.timeout);
836
+ return res;
837
+ });
838
+ }
839
+ /**
840
+ * List import tasks
841
+ *
842
+ * @param data
843
+ * | Property | Type | Description |
844
+ * | :--- | :-- | :-- |
845
+ * | collection_name | String | The name of the collection |
846
+ * | limit | number | optional, maximum number of tasks returned, list all tasks if the value is 0 |
847
+ *
848
+ *
849
+ * @returns
850
+ * | Property | Description |
851
+ * | :--- | :-- |
852
+ * | status | { error_code: number,reason:string } |
853
+ * | state | import state |
854
+ * | row_count | how many rows to import|
855
+ * | id_list| id lists |
856
+ * | collection_id | collection to be imported to |
857
+ * | tasks | taskId array |
858
+ *
859
+ *
860
+ * #### Example
861
+ *
862
+ * ```
863
+ * const res = await listImportTasks({
864
+ * collection_name: COLLECTION
865
+ * });
866
+ * ```
867
+ */
868
+ /* istanbul ignore next */
869
+ listImportTasks(data) {
870
+ return __awaiter(this, void 0, void 0, function* () {
871
+ if (!data || !data.collection_name) {
872
+ throw new Error(__1.ERROR_REASONS.COLLECTION_NAME_IS_REQUIRED);
873
+ }
874
+ const res = yield (0, __1.promisify)(this.client, 'ListImportTasks', Object.assign(Object.assign({}, data), { limit: data.limit || 0 }), data.timeout || this.timeout);
875
+ return res;
876
+ });
877
+ }
878
+ }
879
+ exports.Data = Data;
880
+ //# sourceMappingURL=Data.js.map