Skip to main content

Crate rusmes_search

Crate rusmes_search 

Source
Expand description

Full-text search for RusMES

This crate provides a Tantivy-backed full-text search index for mail messages. It exposes a trait-based abstraction so that the rest of the RusMES system can remain decoupled from the underlying search engine.

§Key Features

  • Tantivy back-end — uses Tantivy for on-disk inverted-index full-text search with BM25 ranking.
  • Async interface — the SearchIndex trait is fully async and Send + Sync, making it easy to integrate into Tokio-based protocol handlers.
  • Schema: indexes from, to, subject (TEXT + STORED) and body (TEXT) fields extracted from rusmes_proto::Mail envelopes, plus a stored message_id field for result correlation. Additional fields: attachment_filenames (TEXT+STORED), header_values (TEXT), and date (i64, indexed+stored) for date-range queries.
  • Ranked resultssearch returns SearchResult items sorted by Tantivy relevance score.
  • Atomic commits — pending index changes are buffered in memory and flushed to disk only when commit is called, enabling batched indexing.
  • Mutex-guarded writer — the IndexWriter is protected by a std::sync::Mutex so that multiple async tasks can share the same index safely.
  • Result caching — see cache::ResultCache. Lookups are short-circuited by an LRU cache (capacity 256 by default); writes bump a global version stamp that invalidates all entries.
  • Maintenance APIsTantivySearchIndex::rebuild re-indexes every message in a storage backend; spawn_reindex_worker runs that on a tokio interval; spawn_incremental_indexer subscribes to the storage event stream and indexes messages as they arrive; TantivySearchIndex::index_size_bytes reports the on-disk footprint.
  • Schema versioning — a schema_version.txt sidecar file gates schema compatibility. On version mismatch, the index directory is purged and rebuilt with the current schema.

§Usage

use rusmes_search::{TantivySearchIndex, SearchIndex};
use std::path::Path;

// Create or open an index at the given path
let index = TantivySearchIndex::new("/var/lib/rusmes/search")?;

// Search (returns up to `limit` results ranked by relevance)
let results = index.search("quarterly report", 10).await?;
for r in &results {
    println!("message_uuid={} score={}", r.message_uuid, r.score);
}

// Commit pending writes before querying new documents
index.commit().await?;

§Opening vs Creating

Use TantivySearchIndex::new when creating an index for the first time. Use TantivySearchIndex::open to reopen an existing index after a restart. Both paths fail with a SearchError if the directory cannot be accessed or the schema is incompatible.

§Error Handling

All fallible operations return Result<T> which aliases std::result::Result<T, SearchError>. The SearchError enum covers Tantivy engine errors, query parse failures, I/O errors, and missing-message conditions.

Re-exports§

pub use query_translator::jmap_filter_to_tantivy;
pub use query_translator::parse_search_term;
pub use query_translator::search_query_to_tantivy;
pub use query_translator::JmapSearchFilter;
pub use query_translator::SearchComparator;
pub use query_translator::SearchCondition;
pub use query_translator::SearchField;
pub use query_translator::SearchQuery;
pub use query_translator::TermKind;
pub use cache::ResultCache;

Modules§

cache
LRU result cache for the search index.
query_translator
Query translation layer: protocol search filters → Tantivy queries

Structs§

IncrementalConfig
Tunables for spawn_incremental_indexer_with_config.
MergePolicyConfig
Tunable parameters for the segment-merge policy.
SearchResult
Search result containing message ID information
TantivySearchIndex
Tantivy-based search index implementation

Enums§

SearchError
Search index errors

Constants§

SCHEMA_VERSION
Current schema version — increment whenever the Tantivy schema changes.

Traits§

SearchIndex
Search index trait for message indexing and querying

Functions§

spawn_incremental_indexer
Spawn a tokio task that subscribes to store.event_stream() and indexes (or deletes) messages as MessageStored / MessageExpunged events arrive.
spawn_incremental_indexer_with_config
As spawn_incremental_indexer but with a custom debounce config.
spawn_reindex_worker
Spawn a tokio task that periodically calls TantivySearchIndex::rebuild.

Type Aliases§

Result