Write documents
You add and change documents with upsert, and remove them with delete or deleteKeys. This page shows each
case, and the three rules that keep writes cheap.
A document
Section titled “A document”A document can have these parts. All are optional except a name (a key or an id).
| Part | What it is | Example |
|---|---|---|
keys |
A unique string name. thread gives the document an id for you. | "notes/deploy.md" |
ids |
A numeric id (an unsigned 32-bit integer), if you prefer to choose it yourself. | 1042 |
texts |
The text. It is indexed for keyword search, and embedded if the index has a model. | "The deploy failed..." |
vectors |
The embedding, if you make it yourself. | Float32Array(768) |
metas |
A string that comes back with a hit: a title, or a JSON object. | '{"title":"Runbook"}' |
attrs |
Small fields that you can filter on. Define the fields first. | { tenant: 42, lang: "en" } |
Every part is a list, with one entry for each document in the batch.
Add or replace documents
Section titled “Add or replace documents”const result = await index.upsert({ keys: ["notes/deploy.md", "notes/billing.md"], texts: ["The deploy failed because the migration timed out.", "Invoices go out on the first of the month."], metas: ["Deploy runbook", "Billing FAQ"],});// result.ids = [0, 1] the ids of the documents// result.rev = 4 the revision that this write maderesult = index.upsert( keys=["notes/deploy.md", "notes/billing.md"], texts=["The deploy failed because the migration timed out.", "Invoices go out on the first of the month."], metas=["Deploy runbook", "Billing FAQ"],)upsert means “insert or replace”. If the key exists, the new document replaces the old one: its vector, text,
meta and attributes. If the key is new, the document is added.
Where the vectors come from:
| The index | You pass | Result |
|---|---|---|
Has an embedding model, opened with embed |
texts |
Each text is embedded for you |
| Any index with vectors | vectors |
Your vectors are stored as they are (after normalization) |
| Any index with vectors | texts and embed: async (texts) => vectors |
Your function is called once for the batch |
A text index, or embed: false |
texts |
Text only. The document is found by keywords. |
In one batch, some documents can have a vector and others not: vectors: [vectorA, null].
Long documents
Section titled “Long documents”A long text gives a poor vector, and a hit on a 50-page file does not tell you where to look. Add chunk: true
and each text is split into passages of 200 words. Each passage becomes its own document.
await index.upsert({ keys: ["docs/handbook.md"], texts: [handbook], metas: ["Handbook"], chunk: true });// stored as docs/handbook.md#0, docs/handbook.md#1, ...Chunking has the options and the rules.
Change one part of a document
Section titled “Change one part of a document”To add or change only the vector, or only the text, use scope. The other part stays as it is.
// add a vector to a document that has only text. Read its attributes, and give them to the vector.const key = "notes/deploy.md";const { records } = await index.get([key]);const doc = records.get(key);if (doc) await index.upsert({ keys: [key], vectors: [vector], attrs: [doc.attrs], scope: "dense" });
// change the text and the meta, keep the vectorawait index.upsert({ keys: [key], texts: [newText], metas: ["Deploy runbook v2"], scope: "lexical" });With scope: "dense", pass the document’s own attrs, as above. The vector keeps its own copy of the
attributes, and a filter on a vector search tests that copy. Without attrs the vector has none, and with other
values the vector and the document disagree: a filter can then miss the document or match the wrong one. Do the
read and the write from one writer, so that the document cannot change between them.
Delete documents
Section titled “Delete documents”await index.deleteKeys(["notes/billing.md"]); // by keyawait index.delete([1042, 1043]); // by idindex.delete_keys(["notes/billing.md"])index.delete([1042, 1043])A delete hides the whole document at once: its vector, its text, its meta and its key.
To delete every chunk of a long document, see Chunking.
When a write is visible
Section titled “When a write is visible”| Reader | Sees the write |
|---|---|
| The handle that made the write | At once |
| Another open index that runs searches | After its next background refresh: a search checks for a new revision when refreshInterval (30 seconds) has passed, and the searches after that one use it |
Another open index that only calls get or listKeys |
After await index.refresh(). Lookups do not start a refresh. |
When another handle must see a write that has completed, refresh it until index.rev has reached the rev that
the write returned. Know your freshness has the details.
Three rules for cheap writes
Section titled “Three rules for cheap writes”1. Batch. Every upsert or delete makes one revision and about ten requests to the store, whether it holds
1 document or 1,000. Send at least 64 documents in a call, or everything that arrived in the last second. Never
make one call for each document. One call can hold up to 4,096 documents.
2. Compact. Each write adds small files that every later query must read. Compaction merges them. If writes go through your own code, run compaction after a write when it is due:
if (index.compactionDue().due) { for (let i = 0; i < 8; i++) { // a few bounded slices, then stop const run = await index.compact(); if (!run.more || run.outcome === "locked") break; // locked: another run has the work }}await index.compactSegments(); // the same for the text side, from time to timeRun it in production explains where to run this.
3. Write from one place. Two writers at the same time are safe on a bucket: the store accepts one revision and the other writer tries again. (A folder on disk that Node and Python both write to is the exception: see Write from one place.) But each retry costs time. One writer for each index (a queue consumer, a Durable Object, a cron job) is simpler and faster. In a Worker, also keep writes out of the isolates that serve queries: a write uses memory that the isolate does not give back.
What a write returns
Section titled “What a write returns”| Field | Meaning |
|---|---|
rev |
The revision that the write made |
ids |
The id of each document, in order |
allocated |
How many keys were new |
count |
How many documents were written |
chunks |
With chunk: { documents, count, stale } |
conflicts |
How many times the write lost a race with another writer and tried again |
Errors
Section titled “Errors”Errors are ThreadError with an HTTP-style status.
| Status | Meaning | What to do |
|---|---|---|
| 400 | The request is wrong: a missing part, a vector of the wrong size, too many documents in one call, an index with no text configuration | Fix the call |
| 404 | An object is missing: no index at this store or prefix | Check the store, the prefix and the generation |
| 409 | A conflict: the generation exists, or the pointer was not where you expected | Read the state again, then decide |
| 413 | Compaction or a bulk build cannot process an input within its memory budget | Send smaller batches, or raise the budget |
| 502 | The embedding provider failed | Try again later |
- Search
- Filter, Keys, Chunking
- Update path: what a write puts in the bucket, byte by byte.