Advanced Features
This guide covers advanced features including connection pooling, retry mechanisms, error handling, logging, and iterator patterns.
Connection Pooling
Section titled “Connection Pooling”The SDK uses connection pooling to manage gRPC connections efficiently.
Configuration
Section titled “Configuration”const client = new MilvusClient({ address: 'localhost:19530', pool: { min: 2, // Minimum pool size max: 10, // Maximum pool size idleTimeoutMillis: 30000, // Idle timeout },});Benefits
Section titled “Benefits”- Efficiency: Reuses connections instead of creating new ones
- Performance: Reduces connection overhead
- Resource management: Limits concurrent connections
Retry Mechanisms
Section titled “Retry Mechanisms”Configure automatic retries for failed operations.
Configuration
Section titled “Configuration”const client = new MilvusClient({ address: 'localhost:19530', maxRetries: 3, // Maximum retry attempts retryDelay: 1000, // Delay between retries (ms)});Retry Behavior
Section titled “Retry Behavior”The SDK automatically retries on:
- Network errors
- Transient server errors
- Timeout errors
Error Handling
Section titled “Error Handling”Error Types
Section titled “Error Types”import { ErrorCode } from '@zilliz/milvus2-sdk-node';
try { await client.createCollection({ /* ... */ });} catch (error) { switch (error.error_code) { case ErrorCode.CollectionNotExists: console.error('Collection does not exist'); break; case ErrorCode.IllegalArgument: console.error('Invalid argument'); break; case ErrorCode.UnexpectedError: console.error('Unexpected error'); break; default: console.error('Error:', error); }}Common Error Codes
Section titled “Common Error Codes”Success: Operation succeededCollectionNotExists: Collection not foundIllegalArgument: Invalid parameterIndexNotExist: Index not foundUnexpectedError: Unexpected server errorRateLimit: Rate limit exceededSchemaMismatch: Schema mismatch
Error Response Structure
Section titled “Error Response Structure”{ error_code: 'CollectionNotExists', reason: 'Collection my_collection does not exist', // ... other fields}Logging and Debugging
Section titled “Logging and Debugging”Log Levels
Section titled “Log Levels”Configure logging levels:
const client = new MilvusClient({ address: 'localhost:19530', logLevel: 'debug', // 'error', 'warn', 'info', 'debug' logPrefix: 'milvus-client',});Log Levels
Section titled “Log Levels”- error: Only error messages
- warn: Warnings and errors
- info: Informational messages, warnings, and errors
- debug: All messages including debug information
Debugging
Section titled “Debugging”Enable trace for detailed debugging:
const client = new MilvusClient({ address: 'localhost:19530', trace: true,});Request Tracing (TraceID)
Section titled “Request Tracing (TraceID)”Track and trace individual requests using trace IDs for debugging and monitoring purposes.
Overview
Section titled “Overview”The SDK automatically adds a timestamp (client-request-unixmsec) to every request. You can optionally provide a client_request_id (or client-request-id) to track specific requests across your application.
Automatic Timestamp
Section titled “Automatic Timestamp”Every request automatically includes a client-request-unixmsec timestamp:
// This request will have client-request-unixmsec automatically addedawait client.createCollection({ collection_name: 'my_collection', fields: [...], // Timestamp is added automatically, no configuration needed});Using TraceID
Section titled “Using TraceID”Add a trace ID to track specific requests:
// Recommended: use client_request_id (JavaScript/TypeScript convention)const traceId = `trace-${Date.now()}`;await client.createCollection({ collection_name: 'my_collection', fields: [...], client_request_id: traceId,});
// Alternative: use client-request-id (compatible with pymilvus)await client.createCollection({ collection_name: 'my_collection', fields: [...], 'client-request-id': traceId,});Note: If both formats are provided, client_request_id takes priority (JavaScript/TypeScript convention).
Supported Operations
Section titled “Supported Operations”All API methods that extend GrpcTimeOut support trace IDs:
// Insert with trace IDawait client.insert({ collection_name: 'my_collection', data: [ { id: 1, vector: [0.1, 0.2, 0.3] }, ], client_request_id: 'insert-trace-123',});
// Search with trace IDawait client.search({ collection_name: 'my_collection', vector: [0.1, 0.2, 0.3], limit: 10, client_request_id: 'search-trace-456',});
// Query with trace IDawait client.query({ collection_name: 'my_collection', expr: 'id > 100', client_request_id: 'query-trace-789',});
// All other operations support trace IDsawait client.createIndex({ collection_name: 'my_collection', field_name: 'vector', index_type: 'IVF_FLAT', client_request_id: 'index-trace-101',});Use Cases
Section titled “Use Cases”- Request Tracking: Track requests across distributed systems
- Debugging: Identify specific requests in logs
- Performance Monitoring: Correlate trace IDs with performance metrics
- Error Investigation: Trace errors back to specific requests
Implementation Details
Section titled “Implementation Details”- Trace IDs are passed through gRPC metadata
- Both
client_request_idandclient-request-idformats are supported - Trace IDs are automatically extracted from request parameters
- The timestamp
client-request-unixmsecis automatically added by the interceptor - No need to modify API method implementations
Iterator Patterns
Section titled “Iterator Patterns”Query Iterator
Section titled “Query Iterator”Iterate through large query results:
const iterator = await client.queryIterator({ collection_name: 'my_collection', expr: 'age > 25', output_fields: ['id', 'text'], batch_size: 100,});
while (true) { const batch = await iterator.next(); if (batch.done) break;
console.log('Batch:', batch.value);}Search Iterator
Section titled “Search Iterator”Iterate through large search results:
const iterator = await client.searchIterator({ collection_name: 'my_collection', data: [[0.1, 0.2, 0.3, 0.4]], limit: 10000, batch_size: 100,});
while (true) { const batch = await iterator.next(); if (batch.done) break;
console.log('Batch:', batch.value);}Sparse Vectors
Section titled “Sparse Vectors”Work with sparse vector data:
// Array formatconst sparseVector = [1.0, undefined, undefined, 2.5, undefined];
// Dictionary formatconst sparseVector = { '0': 1.0, '3': 2.5 };
// CSR formatconst sparseVector = { indices: [0, 3], values: [1.0, 2.5],};
// COO formatconst sparseVector = [ { index: 0, value: 1.0 }, { index: 3, value: 2.5 },];
await client.insert({ collection_name: 'my_collection', data: [ { sparse_vector: sparseVector, }, ],});Binary Vectors
Section titled “Binary Vectors”Work with binary vector data:
// Binary vector: length must be dim / 8const binaryVector = [1, 0, 1, 0, 1, 0, 1, 0]; // For dim=64
await client.insert({ collection_name: 'my_collection', data: [ { binary_vector: binaryVector, }, ],});Float16/BFloat16 Vectors
Section titled “Float16/BFloat16 Vectors”Use transformers for Float16 and BFloat16 vectors:
import { f32ArrayToF16Bytes } from '@zilliz/milvus2-sdk-node';
await client.insert({ collection_name: 'my_collection', data: [ { vector: [0.1, 0.2, 0.3, 0.4], // f32 array }, ], transformers: { vector: (data) => f32ArrayToF16Bytes(data), },});Dynamic Fields
Section titled “Dynamic Fields”Enable dynamic fields for flexible schemas:
// Create collection with dynamic fieldsawait client.createCollection({ collection_name: 'my_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 fieldsawait client.insert({ collection_name: 'my_collection', data: [ { vector: [/* ... */], dynamic_field_1: 'value1', // Automatically added dynamic_field_2: 123, // Automatically added }, ],});Performance Monitoring
Section titled “Performance Monitoring”Get Metrics
Section titled “Get Metrics”Get system metrics:
const metrics = await client.getMetric({ request: { metric_type: 'system_info', },});
console.log('Metrics:', metrics);Replica Management
Section titled “Replica Management”Get Replicas
Section titled “Get Replicas”Get replica information:
const replicas = await client.getReplicas({ collection_name: 'my_collection',});
console.log('Replicas:', replicas.replicas);Best Practices
Section titled “Best Practices”- Connection pooling: Configure appropriate pool sizes
- Retry configuration: Set reasonable retry limits
- Error handling: Always handle errors appropriately
- Logging: Use appropriate log levels for your environment
- Iterators: Use iterators for large result sets
- Monitoring: Monitor metrics for performance optimization
Next Steps
Section titled “Next Steps”- Learn about Best Practices
- Explore HTTP Client
- Check out Examples & Tutorials