This tutorial explores on using Node.js to create a collection in Milvus, an open-source vector database.
Prerequisites:
To follow along with this tutorial, ensure that you have the following prerequisites:
- Bacis JavaScript knowledge
- Installed Node.js 16 and above on your machine
- A running instance of Milvus
Setup the Project
Open your preferred code editor and create a new directory for your project. Next, navigate to the project directory using the following command line interface:
cd milvus_node
Finally, initialize a new Node.js project using the following command:
This creates a new “package.json” file in your project directory.
Install the Milvus SDK
Milvus provides an official Node.js SDK which we’ll use in this tutorial.
Install the SDK by running the following command in your project directory:
# or ...
yarn add @zilliz/milvus2-sdk-node
Connect to the Milvus Server
Create a new “app.js” file and add the code to connect to the Milvus server:
const address = "localhost:19530";
const ssl = false;
const milvusClient = new MilvusClient({address, ssl});
Create a Milvus Collection
In the “app.js” file, we can add the code to create a new Milvus collection as shown in the following example:
const params = {
collection_name: "film",
description: "Test film search",
fields: [
{
name: "film_intro",
description: "",
data_type: DataType.FloatVector,
dim: 2,
},
{
name: "film_id",
data_type: DataType.Int64,
is_primary_key: true,
description: "",
},
{
name: "film_title",
data_type: DataType.VarChar,
max_length: 256,
description: "",
},
{
name: "duration",
data_type: DataType.Float,
description: "",
},
],
enableDynamicField: true
};
The given code creates a collection code film with the fields to store the film information.
The fields in the collection schema are as follows:
- The film_intro as a FloatVector with a dimension of 2.
- The film_id field as VarChar, with a maximum length of 256 characters, represents the film’s title.
- The duration field is defined as a Float type which denotes the film’s duration.
The enableDynamicField property is set to true to allow the dynamic field addition to the collection.
Once done, we can create the collection with the schema as shown in the following:
Conclusion
In this post, we learned how to use the Node.js Milvus SDK to create a basic collection to store the film data. You can explore the docs to learn more.