-
Notifications
You must be signed in to change notification settings - Fork 43
/
HelloSparse.ts
87 lines (77 loc) · 2.25 KB
/
HelloSparse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { MilvusClient, InsertReq, DataType, SparseFloatVector } from '@zilliz/milvus2-sdk-node';
import { sparseVectorsData } from './Data';
const COLLECTION_NAME = 'hello_sparse';
(async () => {
// build client
const milvusClient = new MilvusClient({
address: 'localhost:19530',
username: 'username',
password: 'Aa12345!!',
});
console.log('Node client is initialized.');
await milvusClient.dropCollection({
collection_name: COLLECTION_NAME,
});
// create collection
const create = await milvusClient.createCollection({
collection_name: COLLECTION_NAME,
fields: [
{
name: 'age',
description: 'ID field',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
description: 'Vector field',
data_type: DataType.SparseFloatVector,
},
{ name: 'height', description: 'int64 field', data_type: DataType.Int64 },
{
name: 'name',
description: 'VarChar field',
data_type: DataType.VarChar,
max_length: 128,
},
],
});
console.log('Create collection is finished.', create);
const params: InsertReq = {
collection_name: COLLECTION_NAME,
fields_data: sparseVectorsData,
};
// insert data into collection
await milvusClient.insert(params);
console.log('Data is inserted.');
// create index
const createIndex = await milvusClient.createIndex({
collection_name: COLLECTION_NAME,
field_name: 'vector',
metric_type: 'IP',
index_type: 'SPARSE_INVERTED_INDEX',
});
console.log('Index is created', createIndex);
// need load collection before search
const load = await milvusClient.loadCollectionSync({
collection_name: COLLECTION_NAME,
});
console.log('Collection is loaded.', load);
// do the search
for (let i = 0; i < 1; i++) {
console.time('Search time');
const search = await milvusClient.search({
collection_name: COLLECTION_NAME,
vector: sparseVectorsData[i]['vector'],
output_fields: ['age'],
limit: 5,
});
console.timeEnd('Search time');
console.log('Search result', search);
}
// drop collection
await milvusClient.dropCollection({
collection_name: COLLECTION_NAME,
});
})();