2023-02-22 22:34:37 +08:00
|
|
|
use super::logger::SearchLogger;
|
2023-03-30 19:15:43 +08:00
|
|
|
use super::query_graph::QueryNode;
|
2023-03-30 17:10:38 +08:00
|
|
|
use super::resolve_query_graph::compute_query_graph_docids;
|
2023-03-30 19:15:43 +08:00
|
|
|
use super::small_bitmap::SmallBitmap;
|
2023-03-14 23:37:47 +08:00
|
|
|
use super::{QueryGraph, RankingRule, RankingRuleOutput, SearchContext};
|
2023-03-07 02:21:55 +08:00
|
|
|
use crate::{Result, TermsMatchingStrategy};
|
2023-03-30 19:15:43 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2023-02-21 16:49:25 +08:00
|
|
|
|
|
|
|
pub struct Words {
|
2023-03-21 17:44:40 +08:00
|
|
|
exhausted: bool, // TODO: remove
|
2023-02-21 16:49:25 +08:00
|
|
|
query_graph: Option<QueryGraph>,
|
2023-03-21 17:44:40 +08:00
|
|
|
iterating: bool, // TODO: remove
|
2023-03-30 19:15:43 +08:00
|
|
|
nodes_to_remove: Vec<SmallBitmap<QueryNode>>,
|
2023-02-21 16:49:25 +08:00
|
|
|
terms_matching_strategy: TermsMatchingStrategy,
|
|
|
|
}
|
|
|
|
impl Words {
|
|
|
|
pub fn new(terms_matching_strategy: TermsMatchingStrategy) -> Self {
|
|
|
|
Self {
|
|
|
|
exhausted: true,
|
|
|
|
query_graph: None,
|
|
|
|
iterating: false,
|
2023-03-30 19:15:43 +08:00
|
|
|
nodes_to_remove: vec![],
|
2023-02-21 16:49:25 +08:00
|
|
|
terms_matching_strategy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:03:48 +08:00
|
|
|
impl<'ctx> RankingRule<'ctx, QueryGraph> for Words {
|
2023-02-22 22:34:37 +08:00
|
|
|
fn id(&self) -> String {
|
|
|
|
"words".to_owned()
|
|
|
|
}
|
2023-02-21 16:49:25 +08:00
|
|
|
fn start_iteration(
|
|
|
|
&mut self,
|
2023-03-30 20:54:08 +08:00
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-02-28 18:49:24 +08:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
|
|
|
_parent_candidates: &RoaringBitmap,
|
2023-02-21 16:49:25 +08:00
|
|
|
parent_query_graph: &QueryGraph,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.exhausted = false;
|
|
|
|
self.query_graph = Some(parent_query_graph.clone());
|
2023-03-30 19:15:43 +08:00
|
|
|
self.nodes_to_remove = match self.terms_matching_strategy {
|
2023-02-21 16:49:25 +08:00
|
|
|
TermsMatchingStrategy::Last => {
|
2023-03-30 20:54:08 +08:00
|
|
|
let mut ns = parent_query_graph.removal_order_for_terms_matching_strategy_last(ctx);
|
2023-03-30 20:01:52 +08:00
|
|
|
ns.reverse();
|
|
|
|
ns
|
2023-03-30 19:15:43 +08:00
|
|
|
}
|
|
|
|
TermsMatchingStrategy::All => {
|
|
|
|
vec![]
|
2023-02-21 16:49:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.iterating = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_bucket(
|
|
|
|
&mut self,
|
2023-03-13 21:03:48 +08:00
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-02-22 22:34:37 +08:00
|
|
|
logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 16:49:25 +08:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<QueryGraph>>> {
|
|
|
|
assert!(self.iterating);
|
|
|
|
assert!(universe.len() > 1);
|
2023-02-23 20:13:19 +08:00
|
|
|
|
2023-02-21 16:49:25 +08:00
|
|
|
if self.exhausted {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let Some(query_graph) = &mut self.query_graph else { panic!() };
|
2023-04-11 17:56:31 +08:00
|
|
|
logger.log_internal_state(query_graph);
|
2023-02-21 20:57:34 +08:00
|
|
|
|
2023-03-30 17:10:38 +08:00
|
|
|
let this_bucket = compute_query_graph_docids(ctx, query_graph, universe)?;
|
2023-02-21 20:57:34 +08:00
|
|
|
|
2023-02-21 16:49:25 +08:00
|
|
|
let child_query_graph = query_graph.clone();
|
2023-03-30 19:15:43 +08:00
|
|
|
|
|
|
|
if self.nodes_to_remove.is_empty() {
|
|
|
|
self.exhausted = true;
|
|
|
|
} else {
|
|
|
|
let nodes_to_remove = self.nodes_to_remove.pop().unwrap();
|
2023-03-30 20:49:25 +08:00
|
|
|
query_graph.remove_nodes_keep_edges(&nodes_to_remove.iter().collect::<Vec<_>>());
|
2023-02-21 16:49:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(RankingRuleOutput { query: child_query_graph, candidates: this_bucket }))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_iteration(
|
|
|
|
&mut self,
|
2023-03-13 21:03:48 +08:00
|
|
|
_ctx: &mut SearchContext<'ctx>,
|
2023-02-22 22:34:37 +08:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 16:49:25 +08:00
|
|
|
) {
|
|
|
|
self.iterating = false;
|
|
|
|
self.exhausted = true;
|
2023-03-30 19:15:43 +08:00
|
|
|
self.nodes_to_remove = vec![];
|
2023-03-06 15:35:01 +08:00
|
|
|
self.query_graph = None;
|
2023-02-21 16:49:25 +08:00
|
|
|
}
|
|
|
|
}
|