Milvus Node.js SDK

repository·main·Indexed 41 days ago

https://github.com/milvus-io/milvus-sdk-node

The official Node.js SDK (@zilliz/milvus2-sdk-node) for the Milvus vector database. It provides gRPC and HTTP clients for vector similarity search, metadata filtering, and full collection, index, and user management. Supports integration with LangChain.js and features like BulkWriter for large-scale offline imports, hybrid search, and dynamic schemas.

Tokens
104.4K
Snippets
317
Records
394
Agent score
48%

What's inside Milvus

  1. Browse the Milvus Node.js SDK API Reference

    main

    The Milvus Node.js SDK API reference is organized into several functional areas to help you find specific method signatures and configurations. Use the following categories to navigate the API:

    • Client: Access MilvusClient for core SDK entry points and HttpClient for REST-specific configurations and HTTP-only operations.
    • Operations: Grouped by resource type, including:
      • Collection: Manage lifecycle (create, load, release, rename, etc.).
      • Data: Perform CRUD and search (insert, upsert, query, search, delete, bulk import).
      • Index: Manage vector and scalar indexes.
      • Partition: Manage collection partitions.
      • Database: Manage database instances.
      • User & Role: Manage access control (users, passwords, roles, privileges).
      • Resource Group: Manage capacity and node transfers.
      • System: Health checks, metrics, and analyzer management.
      • BulkWriter: Client-side JSON/Parquet bulk file preparation.
    • Types: Reference common data types, error codes, and response shapes.
  2. Understand what data is collected by Client Telemetry

    main

    Client telemetry tracks the following information:

    • Operation Metrics: Request, success, and error counts, plus average, p99, and maximum latency for supported data operations. High-level operations include their retries in a single logical operation. Note that iterator setup and batches are excluded.
    • Heartbeat Metadata: SDK version, client identity, hostname, user, and database context.
    • Error Details: Operation, collection, request ID, and error text.

    Important Notes:

    • Sampling: The samplingRate reduces the number of recorded operations rather than extrapolating counts.
    • Latency: Metrics arrive in windows and do not provide an instantaneous view of requests.
    • Server Control: The server can request metrics, inspect configuration, or push changes to your enabled state, heartbeatIntervalMs, or samplingRate. If the server pushes a 'disabled' state, the heartbeat control channel remains active.
  3. How exponential backoff works in the SDK

    main

    The SDK uses an exponential backoff formula to space out retry attempts, preventing the client from overwhelming the server during periods of instability.

    The formula used is: delay = 2^retryCount × retryDelay

    For example, with a retryDelay of 10ms:

    • Attempt 1: 2¹ × 10ms = 20ms delay
    • Attempt 2: 2² × 10ms = 40ms delay
    • Attempt 3: 2³ × 10ms = 80ms delay
  4. Use Molecular Data types

    main

    Milvus 3.0 supports molecular data through a new DataType.Mol type. This includes support for molecular function types like MinHash and MolFingerprint.

    When working with molecular data, the SDK handles mol_data (arrays of bytes) and mol_smiles_data (arrays of strings) within scalar field payloads. The SDK provides value decoding for these fields in query and search results.

  5. Construct filter expressions for queries

    main

    Filter expressions are used in search(), query(), and delete() methods. They support comparison operators, logical operators, IN clauses, string matching with LIKE, array operations, and JSON field access. You can also use parameterized template expressions to prevent injection and improve readability.

    // Comparison
    age > 18
    price <= 99.9
    status == "active"
    name != "test"
    
    // Logical operators
    age > 18 AND status == "active"
    price < 10 OR price > 100
    NOT (status == "deleted")
    
    // IN operator
    id IN [1, 2, 3]
    category IN ["books", "music"]
    
    // String matching
    title LIKE "hello%"         # Starts with "hello"
    title LIKE "%world"         # Ends with "world"
    
    // Array operations
    ARRAY_CONTAINS(tags, "ai")
    ARRAY_LENGTH(tags) > 3
    
    // JSON field access
    metadata["key"] == "value"
    metadata["nested"]["field"] > 10
    
    // Template expressions (parameterized)
    await client.query({
      collection_name: 'test',
      filter: 'age > {min_age}',
      filter_params: { min_age: 18 },
    });
  6. How deadline handling works during retries

    main
    To ensure consistent timeout behavior, each retry attempt resets the deadline. However, the original timeout duration is preserved across all retry attempts to ensure the total time spent on the request does not exceed the user's intended limit.
  7. Use MilvusClient sessions for cluster-specific operations

    main

    A session allows you to create a lightweight DQL session pinned to a specific clusterId. The session reuses the parent client's connection but injects the cluster_id into supported requests.

    Lifecycle Note: Closing a session via session.close() prevents future session calls but does not close the parent MilvusClient or the gRPC channel pool.

    Supported Session Methods:

    • search(params)
    • hybridSearch(params)
    • searchIterator(params)
    • query(params)
    • queryIterator(params)
    • get(params)
    • close()
    const session = client.session('cluster-a');
    
    const results = await session.search({
      collection_name: 'books',
      data: [[0.1, 0.2, 0.3, 0.4]],
      anns_field: 'vector',
      limit: 10,
    });
    
    session.close();
  8. Complex Types: Array and Struct

    main

    For highly structured or nested data, Milvus supports Array and Struct types.

    Array

    An Array is a collection of scalar values. Requires element_type and max_capacity.

    Struct

    A Struct is a nested structure. Requires element_type (which is an object defining the child fields).

    StructArray (Milvus 3.0+)

    A StructArray is an Array where each element follows a nested Struct schema. This allows storing arrays of complex objects.

    Using StructArray in Queries:

    • Use bracketed paths for child fields: features[label].
    • Use element_filter for predicates matching values within the same array element: element_filter(features, $[score] >= 90).
    • To add a StructArray to an existing collection, set nullable: true to maintain compatibility with existing rows.
    // StructArray definition
    {
      name: 'features',
      data_type: DataType.Array,
      element_type: DataType.Struct,
      max_capacity: 16,
      nullable: true,
      fields: [
        { name: 'label', data_type: DataType.VarChar, max_length: 64 },
        { name: 'score', data_type: DataType.Int32 },
        { name: 'active', data_type: DataType.Bool },
        { name: 'embedding', data_type: DataType.FloatVector, dim: 128 },
      ],
    }
    
    // Querying a StructArray
    const rows = await client.query({
      collection_name: 'documents',
      filter: 'element_filter(features, $[score] >= 90 AND $[active] == true)',
      output_fields: ['id', 'features[label]', 'features[score]'],
    });
  9. Understand scalar and vector value types

    main

    When inserting or querying data, values must match the expected TypeScript types.

    Scalar Types:

    • Bool: boolean
    • Int8, Int16, Int32, Int64, Float, Double: number
    • VarChar: string
    • JSON: { [key: string]: any }
    • Timestamptz: string

    Vector Types:

    • FloatVector: number[]
    • Float16Vector / BFloat16Vector: number[] | Uint8Array
    • BinaryVector: number[]
    • Int8Vector: number[] | Int8Array
    • SparseFloatVector: Can be SparseVectorArray ((number | undefined)[]), SparseVectorDic ({ [key: string]: number }), SparseVectorCSR ({ indices: number[]; values: number[] }), or SparseVectorCOO ({ index: number; value: number }[]).
  10. Use Milvus Management and Visualization Tools

    main

    Enhance your development workflow with these related tools:

    Attu (GUI for Milvus)

    Attu provides a modern graphical user interface for:

    • Visual collection management
    • Data browsing and editing
    • Query and search interfaces
    • Performance monitoring

    Feder (Index Visualization)

    Feder is used for visualizing ANN (Approximate Nearest Neighbor) indexes, providing:

    • Index structure visualization
    • Performance analysis
    • Index optimization tools
  11. Understand the default partition

    main

    Every Milvus collection contains a default partition named _default. If you perform an insert operation without explicitly providing a partition_name, the data is automatically routed to the _default partition.

    // Data goes to _default partition
    await client.insert({
      collection_name: 'my_collection',
      data: [
        /* ... */
      ],
    });
    
    // Explicitly specify partition
    await client.insert({
      collection_name: 'my_collection',
      partition_name: 'partition_1',
      data: [
        /* ... */
      ],
    });