# Vector search

Source: https://docs.rushdb.com/engine/indexes-and-search/vector-search/

<!-- Generated from docs/manual/indexes-and-search/vector-search.md in the RushDB engine repository by scripts/sync-engine-manual.mjs. Edit it there. -->

# Vector search

This page shows how to store embeddings in RushDB and search them by
similarity: creating a vector index, attaching vectors to records, running
exact and approximate (ANN) queries with filters, and keeping vectors in step
with changing data. It is for developers building semantic search or
retrieval-augmented generation (RAG) on top of RushDB.

RushDB stores and searches vectors; it **never calls an embedding model**. You
compute embeddings with your own provider and send them to the database.

## Concepts

| Term | Meaning |
|---|---|
| **Vector index** | A named vector space for one label (or relationship type) and one property, with a fixed number of `dimensions` and a `similarity` metric. |
| **Space** | The immutable contract behind an index name: dimensions and metric never change. To change them, drop the index and create a new one. |
| **Binding** | The association between one record (or relationship) and one stored vector in a space. A record has at most one vector per index. |
| **Generation** | An opaque token issued when the index is declared (an identical re-declaration keeps it), used to reject embedding jobs that started against an older declaration. |

Vectors are stored as 32-bit floats. Each tenant can hold at most **256**
vector spaces.

## Sending vectors

Every vector in a request — `upsertVectors` items, inline `vectors` and
`$vectors` entries, `query.vector` and the hybrid vector leg — is either a
JSON array of numbers or the **binary form**:

```json
{ "f32": "zczMPQAAgD8AAAAA" }
```

`f32` is the standard base64 (RFC 4648, with padding) of the components as
little-endian IEEE-754 32-bit floats, back to back. The example is
`[0.1, 1.0, 0.0]`. The binary form carries every bit as sent — no decimal
rounding on either side — in about a quarter of the text a JSON array of
full-precision floats takes, and needs no float parsing. A binary vector whose
decoded length is not a multiple of 4 bytes, that holds a NaN or an infinity,
or that is not valid base64 is refused with `invalid_request`; its length must
equal the index's `dimensions` like any other vector.

## Create a vector index

`index.createVector` takes a `VectorIndexDefinition`:

| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| `name` | string | yes¹ | — | Index name, 1–64 bytes of `[A-Za-z0-9._:-]`, not starting with `emb__`. |
| `label` | string | yes | — | Label of the records to index (the relationship type when `owner` is `relationship`). |
| `property` | string | yes | — | Property the vectors belong to. |
| `dimensions` | integer | yes | — | Vector length; must be positive. Every stored and query vector must have exactly this length. |
| `similarity` | string | no | `cosine` | `cosine`, `dot`, or `euclidean` (alias `l2`). |
| `model` | string | no | — | Your embedding model identifier. Not interpreted, but re-declaring the same `name` with a different `model` is rejected, so vectors from incomparable models cannot be mixed. |
| `owner` | string | no | `record` | `record` (alias `node`) or `relationship` (alias `edge`). |

¹ The server option `resources.require-vector-index-name`
(`RUSHDB_REQUIRE_VECTOR_INDEX_NAME`) is **on by default** and makes `name`
mandatory on create, search and inline vectors. When it is turned off, a
missing name falls back to a derived alias
`emb__<label>__<property>__<similarity>__<dimensions>`, which cannot tell two
same-shape models apart.

```bash
curl -s http://127.0.0.1:3000/protocol/v1/execute \
  -H "Authorization: Bearer $RUSHDB_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "protocolVersion": 1,
    "operation": {
      "kind": "index",
      "index": {
        "kind": "createVector",
        "definition": {
          "name": "doc-embedding",
          "label": "Doc",
          "property": "embedding",
          "dimensions": 3,
          "similarity": "cosine",
          "model": "my-embedder-v1"
        }
      }
    }
  }'
```

```json
{
  "protocolVersion": 1,
  "response": {
    "kind": "vectorIndex",
    "index": {
      "id": "doc-embedding",
      "name": "doc-embedding",
      "label": "Doc",
      "property": "embedding",
      "similarity": "cosine",
      "dimensions": 3,
      "status": "awaiting_vectors",
      "model": "my-embedder-v1",
      "nameSource": "explicit",
      "owner": "record",
      "generation": "0192f0c4-…"
    }
  }
}
```

Re-declaring an existing name with an identical definition is a no-op: it
returns the index unchanged, with the same `generation`, so embedding jobs in
flight stay valid. Re-declaring it with different `dimensions` or
`similarity` fails (spaces are immutable); with a different `model`, `label`,
`property` or `owner` it fails with `conflict` (`already exists ... different
...`). Drop the index first to change its definition.

### From statement languages

| Language | Create | Drop |
|---|---|---|
| SQL | `CREATE VECTOR INDEX name ON Label(prop) [SIMILARITY cosine\|dot\|euclidean] DIMENSIONS n` | `DROP INDEX name` |
| Mongo | `{"createIndex": {"collection": "Label", "property": "prop", "dimensions": n, "similarity": "cosine", "name": "…"}}` | `{"dropIndex": {"name": "…"}}` |
| Cypher / GQL | — | `DROP INDEX name` |

Mongo `{"reIndex": {"name": "…"}}` runs [`revalidateVector`](#keep-vectors-in-step-with-data).
`DROP INDEX` drops a declared (ordered or text) index of that name first, and
a vector index only if no declared index matches.

## Attach vectors to records

You can bind vectors in three ways. All of them validate the whole batch
(index exists, every vector has the index's dimensions, every component is
finite) before binding anything.

### 1. `upsertVectors` for existing records

```bash
curl -s http://127.0.0.1:3000/protocol/v1/execute \
  -H "Authorization: Bearer $RUSHDB_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "protocolVersion": 1,
    "operation": {
      "kind": "index",
      "index": {
        "kind": "upsertVectors",
        "name": "doc-embedding",
        "items": [
          { "recordId": "0192f0c4-7a1e-7b8a-9c3d-2f6e5a4b3c21", "vector": [0.12, 0.98, 0.05] },
          { "recordId": "0192f0c47a1e7b8a9c3d2f6e5a4b3c22", "vector": [0.91, 0.10, 0.40] }
        ]
      }
    }
  }'
```

The response is `{"kind": "vectorsUpserted", "updated": 2, "requested": 2}`.
Record ids may be dashed UUIDs or 32-character hex. Upserting again replaces
the record's vector; upserting an identical vector is a no-op.

| Item field | Required | Description |
|---|---|---|
| `recordId` | yes | Record (or relationship, for a relationship index) id. |
| `vector` | yes | Components, as a number array or the [binary form](#sending-vectors); length must equal `dimensions`. |
| `indexGeneration` | no | The `generation` you read when your embedding job started. If the index was dropped and recreated (or re-declared) since, the whole batch is rejected with `stale vector index generation`. |
| `sourceHash` | no | Engine value hash of the source property captured before embedding. When present, the bind succeeds only if the property still has that value; the binding is then *source-tracked* (see below). |

Items without `sourceHash` on a record index are bound in a single commit.

### 2. Inline with the record (`putRecord` and ingest)

`putRecord` accepts a `vectors` array, and `ingest.documents` accepts a
`$vectors` array on any document. Each entry names the property, the vector
and the index:

```json
{
  "protocolVersion": 1,
  "operation": {
    "kind": "ingest",
    "ingest": {
      "kind": "documents",
      "label": "Doc",
      "documents": [
        {
          "title": "Graph basics",
          "$vectors": [
            { "propertyName": "embedding", "vector": [0.12, 0.98, 0.05], "index": "doc-embedding" }
          ]
        }
      ]
    }
  }
}
```

An entry naming an index that does not exist fails the request before any
record is created (`no embedding index named …`). `similarityFunction` is an
optional entry field used only to disambiguate when names are not required.

### 3. Backfill unbound records

`query.vectorIndexCandidates` pages through records of a label that carry the
property but have no binding in the index yet. Use it to drive an embedding
backfill: fetch a page, embed each `value`, then `upsertVectors`.

```json
{
  "protocolVersion": 1,
  "operation": {
    "kind": "query",
    "query": {
      "kind": "vectorIndexCandidates",
      "query": { "label": "Doc", "property": "body", "index": "doc-embedding", "skip": 0, "limit": 500 }
    }
  }
}
```

The response is `{"kind": "vectorIndexCandidates", "records": [{"id": "…", "value": "…", "sourceHash": …, "indexGeneration": "…"}], "total": n}`,
where `total` counts every unbound candidate. `limit` defaults to 500. Send
each candidate's `sourceHash` and `indexGeneration` back with its vector in
`upsertVectors`: the bind is then rejected if the value or the index changed
while the embedding was computed.

## Search

`query.vector` ranks the records of a label by similarity to a query vector.

| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| `labels` | string[] | yes | — | Label to search (the first entry selects the derived alias when `index` is omitted). `[]` searches the index's own label. |
| `property` | string | yes | — | Vector property. |
| `vector` | number[] or `{"f32": …}` | yes | — | Query vector, non-empty, finite, with the index's dimensions; see [Sending vectors](#sending-vectors). |
| `index` | string | yes¹ | — | Index name. When set, the space's own metric is used. |
| `similarity` | string | no | `cosine` | Only used to derive the alias when `index` is omitted. |
| `predicate` | object | no | `{}` | Filter applied **before** ranking (same syntax as `query.records`). |
| `offset` | integer | no | 0 | Ranked results to skip. |
| `limit` | integer | yes | — | Results to return. |
| `mode` | string | no | `auto` | `auto`, `exact` or `ann`; see [Exact versus approximate](#exact-versus-approximate-ann). `ef > 0` with no `mode` means `ann`. |
| `ef` | integer | no | `resources.vector-default-ef` (128) | ANN breadth whenever the search runs on the ANN graph: the graph walk keeps this many candidates and all of them are re-scored exactly. At least `offset + limit` is used. |
| `scoreThreshold` | number | no | — | Keep only hits scoring at least this much. A page may then hold fewer than `limit` records. |

```bash
curl -s http://127.0.0.1:3000/protocol/v1/execute \
  -H "Authorization: Bearer $RUSHDB_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "protocolVersion": 1,
    "operation": {
      "kind": "query",
      "query": {
        "kind": "vector",
        "query": {
          "labels": ["Doc"],
          "property": "embedding",
          "index": "doc-embedding",
          "vector": [0.10, 0.95, 0.00],
          "predicate": { "lang": "en" },
          "limit": 5
        }
      }
    }
  }'
```

Results come back as a `records` response, best first. Each record carries a
`score`; `total` is the number of records on this page. `vectorSearch` reports
how the search ran: `mode` (`exact` or `ann`), `ef` for an ANN search, `pool`
(the vectors the search could score: the records matching `predicate`, at most
the index's vectors, or the whole index without a predicate) and, when a search
that would use the ANN graph was answered exactly, `fallback` (see below).

```json
{
  "kind": "records",
  "total": 1,
  "records": [
    {
      "id": "0192f0c4-7a1e-7b8a-9c3d-2f6e5a4b3c21",
      "label": "Doc",
      "properties": { "title": "Graph basics", "lang": "en" },
      "propertyTypes": { "title": "string", "lang": "string" },
      "score": 0.998
    }
  ],
  "vectorSearch": { "mode": "exact", "pool": 3 }
}
```

### Similarity metrics

All scores are "higher is more similar".

| `similarity` | Score | ANN with `ef` |
|---|---|---|
| `cosine` | dot(a, b) / (‖a‖·‖b‖) | yes |
| `dot` | dot(a, b) | no — always answered exactly |
| `euclidean` / `l2` | 1 / (1 + ‖a − b‖) | no — always answered exactly |

Scores are computed in 64-bit floating point and returned as 32-bit floats.
The same vectors give the same score bits on every CPU (see [Vector
internals](../architecture/vector-internals.md#scoring)). Search orders equal
scores deterministically by record.

### Exact versus approximate (ANN)

- **Exact:** every bound vector that passes the filter is scored;
  the top `offset + limit` are kept. Results are exact and memory use is
  proportional to `offset + limit`, not to the index size. With a
  `predicate`, only the matching records' vectors are read (one lookup per
  matching record), so the cost follows the number of matches, not the index
  size. Without one, every vector in the index is scored. A large search is
  split into fixed-size parts scored on several threads (as many as the
  server's thread budget lends at that moment, see `RUSHDB_THREADS`); the
  result is identical however many threads ran it.
- **ANN** (cosine only): an in-memory HNSW graph walk keeps the best
  `max(ef, offset + limit)` candidates, every candidate is **re-scored
  exactly** against the stored vector, and the best are returned. With a
  `predicate`, the graph walk skips records outside it, so every gathered
  candidate matches. `ef` is the per-query trade-off: a smaller `ef` walks
  fewer graph nodes and re-scores fewer vectors (lower latency, lower
  recall), a larger one the opposite. A resident graph answers in about a
  millisecond, but building it takes seconds; the build adds vectors on
  several threads.

`mode` chooses between them. The choice is made on the **pool**: the records
matching `predicate` (at most the index's vectors) when there is one, the
index's vectors otherwise.

`resources.vector-exact-max-pool` sets what "small enough" means. The default,
`auto`, compares the cost of the two paths for each search: scoring the pool
exactly grows with the pool and the dimensions, while a filtered graph walk
grows with `ef` and with how few of the index's vectors the filter admits
(the walk has to step over the others). A filtered search is exact while
`pool² × dimensions ≤ 26,000 × ef × index vectors`, and an unfiltered one while
`index vectors × dimensions ≤ 144,000 × ef`. For example, at the default `ef`
of 128:

| Index | Unfiltered: exact up to | Filtered: exact up to (`ef` 128 / 512) |
|---|---|---|
| 100,000 × 1,536-d | 12,000 vectors | 14,720 / 29,439 matching records |
| 1,000,000 × 768-d | 24,000 vectors | 65,828 / 131,656 matching records |

A number fixes the pool instead: `vector-exact-max-pool: 20000` scores at most
20,000 vectors exactly whatever the dimensions and `ef`. `5000`, the value every
`config.yml` before 3.0.13 was generated with, reads as `auto`; to keep a fixed
pool of 5,000, write `fixed:5000`.

| `mode` | Behaviour |
|---|---|
| `auto` (default) | Exact while the pool is small enough (`resources.vector-exact-max-pool`, above) or the metric is not cosine, so a narrow filter on a large index is answered exactly, with full recall. Above that, ANN once the graph is resident; until then the search is answered exactly while the graph is built in the background (`fallback: annWarming`), so no search waits for a build. |
| `exact` | Exact search. |
| `ann` | ANN, building the graph first if it is not resident. A filtered pool small enough to score exactly is answered exactly instead (`fallback: smallCandidateSet`): it is cheaper than the graph walk and never less accurate. Refused (`invalid_request`) on a `dot` or `euclidean` index. |

ANN degrades to exact search instead of returning a short or wrong answer, and
reports why in `vectorSearch.fallback`:

| Situation | Behaviour | `fallback` |
|---|---|---|
| A forced `ann` whose filtered pool is small enough to score exactly | Exact search over the matching records | `smallCandidateSet` |
| The graph walk found fewer than `min(ef, offset + limit)` candidates that pass the filter | Exact search for this query | `lowConfidence` |
| The ANN graph does not fit the vector cache budget | Exact search; counted in `rushdb_ann_exact_fallbacks_total`. `auto` does not retry the build until the index changes. | `budget` |
| The logical cache budget is disabled (`RUSHDB_LOGICAL_CACHE_BYTES=0`) | No ANN can be resident: exact search, counted in `rushdb_ann_exact_fallbacks_total`; `revalidateVector` reports `annRebuildError` | `budget` |

The ANN graph is a rebuildable cache over the stored vectors: it is checked
against the query's snapshot, reopened from its persisted segments after a
restart or an idle eviction (decoding only what changed since), and kept
current as vectors are bound. Internally it may hold vectors at reduced (16-bit)
precision; results are always reranked with the stored 32-bit vectors. See
[Vector internals](../architecture/vector-internals.md).

### Filters

The `predicate` is resolved by the [query planner](query-planning.md) into a
candidate set **before** ranking, and both exact and ANN search only consider
candidates. A filtered search therefore never returns a record outside the
filter, and never returns fewer results because high-scoring records were
filtered out after ranking. A candidate set small enough to score exactly
(`resources.vector-exact-max-pool`) is scored exactly, reading only those
records' vectors. A larger one is searched on the ANN graph, which skips
non-matching records during the walk instead of filtering a fixed gather
afterwards, so a selective filter does not empty the gather.

> **Tip:** Declared [ordered indexes](indexes.md) speed up the filter stage of
> vector queries exactly as they do for `query.records`.

Records deleted after their vector was bound are never returned.

### Relationship vectors

An index created with `"owner": "relationship"` binds vectors to
relationships. `label` is the relationship type, `upsertVectors` items use the
relationship id as `recordId`, and `query.vector` takes the relationship type
in `labels`. The `predicate` filters on relationship properties. Results are
relationship objects (`__id`, `__type`, `__source_id`, `__target_id` and the
properties) with a `score`.

### Inspect a vector query

`describe.explainVector` takes the same document as `query.vector` and reports
the candidate count and the path the search would take, without ranking:

```json
{
  "protocolVersion": 1,
  "operation": {
    "kind": "describe",
    "describe": {
      "kind": "explainVector",
      "query": {
        "labels": ["Doc"], "property": "embedding", "index": "doc-embedding",
        "vector": [0.1, 0.95, 0.0], "predicate": { "lang": "en" }, "limit": 5
      }
    }
  }
}
```

The `explainVector` plan contains `accessPath` (`vector_index`), `alias` (the
named `index`), `label`, `candidateCount` (records matching the predicate; the
index's vectors without one), `k` (`offset + limit`), `predicate` and `search`:
the `vectorSearch` report the search would return, decided by the same rule
(`mode`, `ef`, `pool`, and a `fallback` known before searching:
`smallCandidateSet` or `annWarming`).

## Keep vectors in step with data

### Index status

`describe.vectorIndexes` lists every vector index with live coverage counts;
`describe.vectorIndex` (`{"kind": "vectorIndex", "name": "…"}`) returns one
index's statistics as a `vectorIndexStats` response.

| Field | Meaning |
|---|---|
| `status` | `awaiting_vectors` (no records carry the property yet), `ready` (every record carrying the property has a current binding), or `behind` (some are unbound or stale). |
| `totalRecords` | Records of the label carrying the property (or the number of bound vectors, if larger). |
| `indexedRecords` | Bindings eligible for search. |
| `unindexedRecords` | `totalRecords − indexedRecords`. |
| `staleBindings` | Bindings whose source value changed since they were bound. |
| `retiredBindings` | Bindings whose owner was deleted. |
| `sourceTrackedBindings` / `externalBindings` | Bindings with and without a source hash. |
| `annLifecycle` | `manifestState` and `providerState` (`memoryCached` or `rebuildRequired`; `rebuildRequired` after every restart is expected). |

Status is computed from the bindings and the source records at the read
snapshot; it is never a stored flag.

### Staleness

A binding is only searchable while its status is *ready*. Two kinds of
change make a binding ineligible:

- **Owner deleted** — the record is never returned by search, and
  `revalidateVector` marks the binding *retired*.
- **Source value changed** (source-tracked bindings only, i.e. those bound
  with `sourceHash`) — `revalidateVector` marks the binding *stale*; it stops
  being searched and is counted in `staleBindings` until you upsert a fresh
  vector.

Vectors bound without `sourceHash` (plain `upsertVectors`, inline vectors)
are treated as data you own: changing other properties of the record does not
make them stale. Re-upsert them when your source text changes.

### `revalidateVector`

```json
{ "protocolVersion": 1, "operation": { "kind": "index", "index": { "kind": "revalidateVector", "name": "doc-embedding" } } }
```

It reconciles every binding's status against live data in one commit, then
rebuilds the ANN graph. The `vectorIndexStats` response reports `stale`,
`retired`, `annEntries`, `annLifecycle` and `annRebuildError` (an ANN rebuild
failure does not undo the status reconciliation).

### Drop

`{"kind": "dropVector", "name": "doc-embedding"}` removes the index's bindings,
its space, its persisted ANN segments and its metadata. The name can then be
reused with a different shape; the new declaration gets a new `generation`.

A tenant holds at most 256 vector spaces at a time (one per vector or
structural index). Dropping an index frees its space, so creating and dropping
indexes never runs out. Declaring an index past the limit fails with
`resource_exhausted` (`retryable: false`), naming the limit; drop an unused
index first.

## Consistency

Vector bindings are committed through the same commit path as records. A search
reads one snapshot: it sees every binding committed before it started, and the
records it returns are reconstructed from that same snapshot. The ANN graph is
validated against the snapshot's version of the space before use.

## Sizing notes

- Each stored vector costs `4 × dimensions` bytes plus a binding row.
- Exact search reads one binding and one vector per candidate: with a
  `predicate` its cost grows with the number of matching records, without one
  with the index size (spread over the threads the server lends it). `auto` (and a forced `ann` with a `predicate`) searches
  exactly while that costs less than the graph walk and on the ANN graph
  above; set `resources.vector-exact-max-pool` to a number to fix that point.
- The ANN graph lives in memory and is charged to the logical cache budget
  (`RUSHDB_LOGICAL_CACHE_BYTES`, default 128 MiB). When it does not fit,
  queries fall back to exact search rather than fail. Watch
  `rushdb_ann_exact_fallbacks_total`.
- When the server setting `require-index-warmup` is on, `/ready` reports not-ready while
  any vector index is `behind` (single-tenant mode).

See [Capacity planning](../operations/capacity-planning.md) for measured
figures.

## See also

- [Hybrid search](hybrid-search.md)
- [Query planning](query-planning.md)
- [Vector internals](../architecture/vector-internals.md)
- [Writing data](../query/writing-data.md)
- [Nested import](../data-model/nested-import.md)
- [Metrics](../reference/metrics.md)
