2023-03-06 19:21:55 +01:00
|
|
|
use roaring::RoaringBitmap;
|
2023-03-08 09:55:53 +01:00
|
|
|
|
|
|
|
use super::logger::SearchLogger;
|
|
|
|
use super::{QueryGraph, SearchContext};
|
2023-06-15 17:36:20 +02:00
|
|
|
use crate::score_details::ScoreDetails;
|
2023-03-09 11:12:31 +01:00
|
|
|
use crate::Result;
|
2023-02-21 09:44:03 +01:00
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
/// An internal trait implemented by only [`PlaceholderQuery`] and [`QueryGraph`]
|
2023-02-21 09:44:03 +01:00
|
|
|
pub trait RankingRuleQueryTrait: Sized + Clone + 'static {}
|
2023-02-21 12:33:32 +01:00
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
/// A type describing a placeholder search
|
2023-02-21 09:44:03 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlaceholderQuery;
|
|
|
|
impl RankingRuleQueryTrait for PlaceholderQuery {}
|
|
|
|
impl RankingRuleQueryTrait for QueryGraph {}
|
|
|
|
|
2023-03-28 12:39:42 +02:00
|
|
|
pub type BoxRankingRule<'ctx, Query> = Box<dyn RankingRule<'ctx, Query> + 'ctx>;
|
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
/// A trait that must be implemented by all ranking rules.
|
|
|
|
///
|
2023-03-13 14:03:48 +01:00
|
|
|
/// It is generic over `'ctx`, the lifetime of the search context
|
2023-03-09 11:12:31 +01:00
|
|
|
/// (i.e. the read transaction and the cache) and over `Query`, which
|
|
|
|
/// can be either [`PlaceholderQuery`] or [`QueryGraph`].
|
2023-03-13 14:03:48 +01:00
|
|
|
pub trait RankingRule<'ctx, Query: RankingRuleQueryTrait> {
|
2023-02-22 15:34:37 +01:00
|
|
|
fn id(&self) -> String;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Prepare the ranking rule such that it can start iterating over its
|
|
|
|
/// buckets using [`next_bucket`](RankingRule::next_bucket).
|
|
|
|
///
|
|
|
|
/// The given universe is the universe that will be given to [`next_bucket`](RankingRule::next_bucket).
|
2023-02-21 09:44:03 +01:00
|
|
|
fn start_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<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
query: &Query,
|
|
|
|
) -> Result<()>;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Return the next bucket of this ranking rule.
|
|
|
|
///
|
|
|
|
/// The returned candidates MUST be a subset of the given universe.
|
|
|
|
///
|
|
|
|
/// The universe given as argument is either:
|
|
|
|
/// - a subset of the universe given to the previous call to [`next_bucket`](RankingRule::next_bucket); OR
|
|
|
|
/// - the universe given to [`start_iteration`](RankingRule::start_iteration)
|
2023-02-21 09:44:03 +01:00
|
|
|
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<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<Query>>>;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Finish iterating over the buckets, which yields control to the parent ranking rule
|
|
|
|
/// The next call to this ranking rule, if any, will be [`start_iteration`](RankingRule::start_iteration).
|
2023-02-21 09:44:03 +01:00
|
|
|
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<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
/// Output of a ranking rule, consisting of the query to be used
|
|
|
|
/// by the child ranking rule and a set of document ids.
|
2023-02-21 09:44:03 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RankingRuleOutput<Q> {
|
2023-02-21 12:33:32 +01:00
|
|
|
/// The query corresponding to the current bucket for the child ranking rule
|
2023-02-21 09:44:03 +01:00
|
|
|
pub query: Q,
|
|
|
|
/// The allowed candidates for the child ranking rule
|
|
|
|
pub candidates: RoaringBitmap,
|
2023-06-15 17:36:20 +02:00
|
|
|
/// The score for the candidates of the current bucket
|
|
|
|
pub score: ScoreDetails,
|
2023-02-21 09:44:03 +01:00
|
|
|
}
|