2023-03-08 09:55:53 +01:00
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2023-02-22 15:34:37 +01:00
|
|
|
use super::logger::SearchLogger;
|
2023-03-14 16:37:47 +01:00
|
|
|
use super::query_graph::QueryNodeData;
|
2023-03-06 19:21:55 +01:00
|
|
|
use super::resolve_query_graph::resolve_query_graph;
|
2023-03-14 16:37:47 +01:00
|
|
|
use super::{QueryGraph, RankingRule, RankingRuleOutput, SearchContext};
|
2023-03-06 19:21:55 +01:00
|
|
|
use crate::{Result, TermsMatchingStrategy};
|
2023-02-21 09:49:25 +01:00
|
|
|
|
|
|
|
pub struct Words {
|
2023-03-21 10:44:40 +01:00
|
|
|
exhausted: bool, // TODO: remove
|
2023-02-21 09:49:25 +01:00
|
|
|
query_graph: Option<QueryGraph>,
|
2023-03-21 10:44:40 +01:00
|
|
|
iterating: bool, // TODO: remove
|
2023-02-21 09:49:25 +01:00
|
|
|
positions_to_remove: Vec<i8>,
|
|
|
|
terms_matching_strategy: TermsMatchingStrategy,
|
|
|
|
}
|
|
|
|
impl Words {
|
|
|
|
pub fn new(terms_matching_strategy: TermsMatchingStrategy) -> Self {
|
|
|
|
Self {
|
|
|
|
exhausted: true,
|
|
|
|
query_graph: None,
|
|
|
|
iterating: false,
|
|
|
|
positions_to_remove: vec![],
|
|
|
|
terms_matching_strategy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:03:48 +01:00
|
|
|
impl<'ctx> RankingRule<'ctx, QueryGraph> for Words {
|
2023-02-22 15:34:37 +01:00
|
|
|
fn id(&self) -> String {
|
|
|
|
"words".to_owned()
|
|
|
|
}
|
2023-02-21 09:49:25 +01:00
|
|
|
fn start_iteration(
|
|
|
|
&mut self,
|
2023-03-13 14:03:48 +01:00
|
|
|
_ctx: &mut SearchContext<'ctx>,
|
2023-02-28 11:49:24 +01:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
|
|
|
_parent_candidates: &RoaringBitmap,
|
2023-02-21 09:49:25 +01:00
|
|
|
parent_query_graph: &QueryGraph,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.exhausted = false;
|
|
|
|
self.query_graph = Some(parent_query_graph.clone());
|
|
|
|
|
|
|
|
let positions_to_remove = match self.terms_matching_strategy {
|
|
|
|
TermsMatchingStrategy::Last => {
|
|
|
|
let mut all_positions = BTreeSet::new();
|
2023-03-14 16:37:47 +01:00
|
|
|
for (_, n) in parent_query_graph.nodes.iter() {
|
|
|
|
match &n.data {
|
|
|
|
QueryNodeData::Term(term) => {
|
2023-02-21 09:49:25 +01:00
|
|
|
all_positions.extend(term.positions.clone().into_iter());
|
|
|
|
}
|
2023-03-14 16:37:47 +01:00
|
|
|
QueryNodeData::Deleted | QueryNodeData::Start | QueryNodeData::End => {}
|
2023-02-21 09:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
let mut r: Vec<i8> = all_positions.into_iter().collect();
|
|
|
|
// don't remove the first term
|
|
|
|
r.remove(0);
|
|
|
|
r
|
2023-02-21 09:49:25 +01:00
|
|
|
}
|
|
|
|
TermsMatchingStrategy::All => vec![],
|
|
|
|
};
|
|
|
|
self.positions_to_remove = positions_to_remove;
|
|
|
|
self.iterating = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_bucket(
|
|
|
|
&mut self,
|
2023-03-13 14:03:48 +01:00
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-02-22 15:34:37 +01:00
|
|
|
logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 09:49:25 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<QueryGraph>>> {
|
|
|
|
assert!(self.iterating);
|
|
|
|
assert!(universe.len() > 1);
|
2023-02-23 13:13:19 +01:00
|
|
|
|
2023-02-21 09:49:25 +01:00
|
|
|
if self.exhausted {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let Some(query_graph) = &mut self.query_graph else { panic!() };
|
2023-02-21 13:57:34 +01:00
|
|
|
|
2023-02-23 13:13:19 +01:00
|
|
|
logger.log_words_state(query_graph);
|
|
|
|
|
2023-03-06 19:21:55 +01:00
|
|
|
let this_bucket = resolve_query_graph(ctx, query_graph, universe)?;
|
2023-02-21 13:57:34 +01:00
|
|
|
|
2023-02-21 09:49:25 +01:00
|
|
|
let child_query_graph = query_graph.clone();
|
2023-03-02 21:27:57 +01:00
|
|
|
loop {
|
|
|
|
if self.positions_to_remove.is_empty() {
|
|
|
|
self.exhausted = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
let position_to_remove = self.positions_to_remove.pop().unwrap();
|
2023-03-08 13:26:29 +01:00
|
|
|
let did_delete_any_node =
|
|
|
|
query_graph.remove_words_starting_at_position(position_to_remove);
|
2023-03-02 21:27:57 +01:00
|
|
|
if did_delete_any_node {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:49:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(RankingRuleOutput { query: child_query_graph, candidates: this_bucket }))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_iteration(
|
|
|
|
&mut self,
|
2023-03-13 14:03:48 +01:00
|
|
|
_ctx: &mut SearchContext<'ctx>,
|
2023-02-22 15:34:37 +01:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 09:49:25 +01:00
|
|
|
) {
|
|
|
|
self.iterating = false;
|
|
|
|
self.exhausted = true;
|
|
|
|
self.positions_to_remove = vec![];
|
2023-03-06 08:35:01 +01:00
|
|
|
self.query_graph = None;
|
2023-02-21 09:49:25 +01:00
|
|
|
}
|
|
|
|
}
|