2023-03-07 02:21:55 +08:00
|
|
|
use super::interner::Interned;
|
|
|
|
use super::query_term::{Phrase, QueryTerm, WordDerivations};
|
|
|
|
use super::{QueryGraph, QueryNode, SearchContext};
|
|
|
|
use crate::{CboRoaringBitmapCodec, Result, RoaringBitmapCodec};
|
2023-02-21 19:55:44 +08:00
|
|
|
use fxhash::FxHashMap;
|
2023-03-07 02:21:55 +08:00
|
|
|
use heed::BytesDecode;
|
2023-02-21 16:45:17 +08:00
|
|
|
use roaring::{MultiOps, RoaringBitmap};
|
2023-03-07 02:21:55 +08:00
|
|
|
use std::collections::VecDeque;
|
2023-02-21 16:45:17 +08:00
|
|
|
|
|
|
|
// TODO: manual performance metrics: access to DB, bitmap deserializations/operations, etc.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NodeDocIdsCache {
|
2023-02-21 20:21:41 +08:00
|
|
|
pub cache: FxHashMap<u32, RoaringBitmap>,
|
|
|
|
}
|
2023-03-07 02:21:55 +08:00
|
|
|
impl<'search> SearchContext<'search> {
|
|
|
|
fn get_node_docids<'cache>(
|
2023-02-21 20:21:41 +08:00
|
|
|
&'cache mut self,
|
|
|
|
term: &QueryTerm,
|
|
|
|
node_idx: u32,
|
|
|
|
) -> Result<&'cache RoaringBitmap> {
|
2023-03-07 02:21:55 +08:00
|
|
|
if self.node_docids_cache.cache.contains_key(&node_idx) {
|
|
|
|
return Ok(&self.node_docids_cache.cache[&node_idx]);
|
2023-02-21 20:21:41 +08:00
|
|
|
};
|
|
|
|
let docids = match term {
|
2023-03-07 02:21:55 +08:00
|
|
|
QueryTerm::Phrase { phrase } => resolve_phrase(self, *phrase)?,
|
2023-02-21 20:21:41 +08:00
|
|
|
QueryTerm::Word {
|
|
|
|
derivations:
|
2023-03-03 04:27:57 +08:00
|
|
|
WordDerivations {
|
|
|
|
original,
|
|
|
|
zero_typo,
|
|
|
|
one_typo,
|
|
|
|
two_typos,
|
|
|
|
use_prefix_db,
|
|
|
|
synonyms,
|
|
|
|
split_words,
|
|
|
|
},
|
2023-02-21 20:21:41 +08:00
|
|
|
} => {
|
2023-03-03 04:27:57 +08:00
|
|
|
let mut or_docids = vec![];
|
2023-03-07 02:21:55 +08:00
|
|
|
for word in zero_typo.iter().chain(one_typo.iter()).chain(two_typos.iter()).copied()
|
|
|
|
{
|
|
|
|
if let Some(word_docids) = self.get_word_docids(word)? {
|
2023-03-03 04:27:57 +08:00
|
|
|
or_docids.push(word_docids);
|
2023-02-21 20:21:41 +08:00
|
|
|
}
|
2023-03-03 04:27:57 +08:00
|
|
|
}
|
|
|
|
if *use_prefix_db {
|
2023-03-07 02:21:55 +08:00
|
|
|
if let Some(prefix_docids) = self.get_prefix_docids(*original)? {
|
2023-03-03 04:27:57 +08:00
|
|
|
or_docids.push(prefix_docids);
|
2023-02-21 20:21:41 +08:00
|
|
|
}
|
2023-03-03 04:27:57 +08:00
|
|
|
}
|
|
|
|
let mut docids = or_docids
|
2023-02-21 20:21:41 +08:00
|
|
|
.into_iter()
|
2023-03-03 04:27:57 +08:00
|
|
|
.map(|slice| RoaringBitmapCodec::bytes_decode(slice).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
2023-03-07 02:21:55 +08:00
|
|
|
for synonym in synonyms.iter().copied() {
|
2023-03-03 04:27:57 +08:00
|
|
|
// TODO: cache resolve_phrase?
|
2023-03-07 02:21:55 +08:00
|
|
|
docids.push(resolve_phrase(self, synonym)?);
|
2023-03-03 04:27:57 +08:00
|
|
|
}
|
2023-03-07 02:21:55 +08:00
|
|
|
if let Some(split_words) = split_words {
|
|
|
|
docids.push(resolve_phrase(self, *split_words)?);
|
2023-03-03 04:27:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MultiOps::union(docids)
|
2023-02-21 20:21:41 +08:00
|
|
|
}
|
|
|
|
};
|
2023-03-07 02:21:55 +08:00
|
|
|
let _ = self.node_docids_cache.cache.insert(node_idx, docids);
|
|
|
|
let docids = &self.node_docids_cache.cache[&node_idx];
|
2023-02-21 20:21:41 +08:00
|
|
|
Ok(docids)
|
|
|
|
}
|
2023-02-21 16:45:17 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 02:21:55 +08:00
|
|
|
pub fn resolve_query_graph<'search>(
|
|
|
|
ctx: &mut SearchContext<'search>,
|
2023-02-21 16:45:17 +08:00
|
|
|
q: &QueryGraph,
|
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<RoaringBitmap> {
|
|
|
|
// TODO: there is definitely a faster way to compute this big
|
|
|
|
// roaring bitmap expression
|
|
|
|
|
2023-02-21 19:33:32 +08:00
|
|
|
let mut nodes_resolved = RoaringBitmap::new();
|
2023-02-21 20:21:41 +08:00
|
|
|
let mut path_nodes_docids = vec![RoaringBitmap::new(); q.nodes.len()];
|
2023-02-21 16:45:17 +08:00
|
|
|
|
|
|
|
let mut next_nodes_to_visit = VecDeque::new();
|
|
|
|
next_nodes_to_visit.push_front(q.root_node);
|
|
|
|
|
|
|
|
while let Some(node) = next_nodes_to_visit.pop_front() {
|
2023-02-21 19:55:44 +08:00
|
|
|
let predecessors = &q.edges[node as usize].predecessors;
|
2023-02-21 16:45:17 +08:00
|
|
|
if !predecessors.is_subset(&nodes_resolved) {
|
|
|
|
next_nodes_to_visit.push_back(node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Take union of all predecessors
|
2023-02-21 20:21:41 +08:00
|
|
|
let predecessors_iter = predecessors.iter().map(|p| &path_nodes_docids[p as usize]);
|
2023-02-21 16:45:17 +08:00
|
|
|
let predecessors_docids = MultiOps::union(predecessors_iter);
|
|
|
|
|
2023-02-21 19:55:44 +08:00
|
|
|
let n = &q.nodes[node as usize];
|
2023-03-03 04:27:57 +08:00
|
|
|
|
2023-02-21 16:45:17 +08:00
|
|
|
let node_docids = match n {
|
2023-03-03 04:27:57 +08:00
|
|
|
QueryNode::Term(located_term) => {
|
2023-02-21 16:45:17 +08:00
|
|
|
let term = &located_term.value;
|
2023-03-07 02:21:55 +08:00
|
|
|
let derivations_docids = ctx.get_node_docids(term, node)?;
|
2023-02-21 20:21:41 +08:00
|
|
|
predecessors_docids & derivations_docids
|
2023-02-21 16:45:17 +08:00
|
|
|
}
|
2023-03-03 04:27:57 +08:00
|
|
|
QueryNode::Deleted => {
|
2023-02-21 20:21:41 +08:00
|
|
|
panic!()
|
2023-02-21 16:45:17 +08:00
|
|
|
}
|
2023-03-03 04:27:57 +08:00
|
|
|
QueryNode::Start => universe.clone(),
|
|
|
|
QueryNode::End => {
|
2023-02-21 16:45:17 +08:00
|
|
|
return Ok(predecessors_docids);
|
|
|
|
}
|
|
|
|
};
|
2023-02-21 19:55:44 +08:00
|
|
|
nodes_resolved.insert(node);
|
2023-02-21 20:21:41 +08:00
|
|
|
path_nodes_docids[node as usize] = node_docids;
|
2023-02-21 16:45:17 +08:00
|
|
|
|
2023-02-21 19:55:44 +08:00
|
|
|
for succ in q.edges[node as usize].successors.iter() {
|
|
|
|
if !next_nodes_to_visit.contains(&succ) && !nodes_resolved.contains(succ) {
|
|
|
|
next_nodes_to_visit.push_back(succ);
|
2023-02-21 16:45:17 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 20:57:34 +08:00
|
|
|
|
2023-02-21 16:45:17 +08:00
|
|
|
// This is currently slow but could easily be implemented very efficiently
|
2023-02-21 19:55:44 +08:00
|
|
|
for prec in q.edges[node as usize].predecessors.iter() {
|
2023-02-21 19:33:32 +08:00
|
|
|
if q.edges[prec as usize].successors.is_subset(&nodes_resolved) {
|
2023-02-21 20:21:41 +08:00
|
|
|
path_nodes_docids[prec as usize].clear();
|
2023-02-21 16:45:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!()
|
|
|
|
}
|
2023-03-03 04:27:57 +08:00
|
|
|
|
2023-03-07 02:21:55 +08:00
|
|
|
pub fn resolve_phrase(ctx: &mut SearchContext, phrase: Interned<Phrase>) -> Result<RoaringBitmap> {
|
|
|
|
let Phrase { words } = ctx.phrase_interner.get(phrase).clone();
|
2023-03-03 04:27:57 +08:00
|
|
|
let mut candidates = RoaringBitmap::new();
|
|
|
|
let mut first_iter = true;
|
|
|
|
let winsize = words.len().min(3);
|
|
|
|
|
|
|
|
if words.is_empty() {
|
|
|
|
return Ok(candidates);
|
|
|
|
}
|
|
|
|
|
|
|
|
for win in words.windows(winsize) {
|
|
|
|
// Get all the documents with the matching distance for each word pairs.
|
|
|
|
let mut bitmaps = Vec::with_capacity(winsize.pow(2));
|
2023-03-07 02:21:55 +08:00
|
|
|
for (offset, &s1) in win
|
2023-03-03 04:27:57 +08:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, word)| word.as_ref().map(|word| (index, word)))
|
|
|
|
{
|
2023-03-07 02:21:55 +08:00
|
|
|
for (dist, &s2) in win
|
2023-03-03 04:27:57 +08:00
|
|
|
.iter()
|
|
|
|
.skip(offset + 1)
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, word)| word.as_ref().map(|word| (index, word)))
|
|
|
|
{
|
|
|
|
if dist == 0 {
|
2023-03-07 02:21:55 +08:00
|
|
|
match ctx.get_word_pair_proximity_docids(s1, s2, 1)? {
|
2023-03-03 04:27:57 +08:00
|
|
|
Some(m) => bitmaps.push(CboRoaringBitmapCodec::deserialize_from(m)?),
|
|
|
|
// If there are no documents for this pair, there will be no
|
|
|
|
// results for the phrase query.
|
|
|
|
None => return Ok(RoaringBitmap::new()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut bitmap = RoaringBitmap::new();
|
|
|
|
for dist in 0..=dist {
|
2023-03-07 02:21:55 +08:00
|
|
|
if let Some(m) =
|
|
|
|
ctx.get_word_pair_proximity_docids(s1, s2, dist as u8 + 1)?
|
|
|
|
{
|
2023-03-03 04:27:57 +08:00
|
|
|
bitmap |= CboRoaringBitmapCodec::deserialize_from(m)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bitmap.is_empty() {
|
|
|
|
return Ok(bitmap);
|
|
|
|
} else {
|
|
|
|
bitmaps.push(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We sort the bitmaps so that we perform the small intersections first, which is faster.
|
|
|
|
bitmaps.sort_unstable_by_key(|a| a.len());
|
|
|
|
|
|
|
|
for bitmap in bitmaps {
|
|
|
|
if first_iter {
|
|
|
|
candidates = bitmap;
|
|
|
|
first_iter = false;
|
|
|
|
} else {
|
|
|
|
candidates &= bitmap;
|
|
|
|
}
|
|
|
|
// There will be no match, return early
|
|
|
|
if candidates.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(candidates)
|
|
|
|
}
|