# Vector search

Store embeddings in a table and find the rows nearest to a vector: "documents like this one", "answers close to this question", "products similar to that product". Rezin Core runs [pgvector](https://github.com/pgvector/pgvector) 0.8 on every project's database.

You bring the embeddings. Your application computes them with whatever model it uses and stores them; Rezin Core stores, indexes and searches them. Rezin Core does not offer a model or an embedding service to your application.

## A vector column

In the table editor, choose the type **Vector (embedding)** and give its **dimensions**: the length of the embeddings you will store (for example 384, 768 or 1536). Every vector in the column must have exactly that length. A column can hold up to 16,000 dimensions; an index takes up to 2,000.

In SQL the type is `vector(n)`:

```sql
create table notes (
  id uuid primary key default gen_random_uuid(),
  organization_id uuid not null default core.app_org(),
  body text not null,
  embedding vector(1536)
);
```

The extension lives in a shared schema called `extensions`, which is on every project's search path. A project can use the `vector` type, its functions and its operators, but cannot create, change or drop extensions or add anything to that schema. See [How your database is built: extensions](https://rezincore.com/docs/database/area.md#extensions).

Through the [data API](https://rezincore.com/docs/database/data-api.md) and the [client library](https://rezincore.com/docs/client.md), a vector goes in and comes back as a list of numbers: `[0.12, -0.03, ...]`.

## Searching

Send the vector to search for. The rows nearest to it come back, nearest first, each with `_distance`:

```
POST /v1/data/notes/search?organization_id=eq.<id>
apikey: <key>
authorization: Bearer <user token>

{ "column": "embedding", "vector": [0.12, -0.03, ...], "distance": "cosine", "limit": 5 }
```

```json
{ "rows": [ { "id": "…", "body": "…", "embedding": [ … ], "_distance": 0.081 }, … ] }
```

| Field | Meaning |
|---|---|
| `column` | The vector column to search. |
| `vector` | Your embedding, the same length as the column's dimensions. |
| `distance` | `cosine` (the default), `l2` (straight-line distance) or `inner` (inner product). |
| `limit` | 1 to 1000; 10 if left out. |
| `select` | Which columns to return, as in a read: `"id,body"`. |
| `maxDistance` | Only rows at most this far away. |
| `efSearch` | How hard an HNSW index looks, for this search only (Postgres uses 40). Higher finds more, more slowly. |
| `probes` | How many IVFFlat lists are read, for this search only (Postgres uses 1). |

The query string takes the same [filters](https://rezincore.com/docs/database/data-api.md#filters) as a read, so a search can be narrowed to, say, published rows: `?status=eq.published`.

**Distances.** Smaller is always nearer. `cosine` is 0 for the same direction and 2 for the opposite; for similarity, use `1 - _distance`. `l2` is the straight-line distance. `inner` is the *negative* inner product, so that smaller is nearer; for normalised embeddings it orders rows the same way as cosine and is a little quicker.

**Access rules apply.** A search is a read. It runs as the caller, in the caller's organization, under the table's [row-level security](https://rezincore.com/docs/database/row-level-security.md) like any other read: a row the caller could not read is never among the results, and never counted. Rows with no vector are left out.

With the client library:

```js
const near = await rezin.table('notes').search({
  column: 'embedding',
  vector: embedding,          // your own, e.g. from the model your app uses
  limit: 5,
  where: { archived: false },
  columns: ['id', 'body'],
});
// [{ id, body, _distance }, …]
```

In SQL (the [SQL workspace](https://rezincore.com/docs/database/sql-workspace.md), migrations, or the connector's `query_sql`):

```sql
select id, body, embedding <=> '[0.12,-0.03,...]' as distance
from notes
order by embedding <=> '[0.12,-0.03,...]'
limit 5;
```

`<=>` is cosine distance, `<->` straight-line distance and `<#>` negative inner product.

## Indexes

Without an index, a search reads every row: exact, and fine for a few thousand rows. For more, add an index on the table's **Structure** tab, under **Indexes**, in **Index for similarity search**. Choose the column, the kind and the distance; the console shows how many rows there are, roughly how large the index will be and whether it fits in the server's build memory before you build it.

| Kind | Good for |
|---|---|
| HNSW | Most uses: the best recall and speed. Slower to build and larger. It builds much more slowly once it no longer fits in the build memory. Settings: `m` 16, `ef_construction` 64. |
| IVFFlat | Quicker to build and smaller. Build it after the table holds most of its rows, because its lists are chosen from the rows there at the time. Lists: the rows divided by 1,000 (the square root of the rows above a million). |

An index helps only searches by the distance it was built for: build a cosine index for cosine searches. Both kinds are approximate: a search with an index may miss a row an exact search would find. Raise `efSearch` (HNSW) or `probes` (IVFFlat) to find more.

In SQL:

```sql
create index on notes using hnsw (embedding vector_cosine_ops);
create index on notes using ivfflat (embedding vector_l2_ops) with (lists = 100);
```

A filter (`?status=eq.published`, or rules that hide many rows) applies after the index has found its nearest rows, so a narrow filter can return fewer rows than `limit`. Raise `efSearch` or `probes`, or index a smaller table.

## Limits

- Up to 16,000 dimensions in a column, up to 2,000 for an HNSW or IVFFlat index.
- Vectors and their indexes count toward your plan's database size.
- Building an index runs within the project's statement time limit; a very large build may need to be done in a [migration](https://rezincore.com/docs/database/sql.md).
