Implement powerful text search with TF-IDF ranking and Unicode tokenization
Jasonisnthappy includes a built-in full-text search engine that uses TF-IDF (Term Frequency-Inverse Document Frequency) scoring to rank search results by relevance.
use jasonisnthappy::Database;use serde_json::json;let db = Database::open("my.db")?;// Create text index on "content" fielddb.create_text_index( "posts", // collection name "content_idx", // index name &["content"] // fields to index)?;println!("Text index created!");
// Index both title and body for blog postsdb.create_text_index( "posts", "search_idx", &["title", "body"] // Multiple fields)?;// Index product name and descriptiondb.create_text_index( "products", "product_search", &["name", "description", "tags"])?;
Include all fields you want to search in a single text index. Multiple text indexes on the same collection are supported but each search uses only one index.
Search for documents and get results sorted by relevance.
let posts = db.collection("posts");// Insert some documentsposts.insert(json!({ "title": "Introduction to Rust", "body": "Rust is a systems programming language focused on safety and performance."}))?;posts.insert(json!({ "title": "Building a Database in Rust", "body": "Learn how to build a high-performance embedded database using Rust."}))?;// Search (returns results ranked by relevance)let results = posts.search("rust database")?;for result in results { println!("Doc ID: {} (score: {:.2})", result.doc_id, result.score); // Fetch the full document let doc = posts.find_by_id(&result.doc_id)?; println!("Title: {}", doc["title"]);}
Search for multiple terms - documents matching more terms score higher.
// Search for "rust database performance"let results = posts.search("rust database performance")?;// Documents containing all three terms rank highest// Documents with 2 terms rank middle// Documents with 1 term rank lowest
use jasonisnthappy::Database;use serde_json::json;let db = Database::open("blog.db")?;// Create text index on title and contentdb.create_text_index("posts", "search_idx", &["title", "body"])?;let posts = db.collection("posts");// Insert blog postsposts.insert(json!({ "title": "Getting Started with Rust", "body": "Rust is a modern systems programming language...", "author": "Alice", "published": true}))?;posts.insert(json!({ "title": "Advanced Rust Patterns", "body": "Explore advanced Rust programming patterns...", "author": "Bob", "published": true}))?;// Search published postslet results = posts.search("rust programming")?;for result in results.iter().take(5) { let post = posts.find_by_id(&result.doc_id)?; if post["published"].as_bool().unwrap_or(false) { println!("📝 {} (by {})", post["title"], post["author"]); println!(" Relevance: {:.2}", result.score); }}