feat: add search module and bulk indexing functionality
This commit is contained in:
parent
1891687db2
commit
ffeaa97feb
@ -1 +1,2 @@
|
||||
pub mod hello;
|
||||
pub mod hello;
|
||||
pub mod search;
|
3
src/handler/search.rs
Normal file
3
src/handler/search.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub fn index_new_document() {
|
||||
println!("index_new_document");
|
||||
}
|
@ -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())
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user