Skip to content

Commit

Permalink
Add a sparse example (#328)
Browse files Browse the repository at this point in the history
Signed-off-by: Buqian Zheng <[email protected]>
  • Loading branch information
zhengbuqian authored Jun 20, 2024
1 parent 3bcb585 commit d93b9cf
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/milvus/Data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SparseFloatVector } from "@zilliz/milvus2-sdk-node";

// build example data
export const vectorsData = [
{
Expand Down Expand Up @@ -55,3 +57,43 @@ export const vectorsData = [
name: 'sb7mt',
},
];

type sparseVectorsDataItem = {
vector: SparseFloatVector;
height: number;
name: string;
};

export const sparseVectorsData : sparseVectorsDataItem[] = [
{
"vector": {"1": 0.1, "100": 0.3, "55": 0.324},
"height": 20405,
"name": "zlnmh"
},
{
"vector": {"5": 0.45, "1234": 0.67, "789": 0.23, "456": 0.89},
"height": 93773,
"name": "5lr9y"
},
{
"vector": {"12": 0.34, "5678": 0.56, "234": 0.78, "6789": 0.12},
"height": 85122,
"name": "nes0j"
},
{
"vector": {"3": 0.67, "890": 0.45, "12345": 0.89, "678": 0.23},
"height": 92037,
"name": "ct2li"
},
{
"vector": {"45": 0.12, "678": 0.34, "2345": 0.56, "7890": 0.78},
"height": 31400,
"name": "6ghrg"
},
{
"vector": {"67": 0.45, "8901": 0.67, "123": 0.89, "4567": 0.23},
"height": 1778,
"name": "sb7mt"
}
]
;
87 changes: 87 additions & 0 deletions examples/milvus/HelloSparse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
});
})();

0 comments on commit d93b9cf

Please sign in to comment.