From 1291c4933a2df15e47a8f23c7d248eb8d20825c2 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Thu, 2 Jul 2020 12:32:28 +0200 Subject: [PATCH] Sort and filter results by Tantivy score --- cli/src/main.rs | 33 ++++++++++++++++++++++----------- search/src/lib.rs | 16 +++++++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index f580d22..2af85ec 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -49,25 +49,36 @@ fn main() { while let Ok(line) = editor.readline(">>> ") { editor.add_history_entry(&line); - - let filter = searcher.parse(&line); - let start = Instant::now(); - let stories = fetcher.filter(&filter); + + let result = searcher + .search(&line) + .into_iter() + .filter(|(_sid, score)| *score > 10f32) + .filter_map(|(sid, score)| Some((fetcher.fetch(sid)?, score))) + .collect::>(); + let finish = (Instant::now() - start).as_millis(); - let count = stories.len(); + let count = result.len(); println!("Found {} stories in {} milliseconds!", count, finish); - if count > 32 { - continue; - } - - for story in stories.iter() { + for (story, score) in result { let key = &story.id; let title = &story.title; - println!("[{}] {}", key, title); + let tags = story + .tags + .iter() + .map(|tag| String::from(&tag.name)) + .collect::>() + .join(", "); + + println!("{:02.02}% [{:>6}] {}", score, key, title); + println!("{}", tags); + println!("{}", story.short_description); + println!("{}", story.url); + println!(); } } } diff --git a/search/src/lib.rs b/search/src/lib.rs index 04e80f8..bb8f0a9 100644 --- a/search/src/lib.rs +++ b/search/src/lib.rs @@ -125,7 +125,7 @@ impl Searcher { println!("Index generated in {} seconds.", finish); } - pub fn parse(&self, text: &str) -> impl Fn(&Story) -> bool + Sync { + pub fn search(&self, text: &str) -> Vec<(i64, f32)> { let reader = self .index .reader_builder() @@ -138,12 +138,11 @@ impl Searcher { let content = self.schema.get_field("content").unwrap(); let parser = QueryParser::for_index(&self.index, vec![content]); - let limit = TopDocs::with_limit(16); + let limit = TopDocs::with_limit(32); let query = parser.parse_query(&text).unwrap(); let docs = searcher.search(&query, &limit).unwrap(); - let mut sids: Vec = docs - .into_iter() + docs.into_iter() .map(|(score, address)| { let doc = searcher.doc(address).unwrap(); @@ -152,7 +151,14 @@ impl Searcher { _ => panic!("Invalid story key type!"), } }) - .filter(|(_, score)| *score > 0.1) + .collect() + } + + pub fn parse(&self, text: &str) -> impl Fn(&Story) -> bool + Sync { + let mut sids: Vec<_> = self + .search(text) + .into_iter() + .filter(|(_, score)| *score > 10f32) .map(|(sid, _)| sid) .collect();