Sort and filter results by Tantivy score

This commit is contained in:
Joakim Soderlund 2020-07-02 12:32:28 +02:00
parent c6a9e88834
commit 1291c4933a
2 changed files with 33 additions and 16 deletions

View file

@ -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::<Vec<_>>();
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::<Vec<_>>()
.join(", ");
println!("{:02.02}% [{:>6}] {}", score, key, title);
println!("{}", tags);
println!("{}", story.short_description);
println!("{}", story.url);
println!();
}
}
}

View file

@ -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<i64> = 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();