AI

Create a Milvus Collection Using Node.js

Milvus is designed to store and query the large-scale vector data which makes it ideal for tasks such as similarity search, recommendation systems, and more.

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:

mkdir milvus_node
cd milvus_node

Finally, initialize a new Node.js project using the following command:

npm init -y

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:

npm install @zilliz/milvus2-sdk-node

# 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:

import { MilvusClient } from "@zilliz/milvus2-sdk-node";

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:

import { DataType } from "@zilliz/milvus2-sdk-node";

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:

  1. The film_intro as a FloatVector with a dimension of 2.
  2. The film_id field as VarChar, with a maximum length of 256 characters, represents the film’s title.
  3. 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:

await milvusClient.createCollection(params);

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list