Skip to content
MilvusNode SDK
Sections

Milvus Code Examples and Tutorials

Markdown

Copy for agent

This guide provides comprehensive examples and tutorials for common use cases.

import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({
address: 'localhost:19530',
});
await client.connectPromise;
// Create collection
const schema = [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
data_type: DataType.FloatVector,
dim: 128,
},
{
name: 'text',
data_type: DataType.VarChar,
max_length: 256,
},
];
await client.createCollection({
collection_name: 'my_collection',
fields: schema,
});
// Insert data
const insertResult = await client.insert({
collection_name: 'my_collection',
data: [
{
vector: Array.from({ length: 128 }, () => Math.random()),
text: 'Hello Milvus',
},
{
vector: Array.from({ length: 128 }, () => Math.random()),
text: 'Vector database',
},
],
});
if (insertResult.status.error_code !== 'Success') {
throw new Error(insertResult.status.reason);
}
if (!('int_id' in insertResult.IDs)) {
throw new Error('Expected Int64 primary keys');
}
const ids = insertResult.IDs.int_id.data.map(String);
console.log('Inserted IDs:', ids);
// Create index
await client.createIndex({
collection_name: 'my_collection',
field_name: 'vector',
index_type: 'HNSW',
metric_type: 'L2',
});
// Load collection
await client.loadCollectionSync({
collection_name: 'my_collection',
});
// Search
const searchResults = await client.search({
collection_name: 'my_collection',
data: [Array.from({ length: 128 }, () => Math.random())],
limit: 5,
output_fields: ['text'],
});
console.log('Search results:', searchResults.results);
// Query
const queryResults = await client.query({
collection_name: 'my_collection',
expr: 'text == "Hello Milvus"',
output_fields: ['id', 'text'],
});
console.log('Query results:', queryResults.data);
// Delete
const deleteResult = await client.delete({
collection_name: 'my_collection',
ids: [ids[0]],
});
console.log('Deleted rows:', deleteResult.delete_cnt);
// Drop collection
await client.dropCollection({
collection_name: 'my_collection',
});
await client.closeConnection();
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({
address: 'localhost:19530',
});
await client.connectPromise;
// Create collection for image embeddings
await client.createCollection({
collection_name: 'image_collection',
fields: [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'image_embedding',
data_type: DataType.FloatVector,
dim: 512,
},
{
name: 'image_url',
data_type: DataType.VarChar,
max_length: 512,
},
],
});
// Insert image embeddings
await client.insert({
collection_name: 'image_collection',
data: [
{
image_embedding: Array.from({ length: 512 }, () => Math.random()),
image_url: 'https://example.com/image1.jpg',
},
// ... more images
],
});
// Create index
await client.createIndex({
collection_name: 'image_collection',
field_name: 'image_embedding',
index_type: 'HNSW',
metric_type: 'COSINE',
});
// Load and search
await client.loadCollectionSync({
collection_name: 'image_collection',
});
const queryEmbedding = Array.from({ length: 512 }, () => Math.random());
const results = await client.search({
collection_name: 'image_collection',
data: [queryEmbedding],
limit: 10,
output_fields: ['image_url'],
});
console.log('Similar images:', results.results);
await client.dropCollection({ collection_name: 'image_collection' });
await client.closeConnection();
import {
ConsistencyLevelEnum,
DataType,
MilvusClient,
} from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({ address: 'localhost:19530' });
const textVector = Array.from({ length: 768 }, () => Math.random());
const imageVector = Array.from({ length: 512 }, () => Math.random());
// Create collection with multiple vector fields
await client.createCollection({
collection_name: 'hybrid_collection',
fields: [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'text_vector',
data_type: DataType.FloatVector,
dim: 768,
},
{
name: 'image_vector',
data_type: DataType.FloatVector,
dim: 512,
},
],
});
await client.insert({
collection_name: 'hybrid_collection',
data: [{ text_vector: textVector, image_vector: imageVector }],
});
await client.flushSync({ collection_names: ['hybrid_collection'] });
// Create indexes for both vectors
await client.createIndex({
collection_name: 'hybrid_collection',
field_name: 'text_vector',
index_type: 'HNSW',
metric_type: 'L2',
});
await client.createIndex({
collection_name: 'hybrid_collection',
field_name: 'image_vector',
index_type: 'HNSW',
metric_type: 'L2',
});
// Load collection
await client.loadCollectionSync({
collection_name: 'hybrid_collection',
});
// Hybrid search
const results = await client.hybridSearch({
collection_name: 'hybrid_collection',
data: [
{
data: textVector,
anns_field: 'text_vector',
},
{
data: imageVector,
anns_field: 'image_vector',
},
],
rerank: { strategy: 'rrf', params: { k: 60 } },
consistency_level: ConsistencyLevelEnum.Strong,
limit: 10,
});
console.log('Hybrid search results:', results.results);
await client.dropCollection({ collection_name: 'hybrid_collection' });
await client.closeConnection();
// Create collection with dynamic fields enabled
await client.createCollection({
collection_name: 'dynamic_collection',
fields: [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
data_type: DataType.FloatVector,
dim: 128,
},
],
enable_dynamic_field: true,
});
// Insert data with dynamic fields
await client.insert({
collection_name: 'dynamic_collection',
data: [
{
vector: Array.from({ length: 128 }, () => Math.random()),
category: 'tech', // Dynamic field
tags: ['ai', 'ml'], // Dynamic field
metadata: {
// Dynamic field
author: 'John Doe',
date: '2024-01-01',
},
},
],
});
// Create collection with partition key
await client.createCollection({
collection_name: 'time_partitioned',
fields: [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
data_type: DataType.FloatVector,
dim: 128,
},
{
name: 'timestamp',
data_type: DataType.Int64,
is_partition_key: true, // Partition key
},
],
});
// Insert data (automatically partitioned by timestamp)
await client.insert({
collection_name: 'time_partitioned',
data: [
{
vector: Array.from({ length: 128 }, () => Math.random()),
timestamp: Date.now(),
},
],
});
// Create database
await client.createDatabase({
db_name: 'production_db',
});
// Create client for specific database
const prodClient = new MilvusClient({
address: 'localhost:19530',
database: 'production_db',
});
// Create collection in production database
await prodClient.createCollection({
collection_name: 'prod_collection',
fields: [
{ name: 'id', data_type: DataType.Int64, is_primary_key: true },
{ name: 'vector', data_type: DataType.FloatVector, dim: 4 },
],
});
// List databases
const databases = await client.listDatabases();
console.log('Databases:', databases.db_names);
await prodClient.dropCollection({ collection_name: 'prod_collection' });
await prodClient.closeConnection();
await client.dropDatabase({ db_name: 'production_db' });
const client = new MilvusClient({
address: 'localhost:19530',
username: 'root',
password: 'Milvus',
});
await client.connectPromise;
const client = new MilvusClient({
address: 'your-endpoint.zillizcloud.com:443',
token: 'your-api-key',
ssl: true,
});
await client.connectPromise;
app/api/search/route.ts
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
import { NextResponse } from 'next/server';
const client = new MilvusClient({
address: process.env.MILVUS_ADDRESS,
token: process.env.MILVUS_TOKEN,
});
export async function POST(request) {
const { vector } = await request.json();
const results = await client.search({
collection_name: 'my_collection',
data: [vector],
limit: 10,
});
if (results.status.error_code !== 'Success') {
return NextResponse.json({ error: results.status.reason }, { status: 502 });
}
return NextResponse.json({ results: results.results });
}

Install @langchain/community, @langchain/core, and the Milvus SDK. This example uses deterministic vectors so you can test the integration locally without an embedding service. Use your embedding model for real documents.

import { Milvus } from '@langchain/community/vectorstores/milvus';
const embed = (text) => (text.includes('vector') ? [1, 0] : [0, 1]);
const embeddings = {
embedDocuments: async (texts) => texts.map(embed),
embedQuery: async (text) => embed(text),
};
const vectorStore = await Milvus.fromTexts(
[
'Milvus stores vector embeddings',
'LangChain connects application components',
],
[{ topic: 'milvus' }, { topic: 'langchain' }],
embeddings,
{
url: process.env.MILVUS_ADDRESS ?? 'localhost:19530',
clientConfig: { token: process.env.MILVUS_TOKEN },
collectionName: 'langchain_collection',
}
);
const results = await vectorStore.similaritySearch('vector search', 1);
console.log(results.map((document) => document.pageContent));
const client = new MilvusClient({
address: 'your-endpoint.zillizcloud.com:443',
token: 'your-api-key',
ssl: true,
timeout: 60000,
maxRetries: 5,
});
await client.connectPromise;
const { data, status } = await client.listCollections();
if (status.error_code !== 'Success') throw new Error(status.reason);
console.log(data.map((collection) => collection.name));
await client.closeConnection();
async function safeOperation(operation) {
try {
return await operation();
} catch (error) {
if (error.error_code === 'CollectionNotExists') {
console.error('Collection does not exist');
} else if (error.error_code === 'IllegalArgument') {
console.error('Invalid argument:', error.reason);
} else {
console.error('Unexpected error:', error);
}
throw error;
}
}
// Usage
await safeOperation(() =>
client.createCollection({
collection_name: 'my_collection',
fields: schema,
})
);