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
SearchIndextrait is fullyasyncandSend + Sync, making it easy to integrate into Tokio-based protocol handlers. - Schema: indexes
from,to,subject(TEXT + STORED) andbody(TEXT) fields extracted fromrusmes_proto::Mailenvelopes, plus a storedmessage_idfield for result correlation. Additional fields:attachment_filenames(TEXT+STORED),header_values(TEXT), anddate(i64, indexed+stored) for date-range queries. - Ranked results —
searchreturnsSearchResultitems sorted by Tantivy relevance score. - Atomic commits — pending index changes are buffered in memory and flushed to disk
only when
commitis called, enabling batched indexing. - Mutex-guarded writer — the
IndexWriteris protected by astd::sync::Mutexso 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 APIs —
TantivySearchIndex::rebuildre-indexes every message in a storage backend;spawn_reindex_workerruns that on a tokio interval;spawn_incremental_indexersubscribes to the storage event stream and indexes messages as they arrive;TantivySearchIndex::index_size_bytesreports the on-disk footprint. - Schema versioning — a
schema_version.txtsidecar 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§
- Incremental
Config - Tunables for
spawn_incremental_indexer_with_config. - Merge
Policy Config - Tunable parameters for the segment-merge policy.
- Search
Result - Search result containing message ID information
- Tantivy
Search Index - Tantivy-based search index implementation
Enums§
- Search
Error - Search index errors
Constants§
- SCHEMA_
VERSION - Current schema version — increment whenever the Tantivy schema changes.
Traits§
- Search
Index - 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 asMessageStored/MessageExpungedevents arrive. - spawn_
incremental_ indexer_ with_ config - As
spawn_incremental_indexerbut with a custom debounce config. - spawn_
reindex_ worker - Spawn a tokio task that periodically calls
TantivySearchIndex::rebuild.