Skip to content

Rust API

The Rust API reference is on docs.rs/typemind. Enable the store feature for databases, and aws (or aws-credentials), gcp or azure for cloud storage. The database API is async and runs on Tokio. From synchronous code, typemind::block_on runs one of its futures on typemind's internal runtime; do not call it from async code.

[dependencies]
typemind = { version = "0.1", features = ["store"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

Indexes and engines

Item Feature Role
Document A document: Document::new(id, text, popularity) or Document::keyed(key, text, popularity), with with_synonym, with_abbreviation, with_context and with_vector.
Index Segments searched as one: from_documents, complete, complete_aliases, vector_search, hybrid_search, and the _with variants taking options.
Segment, SegmentWriter An immutable, memory-mapped set of documents and deletes; and a writer of segment files in bounded memory.
Engine Indexes by name, published atomically, searched as layers. Returns LayeredSuggestions.
Suggestion, AliasSuggestion, HybridSuggestion Results, with id, key, text, score and, for completions, kind and byte-range highlights.
MatchKind, Fusion Match kinds, and hybrid fusion methods.
BuildOptions, IndexOptions, SearchOptions, HybridOptions Settings, with chainable setters.
Error Io, Corrupt, InvalidInput, NotFound, Conflict and Store, among others.
use std::sync::Arc;
use typemind::{AliasKind, Document, Index, IndexOptions, MatchKind, Segment};

let docs = [
    Document::new(1, "Under the Bridge – Red Hot Chili Peppers", 0.75)
        .with_alias("RHCP", AliasKind::Abbreviation),
    Document::new(2, "Bohemian Rhapsody – Queen", 0.95),
];
let index = Index::new(vec![Arc::new(Segment::build(docs, [])?)], IndexOptions::default())?;
let hits = index.complete("rhcp", 10);
assert_eq!((hits[0].id, hits[0].kind), (1, MatchKind::Abbreviation));

Databases

Item Feature Role
Database store Versioned manifests of named indexes in a store: Database::open(url, options), begin, open_index, compact, cleanup, acquire_lease, submit.
Transaction store An optimistic, atomic commit.
Replica store Publishes a database's latest version into an Engine: sync, and follow, which syncs from a background thread.
Follower, FollowStatus store The handle Replica::follow returns; dropping it stops the thread. status() returns synced_at_ms and error.
Lease store An expiring, fenced lock in the database.
ChangeSet store Upserts and deletes submitted to the inbox.
Ingestor, IngestStep store The lease-elected committer of change sets, and the result of a round.
CompactionPolicy, CleanupPolicy store Maintenance settings.
Store, BlockingStore store Object-store access, async and blocking.
use std::{sync::Arc, time::Duration};
use typemind::{Database, Document, Engine, IndexOptions, Replica};

#[tokio::main]
async fn main() -> Result<(), typemind::Error> {
    let database = Database::open("./data", Vec::<(String, String)>::new()).await?;
    let mut txn = database.begin().await?;
    txn.append_documents("songs", [Document::keyed("bohemian", "Bohemian Rhapsody – Queen", 0.95)], [])?;
    txn.commit().await?;

    let engine = Arc::new(Engine::new());
    let replica = Arc::new(Replica::new(database, IndexOptions::default()));
    replica.sync(&engine).await?;
    let _follower = replica.follow(&engine, Duration::from_secs(5));   // syncs until dropped
    let hits = engine.complete(&["songs"], "boh", 10)?;
    Ok(())
}

Runnable examples: quickstart.rs for an index and a database, and live_updates.rs for a followed database with an ingestor.

Collections

The collections layer. In Rust, typemind::connect returns this client; a database is opened with Database::open.

Item Feature Role
connect, ConnectOptions store Opens a client on the database at a URL: sync_every, cache_dir, storage options, build and index options.
Client store A database and its collections: collections, collection, create_collection, get_or_create_collection, drop_collection, sync, and database().
Collection store Named documents: add, delete, complete, optimize and stats.
Query store limit, aliases, layers, contexts and vector, with chainable setters.
Completion store A result: id, key, text, score, kind, byte-range highlights and collection.
CollectionStats store Size and freshness, from Collection::stats.
Optimize store Auto { policy, min_interval } or Off.
use typemind::{connect, ConnectOptions, Document, MatchKind, Optimize, Query};

#[tokio::main]
async fn main() -> Result<(), typemind::Error> {
    let client = connect("memory://", ConnectOptions::default()).await?;
    let songs = client.get_or_create_collection("songs", Optimize::default()).await?;
    songs
        .add([
            Document::keyed("bridge", "Under the Bridge – Red Hot Chili Peppers", 0.75)
                .with_abbreviation("RHCP"),
        ])
        .await?;
    let hits = songs.complete("rhcp", &Query::default())?;
    assert_eq!((hits[0].key.as_deref(), hits[0].kind), (Some("bridge"), MatchKind::Abbreviation));
    Ok(())
}