Terraphim Service
The terraphim_service crate is the core service layer that provides document search, ranking, and management capabilities for the Terraphim system.
Architecture
Core Components
TerraphimService
The main service struct that orchestrates all document operations:
Key Responsibilities:
- Document search and ranking
- Role-based access control
- Knowledge graph integration
- Document persistence
- AI enhancement integration
Search Operations
The service provides multiple search methods:
Scoring System
QueryScorer Enum
Defines available scoring algorithms:
Scorer Implementation
The scoring system supports multiple algorithms:
API Reference
Document Search
Basic Search
let mut service = new;
let search_query = SearchQuery ;
let documents = service.search.await?;Role-Based Search
let documents = service.search_documents_selected_role.await?;Document Management
Create Document
let document = Document ;
let created_doc = service.create_document.await?;Get Document by ID
let document = service.get_document_by_id.await?;Configuration Management
Fetch Configuration
let config = service.fetch_config.await;Update Configuration
let updated_config = service.update_config.await?;Update Selected Role
let config = service.update_selected_role.await?;Scoring Algorithms
BM25 Variants
Standard BM25
let query = new.name_scorer;
let documents = sort_documents;Fielded BM25 (BM25F)
let weights = FieldWeights ;Enhanced BM25 (BM25Plus)
let params = BM25Params ;Similarity-Based Scoring
Levenshtein Distance
let query = new.similarity;Jaro Distance
let query = new.similarity;Jaro-Winkler Distance
let query = new.similarity;Knowledge Graph Integration
Thesaurus Management
// Build thesaurus for search
service.build_thesaurus.await?;
// Ensure thesaurus is loaded for role
let thesaurus = service.ensure_thesaurus_loaded.await?;Document Preprocessing
// Apply KG preprocessing to documents
if role.terraphim_it AI Enhancement
OpenRouter Integration
if role.has_openrouter_config Document Summarization
let summary = service.generate_document_summary.await?;Error Handling
The service uses a custom error type:
Performance Considerations
Async Operations
All major operations are async to support non-blocking I/O:
- Document search and ranking
- Configuration management
- AI enhancement
- Knowledge graph operations
Caching
The service implements caching for:
- Configuration state
- Thesaurus data
- Document persistence
- Role-based settings
Memory Management
- Efficient document indexing
- Minimal memory allocations
- Shared type usage across crates
Testing
Unit Tests
Integration Tests
Configuration
Role-Based Configuration
Environment Variables
TERRAPHIM_CONFIG_PATH: Configuration file pathTERRAPHIM_LOG_LEVEL: Logging levelOPENROUTER_API_KEY: AI enhancement API key
Dependencies
Internal Dependencies
terraphim_types: Core type definitionsterraphim_config: Configuration managementterraphim_persistence: Document storageterraphim_middleware: Document processing
External Dependencies
tokio: Async runtimeserde: Serializationlog: Loggingreqwest: HTTP client (for AI integration)
Best Practices
Error Handling
match service.search.await Async/Await Usage
// Always use .await for async operations
let documents = service.search.await?;
let config = service.fetch_config.await;Resource Management
// Use RAII for resource management
let service = new;
// Service is automatically cleaned up when droppedMigration Guide
From Similarity-Based to BM25
- Update role configuration to use BM25 scorers
- Test with your document corpus
- Adjust parameters as needed
- Monitor performance and relevance
Adding New Scoring Algorithms
- Implement the algorithm in the appropriate module
- Add to
QueryScorerenum - Update scoring logic in
Scorer::score_documents - Add tests for the new algorithm
- Update documentation