Skip to main content
Swift SDK / MossSession

MossSession

An on-device index handle for a single index, returned by MossClient.session(_:options:). All embedding runs locally with the bundled model; queries don’t hit the network. A session can be persisted to disk (save / loadFromDisk) or synced to the cloud (pushIndex / loadIndex). The class is thread-safe. close() (also called on deinit) blocks until in-flight calls return before freeing the native handle.

Example

Properties

name

The index name this session was opened against.

docCount

Current document count in the index.

Methods

close()

Frees the native handle. Idempotent; also called on deinit.

addDocs(_:upsert:)

Adds or upserts documents, embedding them on-device. Returns the counts of rows added (new ids) and updated (existing ids). Takes [DocumentInfo].

deleteDocs(_:)

Deletes documents by id. Returns the number actually deleted (missing ids are ignored).

getDocs(_:)

Returns documents by id, or all documents when docIds is nil. An empty array returns nothing. Returns [DocumentInfo].

getDocs(ids:)

Deterministic fetch of exact ids, returned in the order requested. Missing ids are skipped.

getDocs(where:sortBy:ascending:)

Fetch by a metadata predicate — no embedding, no similarity ranking. Build filter with the typed Filter DSL; sortBy orders by a metadata field (numeric-aware).

getDocs(options:)

Full-control deterministic fetch — combine ids, filter, sortBy, ascending, and groupByParent via GetDocsOptions. See the Exact / Graph Retrieval guide for worked examples of all four overloads, the Filter DSL, and parent grouping.

query(_:options:)

Embeds q on-device and runs a local similarity search. Tune with QueryOptions - hybrid alpha and metadata filtering are covered in the Querying guide. Returns a SearchResult.

query(_:embedding:options:)

Search variant that takes a caller-provided embedding, bypassing the on-device model forward pass.

save(toCachePath:)

Persists the session’s index to disk under cachePath so it can be reopened on the next launch without re-embedding.

loadFromDisk(cachePath:)

Restores a session previously written with save(toCachePath:). Returns the document count restored. The session’s name must match the one used at save time.

pushIndex()

Pushes the in-memory session to the cloud as a server-side index. Returns a PushIndexResult with a jobId; poll MossClient.getJobStatus until the status is ready.

loadIndex(_:)

Pulls a server-side index into this session as a one-time hydration (returns the doc count loaded, 0 if no such cloud index). The session then behaves as a local one - subsequent add/delete/query don’t hit the network.