Skip to content

Keys

A key is a unique string name for a document: a path, a URL, an object address, a primary key. With keys you do not manage numeric ids, and you can fetch a document directly, without a search.

await index.upsert({ keys: ["notes/deploy.md"], texts: [text], metas: ["Deploy runbook"] });
const { records } = await index.get(["notes/deploy.md"], { text: true });
records.get("notes/deploy.md"); // { key, id, meta: "Deploy runbook", attrs, text }

Keys need a text configuration on the index. Every index made with createGeneration or createLexicalGeneration has one, unless you passed lex: null.

const result = await index.upsert({
keys: ["s3://bucket/a.bin", "s3://bucket/c.bin"],
metas: [JSON.stringify({ host: "cdn-1", size: 4096 }), JSON.stringify({ host: "cdn-2", size: 128 })],
});
// result.ids = [1000, 1001] thread gave each new key an id
// result.allocated = 2 how many keys were new
result = index.upsert(keys=["s3://bucket/a.bin", "s3://bucket/c.bin"], metas=[meta_a, meta_c])
# result.ids, result.allocated
  • A new key gets the next free id.
  • A known key keeps its id, and the new document replaces the old one.

A key is a non-empty string of at most 4,096 bytes. Keys must be unique within one call.

Task TypeScript Python
Get documents by key await index.get(keys, { text: true }) index.get(keys, text=True)
Get only the ids await index.resolveKeys(keys) index.resolve_keys(keys)
List keys under a prefix await index.listKeys({ prefix, limit }) index.list_keys(prefix, limit=100)
Delete by key await index.deleteKeys(keys) index.delete_keys(keys)

Listing returns pages in key order:

let after: string | undefined;
do {
const page = await index.listKeys({ prefix: "s3://bucket/", limit: 100, after });
for (const { key, id } of page.entries) console.log(key, id);
after = page.more ? page.next : undefined;
} while (after !== undefined);

A missing key is not an error. get leaves it out of records, and deleteKeys lists it in missing.

get is two small range reads from cached files: one finds the id of the key, one reads the document. From a Worker with a warm cache that is tens of milliseconds, and a few milliseconds from a local copy. Keys add nothing to the cost of a search.

  • A search hit does not include its key. If your results must show the key, also put it in metas.
  • Chunked documents use the key of the parent plus # and a number: docs/a.md#0, docs/a.md#1 (Chunking).
  • You can use ids and keys together. upsert({ ids: [7], keys: ["seven"] }) names id 7.

An index with keys and no vectors is a small key-value store that you can also search and filter. It suits records with a unique name and a few attributes: files, hosts, products.

You need Use
The record metas, as a JSON string
Lookup by name get([key])
All names under a prefix listKeys({ prefix })
“All records where host is X” A tag in texts, such as host:cdn-1, then search({ text: "host:cdn-1" }). Create the index with tokenizer: { identifiers: "keep" } so that a tag stays one token.
A small range, such as a size class A filter field

About 180 MB for each million records with 60-byte keys and 100-byte metas. The key-value case below has the details.

The rest of this page is the design: how keys are stored, how ids are allocated, and what a listing reads.

A segment (the immutable THREADIX object an upsert writes, see Text index) can carry a keys section: the batch’s unique string keys sorted by bytes, each with its u32 id, bucketed behind a small resident directory, exactly like the terms directory with an id in place of a posting range. get(key) is then two range reads against cached objects: the key’s bucket (-> id), then the id’s docs bucket (-> meta, attrs, text). Segments are walked newest first and the usual tombstone rule applies, so a re-put of a key replaces its document and a delete hides it; a segment compaction merges the sections (newest copy of a key wins). Keyed upserts need no ids: a known key keeps its id, a new key takes the next one from a counter in the manifest (nextId). Because the section is sorted, a prefix listing is a binary search to the first bucket and a forward scan. Nothing in the dense query path changes.

The case that motivated this: records named by a unique string address with a few attributes (a host, a size, …) that are looked up by key and sometimes listed or filtered, without vectors. This is a lexical-only generation with keys:

need mechanism cost
the record and its attributes metas (a JSON object) in the docs store one bucket read per lookup
get(key) the keys section one bucket read, then the docs read
host = X (any cardinality) a facet token in texts, e.g. host:cdn-1.example.com, with tokenizer: {identifiers: "keep"}; search({text: "host:cdn-1.example.com"}) is the exact set of ids with that host one term lookup + one posting list
a bucketed range such as size a few bits of the 32-bit attrs word (Filter), tested inside the scan or through docs nothing extra
keys under a prefix listKeys({prefix}) one contiguous range per segment

Facet tokens are plain terms, so the BM25 score of a facet-only query is meaningless but the hit set is exact, and they combine with real text terms and with vector search in search. With identifiers: "keep" a token like host:cdn-1.example.com stays one term (it is a technical run with joiners); with the default tokenizer it would be split into words.

Approximate size per record with 60-byte keys, a 100-byte meta and three facets: keys ~65 B, docs ~105 B, postings ~6 B; about 180 MB per million records, with resident directories under 1 MB (16 B + one key per bucket of 128 keys, 16 B per bucket of 128 docs).

manifest.nextId is the next id a keyed upsert allocates; absent, it is manifest.vectors (a built dense base holds ids [0, vectors); a lexical-only generation starts at 0). A keyed upsert resolves its keys against the head revision’s segments, reuses the ids of known keys and takes nextId, nextId + 1, ... for the rest, then commits nextId + allocated. The manifest commit is the serialization point (a create-only PUT), so two writers never allocate the same id: a lost race re-resolves the keys against the new head and re-encodes with the new ids. An upsert with explicit ids moves the counter past its largest id, so explicit and allocated ids never collide once both have been used. A rollback keeps the larger counter: an id is never reused.

Reusing the id is what makes a re-put a replacement: the new segment carries the key again under the same id and the upsert’s tombstone set hides the older copies (slab, delta, docs, postings), exactly as an upsert by id does. Giving ids and keys together is allowed: the keys then name those ids (a key moved to another id resolves to the newest copy; the old id’s document stays until it is deleted or replaced).

resolveKeys walks the segments newest first. In each, the pending keys are looked up (one coalesced bucket read per segment); a key found there is decided by that copy: live if no tombstone set with rev > addedRev holds its id, dead otherwise (a delete by key tombstones the id, which hides the key’s newest entry; a later put of the same key writes a newer entry under a new id). After a compaction every live key sits in the base segment, so a lookup is one keys read and one docs read; before it, a key reads at most one bucket per segment, bounded by the segment-count threshold that triggers compaction. A missing key costs a bucket read in every segment that has keys.

listKeys asks every segment for a page (prefix, after, limit) concurrently and merges them newest first, the first copy of a key deciding whether it is live. A segment page that stopped early (more keys follow) bounds what is known, since keys past its last entry could have a newer copy there: the merged page ends at the smallest such cursor and returns it as next; the caller continues with after: next until more is false. Within one segment a page is one contiguous range read (ceil((limit + 1) / bucketKeys) + 1 buckets, usually a few KB).

deleteKeys resolves the keys through the head revision and writes one tombstone set for the ids found (nothing when none was). compactSegments merges the keys sections like the docs sections: inputs oldest first, the newest copy of a key wins, entries whose id a tombstone set hides are dropped; the base carries keys: true. GC, snapshots and packs treat segments as opaque objects, so keys travel with them, and the interop test checks that an index keyed from one SDK resolves identically from the other.

Nothing. The dense and lexical query paths do not read the keys section; a segment’s resident parts grow by its keys directory (one entry plus one key per bucket). get is two rounds (keys bucket, docs bucket) against immutable cached objects: a few milliseconds from a local copy, tens of milliseconds from a Worker through a warm edge cache, the same as resolving a hit’s document today.

A keyed index often gets its texts and metas first and its embeddings later. upsert({ keys, vectors, scope: "dense" }) (Python scope="dense") attaches the vectors to the existing documents: the keys resolve through the head (a key without a document is an error), no segment is written, and the tombstone set hides only older dense copies, so get(key), listKeys, text search and the stored meta and text stay as they were. With an embedding model, texts (not stored) can stand in for vectors. The other way round, scope: "lexical" changes texts or metas without the vector. See Update path.

  • No reverse lookup (id -> key): put the key in meta if a search hit must show it.
  • No Bloom filter for negative lookups: a missing key reads one bucket per segment with keys.
  • Facet tokens count in BM25 document lengths; a flag that excludes them from the length would keep text scores unchanged in a mixed corpus.
  • A raw tokenizer option (the whole text as one token) would make facet tokens independent of the identifiers mode.