Vector Search
Relevant source files
The following files were used as context for generating this wiki page:
Vector Search is a core functionality of the Milvus Rust SDK that allows users to perform Approximate Nearest Neighbor (ANN) searches, hybrid searches combining multiple vector fields, and structured data queries. It leverages high-performance gRPC communications to interact with the Milvus server, supporting complex filtering expressions, various consistency levels, and paginated results via iterators.
Sources: src/query.rs:1-15, src/iterator.rs:242-260
Search Architecture and Data Flow
The search process begins with a client-side request that encapsulates query vectors, search parameters, and optional filters. The SDK serializes these inputs into gRPC-compatible protobuf messages, specifically SearchRequest or HybridSearchRequest, before transmitting them to the Milvus proxy.
Search Execution Flow
The following diagram illustrates the sequence of operations performed when a user initiates a vector search.
Sources: src/query.rs:596-646, src/query.rs:777-790
Vector Search Components
ANN Search Requests
Individual search operations are defined by the AnnSearchRequest struct. This is primarily used in hybrid search scenarios where multiple vector fields are queried simultaneously.
| Field | Type | Description |
|---|---|---|
data | Vec<Value> | The query vector data (Float or Binary). |
anns_field | String | The specific vector field name to target. |
param | Vec<KeyValuePair> | Search-specific params (e.g., nprobe, metric_type). |
limit | usize | Maximum number of results for this specific request. |
expr | Option<String> | Boolean expression to filter entities. |
Sources: src/query.rs:65-83, src/proto/milvus.proto.milvus.rs:885-899
Search Options
The SearchOptions struct provides a builder-like interface to configure search behavior for the client.search method.
let options = SearchOptions::new()
.limit(10)
.output_fields(vec!["id".to_string(), "age".to_string()])
.add_param("metric_type", "L2")
.add_param("anns_field", "picture");
Sources: examples/query_search.rs:64-70, src/query.rs:434-450
Hybrid Search and Ranking
Hybrid search allows for multi-vector field queries, merging results through ranking algorithms. The SDK provides two primary rankers:
- WeightedRanker: Applies user-defined weights to different search requests.
- RrfRanker: Uses Reciprocal Rank Fusion (RRF) with a configurable
kfactor (defaulting to 60).
Sources: src/query.rs:200-264, src/query.rs:718-765
Data Query and Retrieval
The query method is used for metadata-based filtering without vector similarity. It supports boolean expressions and template-based parameterization to prevent injection and simplify complex logic.
Query Options and Consistency
The SDK supports multiple consistency levels to balance performance and data freshness:
- Strong: Ensures all operations see the latest data (
guarantee_timestamp = 0). - Bounded: Provides bounded staleness.
- Eventually: Provides eventual consistency for maximum performance.
- Session: Guarantees consistency within a single client session.
Sources: src/query.rs:31-50, src/query.rs:549-573
Advanced Search Patterns
Search Iterators
For handling large result sets that exceed gRPC message limits, the SearchIterator and QueryIterator provide a streaming interface. These iterators manage session timestamps and cursors internally to fetch data in batches.
| Parameter | Default | Description |
|---|---|---|
batch_size | 1000 | Number of entities fetched per internal gRPC call. |
limit | Unlimited | Total maximum entities to return across all iterations. |
iterator_cp_file | None | Optional file path to save/load progress checkpoints. |
Sources: src/iterator.rs:68-85, src/iterator.rs:253-271
Iterator Sequence Diagram
Summary
Vector Search in the Milvus Rust SDK is implemented as a robust, asynchronous system capable of handling single-vector ANN searches, complex multi-vector hybrid searches, and paginated data retrieval. By utilizing protobuf schemas and structured options, the SDK ensures type safety and high performance across diverse search workloads, from low-latency interactive queries to high-throughput batch processing via iterators.
Sources: src/query.rs:1-25, tests/aggressivehpctesting/phase_2_search_and_query.rs:115-140