Run Milvus using Node.js
This topic describes how to run Milvus using Node.js.
1. Initialize a Node.js Project
npm init
Node.js version 12 or later is required. View Node.js Beginners Guide for information about installing the correct version for your system.
2. Install TypeScript and Node Milvus SDK and its dependencies
npm install @zilliz/milvus2-sdk-node typescript --save
3. Download sample code HelloMilvus.ts
$ wget https://raw.githubusercontent.com/milvus-io/milvus-sdk-node/main/example/HelloMilvus.ts
4. Scan HelloMilvus.ts
This sample code does the following:
- Imports the Node.js SDK package:
import { MilvusClient, DataType, InsertReq } from "@zilliz/milvus2-sdk-node"
- Connects to the Milvus server:
const milvusClient = new MilvusClient("localhost:19530");
- Creates a collection:
const collectionName = "hello_milvus";
const dim = "4";
const createRes = await milvusClient.createCollection(
{
collection_name: collectionName,
fields: [
{
name: "count",
data_type: DataType.VarChar,
is_primary_key: true,
type_params: {
max_length: '100',
},
},
{
name: "random_value",
data_type: DataType.Double,
description: "",
},
{
name: "float_vector",
data_type: DataType.FloatVector,
description: "",
type_params: {
dim
}
}
]
}
);
console.log("--- Create collection ---", createRes, collectionName);
- Inserts vectors in the new collection:
const generateInsertData = function generateInsertData(
fields: { isVector: boolean; dim?: number; name: string; isBool?: boolean }[],
count: number) {
const results = [];
while (count > 0) {
let value: any = {};
fields.forEach((v) => {
const { isVector, dim, name, isBool } = v;
value[name] = isVector
? [...Array(dim)].map(() => Math.random() * 10)
: isBool
? count % 2 === 0
: count;
});
value["count"] = count;
results.push(value);
count--;
}
return results;
}
const fields = [
{
isVector: true,
dim: 4,
name: "float_vector",
},
{
isVector: false,
name: "random_value",
},
];
const vectorsData = generateInsertData(fields, 1000);
const params: InsertReq = {
collection_name: collectionName,
fields_data: vectorsData,
partition_name: "test",
};
await milvusClient.insert(params);
console.log("--- Insert Data to Collection ---");
- Loads the collection and builds index on it:
await milvusClient.createIndex({
collection_name: collectionName,
field_name: "float_vector",
extra_params: {
index_type: "IVF_FLAT",
metric_type: "L2",
params: JSON.stringify({ nlist: 10 }),
},
});
console.log("--- Create Index in Collection ---");
- Searches the collection:
// need load collection before search
const loadCollectionRes = await milvusClient.loadCollectionSync({
collection_name: collectionName,
});
console.log("--- Load collection (" + collectionName + ") ---", loadCollectionRes);
const result = await milvusClient.search({
collection_name: collectionName,
vectors: [vectorsData[0]["float_vector"]],
search_params: {
anns_field: "float_vector",
topk: "4",
metric_type: "L2",
params: JSON.stringify({ nprobe: 1024 }),
round_decimal: 4,
},
output_fields: ["count"],
vector_type: DataType.FloatVector,
});
console.log("--- Search collection (" + collectionName + ") ---", result);
- Releases the collection:
const releaseRes = await milvusClient.releaseCollection({
collection_name: collectionName,
});
console.log("--- Release Collection ---", releaseRes);
- Drops the collection:
const dropRes = await milvusClient.dropCollection({
collection_name: collectionName,
});
console.log("--- Drop Collection ---", dropRes);
5. Compile the file
npx tsc MilvusHello.ts
6. Run the example
node MilvusHello.js
Congratulations! You have successfully booted Milvus Standalone and created your first collection.