Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vector Search

Vectors are a native field type in rhypedb, not an add-on. You declare a Vector<N> field, optionally index it for approximate nearest-neighbour (ANN) search, and optionally have the server compute embeddings for you from text. Then you query it with .similar(...).

Declaring a vector field

type Post {
    body: String
    embedding: Vector<384>
}

Vector<N> stores an N-dimensional f32 vector. The dimension N is fixed by the schema. On its own, this field stores whatever vectors you write to it; to search it efficiently you add an index.

Two ways to get vectors in

1. Bring your own vectors

Compute embeddings in your application and write them like any other field:

Post.create({ body: "hello world", embedding: [0.12, -0.04, 0.98, ...] })

The vector literal must have exactly N elements. For bulk ingestion of precomputed vectors, the binary TCP protocol has a dedicated VectorBatch message (see the API Reference).

2. Server-side embedding with @vectorize

Let the server embed text for you. Point @vectorize at a String field and a model:

type Post {
    body: String
    embedding: Vector<384> @vectorize(source: "body", model: "all-MiniLM-L6-v2")
}

Now you only supply the text — the server fills in the vector on create and re-embeds on update:

Post.create({ body: "distributed systems are hard" })

The embedding is computed asynchronously by a background worker. Every write that sets the source field queues one — create, create_batch, and single- or multi-object update, over HTTP or TCP.

An update that doesn't touch the source field does not re-embed. The vector depends only on the source text, so an update that leaves body out keeps the existing vector as it is. Writing the source field again, even with the same text, does re-embed it.

The embedding job is recorded in the same transaction as the write, including every object a multi-object write transaction creates or updates. If the write commits, the object is durably marked as owing a vector, even if the process dies a moment later before the worker has run; the job is picked up again when the database next opens. If the object is rewritten while an older job for it is still in flight, the older job's vector is discarded and only the newest text is embedded.

Embedded users (calling rhypedb-engine directly) get the same behaviour by attaching their Vectorizer to the database once, before writing: db.attach_vectorizer(Arc::clone(&vectorizer)). There is no need to call Vectorizer::enqueue per write. A write made while no vectorizer is attached is still marked as owing a vector, and the next vectorizer opened over the database embeds it.

Which fields get embedded follows the database's current schema, including after a schema reload or a rename_field. A vectorizer builds its indexes when it is created, so if a reload adds a @vectorize field the running vectorizer doesn't know, writes to it are held as pending, not dropped: the vectorizer counts them in IndexingStatus::awaiting_reopen, logs the field once, and embeds them after the next restart.

GET /status reports how many embeddings are pending, how many vectors each index holds, and whether the embedding model is currently loaded:

{
  "vectorizer": {
    "pending": 3,
    "indexes": [ { "name": "Post.embedding", "vectors": 1000 } ],
    "model_loaded": true,
    "model_error": null
  }
}

model_loaded is true once the model has loaded successfully at least once. model_error carries the most recent load-failure message while one is in effect (for example, the model is still downloading, or this binary was built without the fastembed feature) and goes back to null as soon as a load succeeds. A model-load failure does not lose or fail any pending jobs — they stay queued and are retried with a backoff; see Embedding pipeline settings below.

The Vector<N> dimension must match the model's output size (e.g. all-MiniLM-L6-v2 produces 384-dimensional vectors).

Embedding pipeline settings

The background embed worker (and the query-time embedder used by a text .similar) can be tuned via the [vectorizer] table in rhypedb.toml (see Running rhypedb):

KeyDefaultWhat it costs
batch_size32Jobs claimed and embedded per pass. Higher batches trade memory for throughput — at 512-token texts, a batch of 256 (the old hard-coded value) could take a desktop process to 15 GB resident; 32 keeps peak memory bounded.
max_length256Token limit per text. MiniLM/BGE-small were trained at 256; attention memory grows with the square of this, so raising it is expensive.
intra_threadshalf the CPU coresONNX Runtime intra-op threads used by each lazily-loaded model.
quantizedtruePrefer the int8-quantized model variant when fastembed has one (all-MiniLM-L6-v2, bge-small-en-v1.5); smaller and faster, a small quality cost. Base/large BGE models have no quantized variant and always use fp32 regardless of this setting.
cpu_arenafalseUse ONNX Runtime's CPU memory arena. With it, a loaded model keeps every block it has allocated and never shrinks: about 600 MB of memory for all-MiniLM-L6-v2, against roughly 100–170 MB without it. Query embedding speed is the same either way; batch embedding (backfills, re-embeds) may be somewhat faster with the arena on. Turn it on when bulk-embedding throughput matters more than memory.
cache_dirfastembed's own defaultWhere downloaded model files are cached on disk.
cross_encoder"off""off" disables cross-encoder reranking of .similar text search; any other string names the reranker model to turn it on with (see Cross-encoder reranking below). A second model, off by default.
rerank_candidatesmin(k * 3, 48)ANN candidates the cross-encoder scores per text query — one forward pass each, so this is the dominant query cost when reranking is on. Never below k.
reranker_dirdownloadLoad the reranker from a local directory (model.onnx plus the four tokenizer files) instead of downloading it — for bundled deployments that must not touch the network.
reranker_fp32falseUse the full-precision bge-reranker-base (~1.1 GB) instead of the int8-quantized build (~280 MB, equivalent quality).

If the model fails to load — a network hiccup mid-download, a cold cache on first boot, or simply the time it takes to download and initialize a ~100MB+ ONNX model — the background worker does not fail or drop the affected jobs. It puts them back on the queue, records the failure under vectorizer.model_error on GET /status, and retries with an exponential backoff (starting at 2s, doubling up to a 60s cap) until a load succeeds, at which point model_error clears and the backoff resets. A .similar text query made while the model is unavailable gets a clear ModelUnavailable error rather than panicking: the first query after a failure attempts the load itself (and waits for that attempt, which for a download can be the network timeout); queries during the backoff window are refused immediately without retrying, so a known-bad model doesn't cost every query a download attempt. Embedded callers driving process_pending see the same failure as an Err(ModelUnavailable) when a batch could not be embedded at all (its jobs are re-queued), so a "loop until 0" driver can back off rather than spin.

Upgrading from a build before this setting existed: the cross-encoder used to run by default and was opted out of with RHYPEDB_DISABLE_RERANK. It is now off unless you set cross_encoder — a deployment that relied on the old default silently loses reranking after upgrading (queries still succeed; GET /status shows reranker_loaded: false). Add [vectorizer] cross_encoder = "bge-reranker-base" to keep it. RHYPEDB_DISABLE_RERANK is no longer read (the server warns at startup if it, or RHYPEDB_RERANKER_DIR with the cross-encoder off, is set).

Cross-encoder reranking

.similar text search can optionally run a SECOND pass over the ANN candidates: a cross-encoder model scores each candidate's source text directly against your query text, which typically ranks results more accurately than vector distance alone — at the cost of one extra model (~280MB) and one forward pass per candidate. It is off by default (cross_encoder = "off"); turn it on by naming the reranker model — today exactly one is supported:

[vectorizer]
cross_encoder = "bge-reranker-base"

This is a separate knob from the per-query rerank: argument (see Searching — .similar below) — rerank: N always means "re-score N candidates against the exact f32 vectors," a cheap step that is always available. The cross-encoder runs in addition to that, whenever it is turned on here AND the field has a readable @vectorize source text; a raw-vector .similar query never uses it (there is no query text to score against). Like the embedder, a cross-encoder load failure is fail-soft: .similar falls back to returning the ANN/rescored order rather than erroring, and the failure is visible under vectorizer.reranker_error on GET /status (vectorizer.reranker_loaded mirrors model_loaded).

The reranker is tuned by the [vectorizer] keys rerank_candidates, reranker_dir and reranker_fp32 (table above; a downloaded reranker shares cache_dir with the embedding models). Three environment variables predate those keys and are still honoured as fallbacks when the key is absent — RHYPEDB_RERANK_CANDIDATES, RHYPEDB_RERANKER_DIR, RHYPEDB_RERANKER_FP32 — prefer the config keys. RHYPEDB_DEBUG_RERANK (any value) logs the search path taken (brute-force / ANN / rerank pool) to stderr.

Indexing for search — @index(hnsw, ...)

To run similarity search at scale, add an HNSW index:

embedding: Vector<384> @vectorize(source: "body", model: "all-MiniLM-L6-v2")
                       @index(hnsw, metric: cosine, quantization: turboquant_4bit, m: 16, ef_construction: 200)
ParameterValuesDefaultNotes
metriccosine, l2, dot_productcosinedistance function
quantizationturboquant_2bit, turboquant_3bit, turboquant_4bitturboquant_4bitin-index compression
m≥ 1engine defaultgraph fan-out
ef_construction≥ 1engine defaultbuild-time accuracy/effort

The index stores quantized vectors (TurboQuant, 2–4 bits per component) for speed and a small memory footprint; the raw f32 vectors are kept losslessly at rest and are what get re-ranked and what survive a backup or logical export. quantization: none is rejected — there is always an index-side quantization.

The index is persisted as hnsw_<field>.bin in the data directory and rebuilt from the raw vectors if missing or stale (for example, after a logical import).

Searching — .similar

Post.similar(.embedding, "vector databases", k: 10)

Arguments:

ArgumentMeaning
.fieldthe Vector field to search
querya "text" string (only if the field has @vectorize) or a raw [f32, ...] vector
k:number of nearest neighbours to return
ef:(optional) HNSW search width — higher = better recall, more work
rerank:(optional) re-score this many ANN candidates with full-precision vectors before returning the top k

A text query is embedded with the same model as the field's @vectorize. A raw-vector query works on any vector field and must match the field dimension. (Text after // below is a note for the reader, not query syntax.)

// text query (field must have @vectorize)
Post.similar(.embedding, "distributed consensus", k: 5)

// raw vector query
Post.similar(.embedding, [0.01, 0.42, -0.17], k: 5)   // shortened: give the field's full N elements

// tune recall: widen the search and rerank the top 50 with exact vectors
Post.similar(.embedding, "distributed consensus", k: 10, ef: 200, rerank: 50)

// narrow the candidate set with a filter first
Post.filter(.published == true).similar(.embedding, "rust async", k: 10)

Every .similar result is ranked and each carries a score, read as Row.score: the index's distance under the field's metric (lower is closer), with results nearest-first. When cross-encoder reranking is on and the reranker scored a row, that row additionally carries rerank_score (higher is more relevant) and the scored rows come back most-relevant-first; score stays the distance, so the two never share a column. Rows the cross-encoder could not score (a raw-vector query, the reranker failed to load, or the object has no readable source text) have no rerank_score; see Ranked results for the exact rules. See Ranked results and, for keyword search over the same objects, .matches.

Under security rules .similar searches only rows the caller may read. A bare Type.similar(...) lists the type and checks each row against the read rule before searching, so it is subject to the governor's scan budget like any listing — on a large type, narrow first with an indexed filter (Post.filter(.ownerUid == "u1").similar(...)). Up to 10 000 readable candidates are searched exactly; beyond that the approximate index is filtered to the readable set, which (as for any large filter) can return fewer than k rows.

Tuning recall vs. latency

Three knobs trade accuracy for speed:

  • quantization (schema, per field) — more bits = higher base recall, larger index. turboquant_4bit is a good default; drop to 3bit/2bit to shrink the index and lean on rerank.
  • ef (per query) — a larger HNSW search width explores more of the graph. Raises recall at a latency cost.
  • rerank (per query) — pull a larger candidate pool from the (quantized) index, then re-score those candidates with the exact f32 vectors and return the best k. This recovers most of the precision lost to quantization for a modest cost.

A common high-recall pattern is a moderate ef with rerank set to a few × k:

Post.similar(.embedding, "query text", k: 10, ef: 200, rerank: 50)

Server-wide defaults for ef and rerank can also be set via the RHYPEDB_EF and RHYPEDB_RERANK environment variables (see Running rhypedb); they apply only to queries that omit the corresponding argument, and an explicit per-query ef:/rerank: (including rerank: 0 to force rerank off) always overrides them.

What's preserved across backups

Because the raw f32 vectors are stored losslessly, both a physical backup and a logical export preserve your vectors exactly. A logical export ships the raw vectors and the HNSW graph is rebuilt on import (the graph itself holds only lossy quantized codes and is cheap to regenerate). See Backup & Recovery.