2023-03-08 15:04:25 +01:00
|
|
|
/*! Module implementing the graph used for the graph-based ranking rules
|
|
|
|
and its related algorithms.
|
|
|
|
|
|
|
|
A ranking rule graph is built on top of the [`QueryGraph`]: the nodes stay
|
|
|
|
the same but the edges are replaced.
|
|
|
|
*/
|
|
|
|
|
2023-03-06 08:35:01 +01:00
|
|
|
mod build;
|
|
|
|
mod cheapest_paths;
|
|
|
|
mod edge_docids_cache;
|
|
|
|
mod empty_paths_cache;
|
2023-03-13 09:52:17 +01:00
|
|
|
mod path_set;
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// Implementation of the `proximity` ranking rule
|
2023-03-06 08:35:01 +01:00
|
|
|
mod proximity;
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Implementation of the `typo` ranking rule
|
2023-03-06 08:35:01 +01:00
|
|
|
mod typo;
|
|
|
|
|
2023-03-13 12:46:32 +01:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
2023-03-14 16:37:47 +01:00
|
|
|
pub use edge_docids_cache::EdgeConditionDocIdsCache;
|
|
|
|
pub use empty_paths_cache::DeadEndPathCache;
|
|
|
|
pub use proximity::{ProximityEdge, ProximityGraph};
|
2023-02-21 09:46:00 +01:00
|
|
|
use roaring::RoaringBitmap;
|
2023-03-14 16:37:47 +01:00
|
|
|
pub use typo::{TypoEdge, TypoGraph};
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-14 16:37:47 +01:00
|
|
|
use super::interner::{DedupInterner, FixedSizeInterner, Interned, MappedInterner};
|
2023-03-08 09:55:53 +01:00
|
|
|
use super::logger::SearchLogger;
|
|
|
|
use super::small_bitmap::SmallBitmap;
|
|
|
|
use super::{QueryGraph, QueryNode, SearchContext};
|
|
|
|
use crate::Result;
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// The condition that is associated with an edge in the ranking rule graph.
|
|
|
|
///
|
|
|
|
/// Some edges are unconditional, which means that traversing them does not reduce
|
|
|
|
/// the set of candidates.
|
|
|
|
///
|
|
|
|
/// Most edges, however, have a condition attached to them. For example, for the
|
|
|
|
/// proximity ranking rule, the condition could be that a word is N-close to another one.
|
|
|
|
/// When the edge is traversed, some database operations are executed to retrieve the set
|
|
|
|
/// of documents that satisfy the condition, which reduces the list of candidate document ids.
|
|
|
|
pub enum EdgeCondition<E> {
|
2023-02-21 09:46:00 +01:00
|
|
|
Unconditional,
|
2023-03-13 12:46:32 +01:00
|
|
|
Conditional(Interned<E>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E> Copy for EdgeCondition<E> {}
|
|
|
|
|
|
|
|
impl<E> Clone for EdgeCondition<E> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Unconditional => Self::Unconditional,
|
|
|
|
Self::Conditional(arg0) => Self::Conditional(*arg0),
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// An edge in the ranking rule graph.
|
|
|
|
///
|
|
|
|
/// It contains:
|
|
|
|
/// 1. The source and destination nodes
|
|
|
|
/// 2. The cost of traversing this edge
|
|
|
|
/// 3. The condition associated with it
|
2023-03-13 12:46:32 +01:00
|
|
|
#[derive(Clone)]
|
2023-02-21 09:46:00 +01:00
|
|
|
pub struct Edge<E> {
|
2023-03-14 16:37:47 +01:00
|
|
|
pub source_node: Interned<QueryNode>,
|
|
|
|
pub dest_node: Interned<QueryNode>,
|
2023-02-23 13:13:19 +01:00
|
|
|
pub cost: u8,
|
2023-03-08 15:04:25 +01:00
|
|
|
pub condition: EdgeCondition<E>,
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// A trait to be implemented by a marker type to build a graph-based ranking rule.
|
|
|
|
///
|
|
|
|
/// It mostly describes how to:
|
|
|
|
/// 1. Retrieve the set of edges (their cost and condition) between two nodes.
|
|
|
|
/// 2. Compute the document ids satisfying a condition
|
2023-02-23 13:13:19 +01:00
|
|
|
pub trait RankingRuleGraphTrait: Sized {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// The condition of an edge connecting two query nodes. The condition
|
2023-02-21 12:33:32 +01:00
|
|
|
/// should be sufficient to compute the edge's cost and associated document ids
|
2023-03-08 15:04:25 +01:00
|
|
|
/// in [`resolve_edge_condition`](RankingRuleGraphTrait::resolve_edge_condition).
|
2023-03-13 12:46:32 +01:00
|
|
|
type EdgeCondition: Sized + Clone + PartialEq + Eq + Hash;
|
2023-02-21 12:33:32 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Return the label of the given edge condition, to be used when visualising
|
|
|
|
/// the ranking rule graph.
|
|
|
|
fn label_for_edge_condition(edge: &Self::EdgeCondition) -> String;
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Compute the document ids associated with the given edge condition,
|
|
|
|
/// restricted to the given universe.
|
2023-03-13 14:03:48 +01:00
|
|
|
fn resolve_edge_condition<'ctx>(
|
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-03-08 15:04:25 +01:00
|
|
|
edge_condition: &Self::EdgeCondition,
|
2023-03-07 14:42:58 +01:00
|
|
|
universe: &RoaringBitmap,
|
2023-02-21 09:46:00 +01:00
|
|
|
) -> Result<RoaringBitmap>;
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Return the cost and condition of the edges going from the previously visited node
|
|
|
|
/// (with [`build_step_visit_source_node`](RankingRuleGraphTrait::build_step_visit_source_node)) to `dest_node`.
|
2023-03-14 10:54:55 +01:00
|
|
|
fn build_edges<'ctx>(
|
2023-03-13 14:03:48 +01:00
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-03-14 16:37:47 +01:00
|
|
|
conditions_interner: &mut DedupInterner<Self::EdgeCondition>,
|
2023-03-14 10:54:55 +01:00
|
|
|
source_node: &QueryNode,
|
2023-03-08 15:04:25 +01:00
|
|
|
dest_node: &QueryNode,
|
|
|
|
) -> Result<Vec<(u8, EdgeCondition<Self::EdgeCondition>)>>;
|
2023-02-23 13:13:19 +01:00
|
|
|
|
|
|
|
fn log_state(
|
|
|
|
graph: &RankingRuleGraph<Self>,
|
2023-03-07 14:42:58 +01:00
|
|
|
paths: &[Vec<u16>],
|
2023-03-14 16:37:47 +01:00
|
|
|
empty_paths_cache: &DeadEndPathCache<Self>,
|
2023-03-02 21:27:42 +01:00
|
|
|
universe: &RoaringBitmap,
|
2023-03-14 16:37:47 +01:00
|
|
|
distances: &MappedInterner<Vec<(u16, SmallBitmap<Self::EdgeCondition>)>, QueryNode>,
|
2023-03-07 14:42:58 +01:00
|
|
|
cost: u16,
|
2023-02-23 13:13:19 +01:00
|
|
|
logger: &mut dyn SearchLogger<QueryGraph>,
|
|
|
|
);
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// The graph used by graph-based ranking rules.
|
|
|
|
///
|
|
|
|
/// It is built on top of a [`QueryGraph`], keeping the same nodes
|
|
|
|
/// but replacing the edges.
|
2023-02-21 09:46:00 +01:00
|
|
|
pub struct RankingRuleGraph<G: RankingRuleGraphTrait> {
|
|
|
|
pub query_graph: QueryGraph,
|
2023-03-14 16:37:47 +01:00
|
|
|
pub edges_store: FixedSizeInterner<Option<Edge<G::EdgeCondition>>>,
|
|
|
|
pub edges_of_node: MappedInterner<SmallBitmap<Option<Edge<G::EdgeCondition>>>, QueryNode>,
|
|
|
|
pub conditions_interner: FixedSizeInterner<G::EdgeCondition>,
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
2023-03-13 14:03:48 +01:00
|
|
|
impl<G: RankingRuleGraphTrait> Clone for RankingRuleGraph<G> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
query_graph: self.query_graph.clone(),
|
|
|
|
edges_store: self.edges_store.clone(),
|
|
|
|
edges_of_node: self.edges_of_node.clone(),
|
|
|
|
conditions_interner: self.conditions_interner.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:46:00 +01:00
|
|
|
impl<G: RankingRuleGraphTrait> RankingRuleGraph<G> {
|
2023-03-14 16:37:47 +01:00
|
|
|
/// Remove all edges with the given condition
|
|
|
|
pub fn remove_edges_with_condition(&mut self, condition_to_remove: Interned<G::EdgeCondition>) {
|
|
|
|
for (edge_id, edge_opt) in self.edges_store.iter_mut() {
|
|
|
|
let Some(edge) = edge_opt.as_mut() else { continue };
|
|
|
|
match edge.condition {
|
|
|
|
EdgeCondition::Unconditional => continue,
|
|
|
|
EdgeCondition::Conditional(condition) => {
|
|
|
|
if condition == condition_to_remove {
|
|
|
|
let (source_node, _dest_node) = (edge.source_node, edge.dest_node);
|
|
|
|
*edge_opt = None;
|
|
|
|
self.edges_of_node.get_mut(source_node).remove(edge_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
}
|