Indexes
Indexes are built asynchronously in the background: writes return immediately, and is_indexes_ready() / await_quiescent() tell you when a fresh index has caught up. Querying before readiness falls back to correct (slower) paths — never wrong answers.
use hakodb::index::composite::definition::SortDirection;
db.create_index("users", "tenant")?; // secondary B-Tree
db.create_composite_index("users", vec![
("tenant".into(), SortDirection::Asc),
("age".into(), SortDirection::Desc),
])?; // composite
db.create_fts_index("articles", "body")?; // full-text
db.list_indexes(Some("users")); // inspect
- Secondary: one field, equality (and range) lookups.
- Composite: field tuples, for multi-field filters and order+limit without sorting.
- FTS: token/prefix search over text fields.
- Vector / graph: their own chapters (Vector, Graph) — same readiness contract.
Index snapshots persist across restarts; after compaction or recovery the engine rewrites them automatically.
Declare the schema once at boot
Services that own their collections usually want the whole index surface declared in one place before the first write. ensure_indexes is that surface — one idempotent call:
use hakodb::engine::IndexSchema;
db.ensure_indexes(&IndexSchema {
fts: vec![("articles".into(), "body".into())],
vector: vec![("articles".into(), "embedding".into(), 384, Metric::Cosine)],
tracked: vec!["docs".into()], // traverse (graph) tracking
})?;
Re-running it with identical definitions is a cheap no-op that never wipes populated indexes. Re-declaring an existing index with a different definition (metric, dim, field) is refused loudly — drop the index first. The per-family create_* calls above remain for dynamic or one-off creation; sync note below applies either way.
Dropping indexes
Every create has a matching drop. Drops remove the definition and its RAM state, leave document data untouched, and are idempotent: dropping an absent index returns Ok. Read-only engines refuse them (same ReadOnly error as writes).
db.drop_indexes("users")?; // canonical: all families
// Advanced: remove one family while keeping the others.
db.drop_index("users", "tenant")?; // secondary
db.drop_fts_index("articles", "body")?; // full-text
db.drop_vector_index("articles", "embedding")?; // vector + HNSW graph
let id = db.list_indexes(Some("users")).composite[0].id;
db.drop_composite_index("users", id)?; // composite
db.drop_traverse_index("docs")?; // stop incoming tracking
db.drop_edge_index("rel")?; // _from/_to + tracking
ensure_indexes/drop_indexesare the canonical boot-schema pair; the per-family calls remain for dynamic or one-off changes.- The composite id must belong to the named collection; a mismatch is
InvalidInput, never a cross-collection delete. - Dropping a unique composite index releases its claim ledger immediately, so the old tuple can be inserted again.
drop_edge_indexowns the_fromand_tosecondary indexes created bycreate_edge_index.- Index definitions persist, so a restart does not resurrect a dropped index.
- Synced deployments: index definitions replicate additively. Drop on every member, otherwise a peer can recreate the index on the next def sync. Def tombstones are tracked in hakodb#115.
Readiness and backfill
Opening a database starts index recovery in the background. Separately, creating a secondary, full-text, vector, or non-unique composite index registers the definition and then backfills it on a background thread. While a collection’s backfill is in flight, the planner treats that collection’s index-backed plans as not ready and falls back to FullCollection. Two lanes have their own rules: the id-order SortedKeys slice stays available because it is index-independent, and vector lanes ignore pending writes (the executor live-filters deleted ids) but still wait for backfills to finish.
effective_config()now reports durability, index definitions,indexes_ready, a compactquiescenceblock (is_quiescent,index_backfills,pending_index_ops, blob/maintenance work), andstatsin one call.is_indexes_ready()covers open-time recovery only, andquiescence_status()reports the raw counters; both remain valid for Rust callers, while the JS SDK adviseseffectiveConfig()instead.await_quiescent(timeout)waits for all of it, including the async index worker and backfills. Use it before benchmarks and tests.
A unique composite index is the exception: its backfill is synchronous. Creation scans the collection, builds the claim ledger, and rejects the index if existing duplicates exist; the admission gate is active when the call returns.
Writes also gate planning, but per collection: a pending index update in collection A makes A use FullCollection until its worker drains, while collection B keeps its index plans. The global QuiescenceStatus.pending_index_ops still counts every pending update.
Dotted paths
Composite indexes accept dotted field paths. A path is limited to 16 segments; creation rejects empty segments (a..b, leading or trailing dots). At index time the fetcher descends through Map values only:
- A missing path, an empty segment, or descent into a non-
Mapvalue reads as absent. - An array in the middle of a path reads as absent; array element matching is
elemMatch, not dotted-path indexing. - Absent is not null.
Value::Nullis a concrete value and can be indexed.eq(null)matches only an explicit null; an absent path does not matcheq,ne, or any range operator. - A type mismatch is fail-closed: it does not match. No numeric string coercion is implied by indexing.
Missing fields, sparse, and dense
Non-unique indexes skip a document when any indexed field is missing; the sparse flag is inert for them. For a unique index:
sparse: truealso skips a document with a missing field.sparse: false(dense) materializes the missing field asValue::Nullso the document still participates in uniqueness.
Non-scalar values (Map, Array, Reference, BlobLink) encode as tag-only keys and are never claimed by a unique index; uniqueness is defined over scalar tuples.
Filtered (partial) indexes
A composite definition may carry a FilterGroup. Only the matching documents enter the tree. The planner may ride a filtered tree only when the query implies the definition predicate; otherwise it uses a full scan so non-indexed documents are not silently lost.
The planner uses a deliberately narrow rule:
- The definition filter must be
FilterLogic::And. - Every definition condition must be pinned by an AND filter in the query with exactly one of:
Eqwith the same value, orInwith a one-element array containing that value.
Orfilters, ranges,Ne, and other operators are not treated as proof; they fall back to the full-scan lanes.
Extra query filters are allowed. A filtered unique index applies the filter before admission, so documents outside the predicate do not hold claims.
Composite ranges
A composite index can serve one range operator (gt/gte/lt/lte/ne) after an equality prefix, for example index (tenant, score) with tenant = "t2" AND score >= 50. Extra filters are re-checked after the scan, and the lane does not claim filter or order satisfaction; limit still rides the executor’s TopN.
Unique indexes and tombstones
Uniqueness is enforced synchronously at write admission by the claim ledger, not by the asynchronously updated B-tree. The ledger is rebuildable from storage; WAL remains the durability source. A violation is HakoError::UniqueViolation and is not retryable.
The ledger keeps a freed set per unique index. It records distinct document ids whose claim was released (delete, tuple switch, or a release that raced the first fold) so a late recovery fold cannot resurrect a stale claim. Entries are removed when the document claims again. There is no periodic sweep, so the memory bound is the number of distinct released document ids per unique index and grows with churn.
See also
- Queries for explain, covering reads, and pagination.
- Reading data for raw/view read paths.
- Vector search and Graph traversal for those index families.
Common mistakes
- Indexing everything. Each index costs write amplification and memory. Index the fields in your hot
whereclauses; nothing else. - Reading your own writes and panicking. A
putfollowed instantly by a filtered query can briefly miss the index — the fallback sweep still finds the row. If a caller needs strict freshness, callawait_quiescent(). - Forgetting FTS is token-based. Substring edge cases (
startsWithvs token prefix) behave differently; check which one your query needs.