query()
Perform vector similarity search with optional filtering and re-ranking. This is the primary method for semantic search in Zvec.Signature
Parameters
One or more vector queries to execute. Each
VectorQuery specifies:- The vector field to search
- The query vector (or document ID to use as query)
- Optional index-specific parameters (e.g.,
effor HNSW)
VectorQuery must be provided.Number of nearest neighbors to return. Must be > 0.
Boolean expression to pre-filter candidates before similarity search.Examples:
"category == 'tech'""price < 100 AND rating >= 4.5""tags ARRAY_CONTAINS 'python'"
Whether to include vector data in the results. Set to
false to reduce memory usage if you only need scalar fields.List of scalar field names to return. If
None, all fields are included.Use this to reduce response size when you only need specific fields.A re-ranking function to refine results after the initial vector search.See Re-ranking for available options.
Returns
List of matching documents sorted by relevance score (highest first).Each
Doc contains:id: Document IDscore: Relevance score (higher is more similar)fields: Scalar metadata fieldsvectors: Vector embeddings (ifinclude_vector=True)
Basic Example
With Filtering
Pre-filter candidates using boolean expressions:Query by Document ID
Use an existing document’s vector as the query:VectorQuery
Defines a single vector search query.Constructor
Name of the vector field to search.
Document ID whose vector should be used as the query. Mutually exclusive with
vector.Explicit query vector. Mutually exclusive with
id.Index-specific search parameters to control accuracy vs. speed tradeoffs.
Example with Query Parameters
Query Parameters
HnswQueryParam
Tune HNSW index search behavior:Number of neighbors to explore during search. Higher values improve recall but increase latency.
- Low ef (32-64): Faster, lower recall
- Medium ef (64-128): Balanced
- High ef (128-512): Slower, higher recall
IVFQueryParam
Tune IVF index search behavior:Number of clusters (partitions) to search. Higher values improve recall but increase latency.
- Low nprobe (1-5): Faster, lower recall
- Medium nprobe (5-20): Balanced
- High nprobe (20-100): Slower, higher recall
Hybrid Search
Search multiple vector fields simultaneously:fetch()
Retrieve documents by ID. This is a direct lookup operation, not a similarity search.Signature
Parameters
One or more document IDs to retrieve.
Returns
Dictionary mapping document IDs to
Doc objects. Missing IDs are omitted from the result.