Skip to main content

Single Query

Retrieves the nearest neighbors for a given query vector.

Parameters

If embedding auto-generation is enabled (by setting the embedding_model parameter in create_index(), then the query_contents parameter can be used instead of query_vectors.
filters use a subset of the MongoDB Query and Projection Operators. For instance: filters: { "$and": [ { "label": "cat" }, { "confidence": { "$gte": 0.9 } } ] } means that only vectors where label == "cat" and confidence >= 0.9 will be considered for encrypted vector search. For more info on metadata, see Metadata Filtering.

Returns

List[Dict[str, Union[int, float, Dict[]]]]: List of results for the query vector. Each result is a list of top_k dictionaries, each containing id (always included), and optionally distance and metadata based on include. When include is empty (the default), only id is returned.

Exceptions

  • Throws if the query vector has incompatible dimensions with the index.
  • Throws if the index was not created or loaded yet.
  • Throws if the query could not be executed.

Example Usage

Single Query with Distances:
Single Query without Distances:
Single Query with Metadata:

Batched Queries

Retrieves the nearest neighbors for one or more query vectors.

Parameters

filters use a subset of the MongoDB Query and Projection Operators. For instance: filters: { "$and": [ { "label": "cat" }, { "confidence": { "$gte": 0.9 } } ] } means that only vectors where label == "cat" and confidence >= 0.9 will be considered for encrypted vector search. For more info on metadata, see Metadata Filtering.

Returns

List[List[Dict[str, Union[int, float, Dict[]]]]]: List of results for each query vector. Each result is a list of top_k dictionaries, each containing id (always included), and optionally distance and metadata based on include. When include is empty (the default), only id is returned.

Exceptions

  • Throws if the query vectors have incompatible dimensions with the index.
  • Throws if the index was not created or loaded yet.
  • Throws if the query could not be executed.

Example Usage

Batch Query with Distances:

NumPy Query

A high-performance query variant that returns only integer IDs as a NumPy array. This is optimized for performance-critical workloads where metadata and distances are not needed.

Parameters

Returns

np.ndarray: A NumPy array of integer IDs for the nearest neighbors. For a single query, returns a 1D array of shape (top_k,). For batch queries, returns a 2D array of shape (num_queries, top_k).

Exceptions

  • Throws if the query vectors have incompatible dimensions with the index.
  • Throws if the index was not created or loaded yet.
  • Throws if the query could not be executed.

Example Usage

query_numpy is a high-performance variant that returns only integer IDs. Use the standard query() method if you need distances, metadata, or string IDs.