Skip to main content

Single Query

Retrieves the nearest neighbors for a given query vector. Passing text alongside query_vectors turns this into a hybrid query that fuses BM25 full-text ranking with the vector ranking (see Full-text and hybrid search).

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.
Hybrid queries still require a query vector — including at alpha=0, where it only fixes the batch shape. For text search with no vector at all, use query_metadata(text=...).
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.
Hybrid queries narrow which filters are accepted. $regex on a non-pattern field, and any predicate on an explicitly non-filterable field, work in a pure query() but are rejected once text is set, because the text leg can only be filtered on fields resolvable from the encrypted metadata index. Move the filter to a pattern / filterable field or drop text.

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 (pure vector query), score (hybrid query — fused BM25 + vector relevance, larger = more relevant), and metadata based on include. A hybrid result carries score in place of distance — never both, since a text-only match has no vector distance. 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 text is passed but the index has no full_text field.
  • Throws if text_fields names a field that is not full_text.
  • Throws if any text_* knob is set without a non-empty text.
  • Throws if rrf_k <= 0 or window_mult < 1 is passed explicitly (pass None for defaults).
  • Throws if a filter references a field the index cannot resolve when text is set (e.g. $regex on a non-pattern field, or any predicate on an explicitly non-filterable field).
  • Throws if the query could not be executed.
  • Throws if query_contents needs embedding and the index’s built-in model cannot be downloaded or loaded (offline cache miss, network failure). Often transient.

Example Usage

Single Query with Distances:
Hybrid Query (BM25 + Vector): Requires an index created with at least one full_text field (see Full-text and hybrid search).
Single Query without Distances:
Single Query with Metadata:
Results are plain dictionaries — access fields by string key (r['id'], r['distance'], r['metadata']):

Index key

index_key is required on every call. Pass the same 32-byte index KEK you used with create_index() / load_index(). For an RBAC user, pass the user’s KEK as index_key= together with their user_id=:

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.
Batched queries accept the same BM25 keyword-only params as the single-query form (text, text_fields, text_field_weights, require_all_terms, alpha, rrf_k, window_mult); the same text value applies to every row in the batch.

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 (pure vector query), score (hybrid query), 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). Slots beyond the number of results found, and IDs that cannot be converted to integers, are set to -1.

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.
  • Throws if the query_vectors format does not match the batch parameter.

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.