Skip to content

Search your app's data

Task: find records by the words in them — notes that mention “invoice”, titles that start with “roa” — without reading every record in the app.

Each node that holds a replica of the app keeps a full-text index (tantivy) of the tables that declare search fields, built from the state it holds. The index is derived data: it is never replicated and never part of the app’s state. A node on an encrypted data disk (mero-tee’s, whose key comes from mero-kms) keeps it on that disk, so it survives a restart; any other node keeps it in memory. A query reads the page of hits, never the table.

Declare the fields to index on the table itself, and the host keeps the index from the app’s state:

#[derive(Record)]
#[table(key = "id", search = ["title", "body"])]
pub struct Note {
pub id: String,
pub title: String,
pub body: String,
}
#[app::view]
pub fn find_notes(&self, text: String) -> app::Result<Vec<Note>> {
let found = self.notes.search(&text, SearchOpts::default().limit(20))?;
Ok(found.items.into_iter().map(|f| f.record).collect())
}
  • The state ABI lists the fields (state.search: table to fields, camelCase). Text fields are indexed; a list of strings is joined; anything else is skipped.
  • Every record of the table is one document. Each replica derives its index from the record keys the entries it applied touched, else from a scan of the table, before a view reads; nothing enters the log.
  • Table::search(text, SearchOpts) queries through the WIT search interface (SearchMode::{Words, Prefix, Substring, Fuzzy}, a cursor and a limit of 1 to 100) and reads each hit’s record back; a hit whose record is gone is skipped. Only views search; any other run traps HostDenied. A call of a declared view runs as a strong view, so it may search too.
  • A view’s searches see exactly the state its reads see, the applied index the view runs at: no hit for a record written after it, and no record deleted after it missing. The node brings the index up to that state before the view’s first search, on the view’s own thread, and the view then searches a point-in-time copy of it, so a write applied while the view runs, and other views searching meanwhile, change nothing it sees. The index is never behind the state a view reads, so a view never waits for indexing to catch up; the first search of a view after writes pays for indexing what they touched.
  • The test harness (TestApp) answers searches with the node’s own index (calimero-search), built from the view’s state: the same words, ranking, scores, snippets and pages a node gives for that state.
  • One index per table, over the fields its #[table(search = [..])] names.
  • A query is at most 256 bytes and 4 KiB encoded; a page at most 100 hits; a cursor at most 10,000 deep.
  • A request makes at most 32 searches (MAX_SEARCHES_PER_RUN), counting the views of other apps its synchronous xcalls reach; past it a search answers "too many searches in one run". Each search costs the view fuel for the documents it matched and the hits it returns (What host work costs).
  • Fuzzy matching is edit distance 1 on words: no stemming, no synonyms, no phonetics. Substring search needs 3 characters.
  • Scores are per app (each index has its own term statistics).
  • A stale view on a replica that is behind searches the state that replica has applied.
  • A new release keeps the index of every table whose search fields it keeps; a new table, or one whose fields changed, is built from state on the first view after it, about 0.1 ms a document.
  • The first view after a restart compares the index with the state and indexes again only the records that changed (on a node that keeps indexes on disk; a node that keeps them in memory builds them again). So does the first view after the app moves to a node, which has no index of it yet.

There is nothing to configure: search is on for every app that declares an index, and its indexes live on the nodes that serve its views (on disk under <data dir>/search when merod runs with --kms-disk-evidence, else in memory). A node keeps the touched entities of the last 4096 entries per app (TOUCHED_RING); a view after a longer gap compares each index with a scan of the state and indexes only what differs.