Depth-first search cache the docids unions

This commit is contained in:
Kerollmops 2020-09-28 16:53:37 +02:00
parent a3821a0b33
commit 6ddb3e722c
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -1,4 +1,5 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use fst::{IntoStreamer, Streamer}; use fst::{IntoStreamer, Streamer};
use levenshtein_automata::DFA; use levenshtein_automata::DFA;
@ -215,13 +216,16 @@ impl<'a> Search<'a> {
words: &[(HashMap<String, (u8, RoaringBitmap)>, RoaringBitmap)], words: &[(HashMap<String, (u8, RoaringBitmap)>, RoaringBitmap)],
candidates: &RoaringBitmap, candidates: &RoaringBitmap,
parent_docids: Option<&RoaringBitmap>, parent_docids: Option<&RoaringBitmap>,
union_cache: &mut HashMap<(usize, u8), RoaringBitmap>,
) -> anyhow::Result<Option<RoaringBitmap>> ) -> anyhow::Result<Option<RoaringBitmap>>
{ {
let (words1, words2) = (&words[0].0, &words[1].0); let (words1, words2) = (&words[0].0, &words[1].0);
let pairs = words_pair_combinations(words1, words2); let pairs = words_pair_combinations(words1, words2);
for proximity in 1..=8 { for proximity in 1..=8 {
let mut docids = match union_cache.entry((words.len(), proximity)) {
Occupied(entry) => entry.get().clone(),
Vacant(entry) => {
let mut docids = RoaringBitmap::new(); let mut docids = RoaringBitmap::new();
if proximity == 8 { if proximity == 8 {
docids = candidates.clone(); docids = candidates.clone();
@ -232,6 +236,9 @@ impl<'a> Search<'a> {
docids.union_with(&di); docids.union_with(&di);
} }
} }
}
entry.insert(docids).clone()
}
}; };
if let Some(parent_docids) = &parent_docids { if let Some(parent_docids) = &parent_docids {
@ -242,7 +249,7 @@ impl<'a> Search<'a> {
let words = &words[1..]; let words = &words[1..];
// We are the last word. // We are the last word.
if words.len() < 2 { return Ok(Some(docids)) } if words.len() < 2 { return Ok(Some(docids)) }
if let Some(di) = depth_first_search(index, rtxn, words, candidates, Some(&docids))? { if let Some(di) = depth_first_search(index, rtxn, words, candidates, Some(&docids), union_cache)? {
return Ok(Some(di)) return Ok(Some(di))
} }
} }
@ -251,8 +258,10 @@ impl<'a> Search<'a> {
Ok(None) Ok(None)
} }
let mut union_cache = HashMap::new();
let answer = depth_first_search(index, rtxn, &derived_words, &candidates, None, &mut union_cache)?;
let mut documents = Vec::new(); let mut documents = Vec::new();
let answer = depth_first_search(index, rtxn, &derived_words, &candidates, None)?;
if let Some(answer) = answer { if let Some(answer) = answer {
documents.push(answer); documents.push(answer);
} }