pub trait IndexBackend: Send + Sync {
// Required methods
fn index(&self, memory: &Memory) -> Result<()>;
fn remove(&self, id: &MemoryId) -> Result<bool>;
fn search(
&self,
query: &str,
filter: &SearchFilter,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>;
fn clear(&self) -> Result<()>;
fn list_all(
&self,
filter: &SearchFilter,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>;
fn get_memory(&self, id: &MemoryId) -> Result<Option<Memory>>;
// Provided methods
fn reindex(&self, memories: &[Memory]) -> Result<()> { ... }
fn get_memories_batch(
&self,
ids: &[MemoryId],
) -> Result<Vec<Option<Memory>>> { ... }
}Expand description
Trait for index layer backends.
Index backends provide full-text search capabilities using BM25 or similar algorithms.
§Implementor Notes
- Methods use
&selfto enable sharing viaArc<dyn IndexBackend> - Use interior mutability (e.g.,
Mutex<Connection>) for mutable state - Implement
get_memories_batch()with an optimized query (e.g., SQLINclause) - Use FTS ranking scores for the
f32score in search results - Ensure
clear()does not affect the persistence layer
Required Methods§
Sourcefn index(&self, memory: &Memory) -> Result<()>
fn index(&self, memory: &Memory) -> Result<()>
Indexes a memory for full-text search.
Uses interior mutability for thread-safe concurrent access.
§Errors
Returns an error if the indexing operation fails.
Sourcefn remove(&self, id: &MemoryId) -> Result<bool>
fn remove(&self, id: &MemoryId) -> Result<bool>
Removes a memory from the index.
Uses interior mutability for thread-safe concurrent access.
§Errors
Returns an error if the removal operation fails.
Sourcefn search(
&self,
query: &str,
filter: &SearchFilter,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>
fn search( &self, query: &str, filter: &SearchFilter, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>
Searches for memories matching a text query.
Returns memory IDs with their BM25 scores, ordered by relevance.
§Errors
Returns an error if the search operation fails.
Sourcefn clear(&self) -> Result<()>
fn clear(&self) -> Result<()>
Clears the entire index.
Uses interior mutability for thread-safe concurrent access.
§Errors
Returns an error if the clear operation fails.
Provided Methods§
Sourcefn reindex(&self, memories: &[Memory]) -> Result<()>
fn reindex(&self, memories: &[Memory]) -> Result<()>
Re-indexes all memories.
Uses interior mutability for thread-safe concurrent access.
§Errors
Returns an error if any memory fails to index.
Sourcefn get_memories_batch(&self, ids: &[MemoryId]) -> Result<Vec<Option<Memory>>>
fn get_memories_batch(&self, ids: &[MemoryId]) -> Result<Vec<Option<Memory>>>
Retrieves multiple memories by their IDs in a single batch query.
This is more efficient than calling get_memory in a loop (N+1 query pattern).
Returns memories in the same order as the input IDs, with None for missing IDs.
§Errors
Returns an error if the operation fails.