feat: add search module and bulk indexing functionality

This commit is contained in:
Alex Wellnitz 2024-02-11 17:22:11 +01:00
parent 1891687db2
commit ffeaa97feb
4 changed files with 24 additions and 1 deletions

View File

@ -1 +1,2 @@
pub mod hello;
pub mod hello;
pub mod search;

3
src/handler/search.rs Normal file
View File

@ -0,0 +1,3 @@
pub fn index_new_document() {
println!("index_new_document");
}

View File

@ -79,6 +79,12 @@ impl SearchEngine {
}
}
pub fn bulk_index(&mut self, documents: Vec<(&str, &str)>) {
for (url, content) in documents {
self.index(url, content);
}
}
pub fn get_urls(&self, keyword: &str) -> HashMap<String, i32> {
let keyword = normalize_string(keyword);
self.index.get(&keyword).cloned().unwrap_or(HashMap::new())

View File

@ -13,4 +13,17 @@ mod tests {
assert_eq!(search_engine.posts().len(), 1);
assert_eq!(search_engine.number_of_documents(), 1);
}
#[test]
fn test_bulk_index() {
let mut search_engine = SearchEngine::new(1.2, 0.75);
search_engine.bulk_index(vec![
("https://www.rust-lang.org/", "Rust Programming Language"),
("https://www.wikipedia.com/", "Rust Programming Language"),
]);
assert_eq!(search_engine.posts().len(), 2);
assert_eq!(search_engine.number_of_documents(), 2);
}
}