# Vector Search

This page explains what ToriiDB needs for vector operations and how `SetVector` attaches an OpenAI embedding to an entry in the background.

## Prerequisites

An embedder is created in `store.New` when an API key is found (see [Configuration](/configuration-api-key)). Without it, ordinary storage still works and vector writes and searches return `ErrNoEmbedder`.

| Setting | Value |
|---|---|
| Model | `text-embedding-3-small` |
| Dimension | `TORIIDB_EMBED_DIM`, default `256` |
| Endpoint | `https://api.openai.com/v1/embeddings` |
| Response check | Vector length must equal the dimension |

## Attaching a Vector

```text
SET article:1 Redis-style embedded storage VECTOR
```

```go
err := torii.SetVector(ctx, "article:1", "embedded JSON database", store.SetDefault, nil)
err = d.SetVector(ctx, 0, "article:1", "embedded JSON database", nil)
```

`SetVector` rejects blank text, runs a normal `Set` (so the value is readable immediately and any old vector is cleared), then starts a background job tracked by the store's wait group:

1. Look up the embedding cache under the read lock.
2. On a miss, call OpenAI with no lock held (35 s timeout) and store the result in the cache.
3. Under the write lock, attach the vector only if the key still exists and its value is still the embedded text.
4. Append a `SET` record carrying the encoded vector.

A failed embedding is silently dropped; the text stays stored without a vector. `Store.Close()` waits for pending jobs.

## Consistency Notes

- A write is searchable only after its background job finishes.
- Changing the value before the job finishes discards the stale embedding.
- Search compares only vectors whose length equals the query vector's length.
- Field-level `SET` cannot carry `VECTOR`.

## Related Pages

- [Embedding Cache](/vector-search-cache)
- [VSEARCH and VSIM](/vector-search-query)
- [API Key](/configuration-api-key)
