2023-03-07 02:21:55 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2023-03-08 16:55:53 +08:00
|
|
|
|
|
|
|
use super::logger::SearchLogger;
|
|
|
|
use super::{QueryGraph, SearchContext};
|
2023-03-03 04:27:42 +08:00
|
|
|
// use crate::search::new::sort::Sort;
|
2023-03-09 22:20:29 +08:00
|
|
|
use crate::search::new::distinct::{apply_distinct_rule, DistinctOutput};
|
2023-03-09 18:12:31 +08:00
|
|
|
use crate::Result;
|
2023-02-21 16:44:03 +08:00
|
|
|
|
2023-03-09 18:12:31 +08:00
|
|
|
/// An internal trait implemented by only [`PlaceholderQuery`] and [`QueryGraph`]
|
2023-02-21 16:44:03 +08:00
|
|
|
pub trait RankingRuleQueryTrait: Sized + Clone + 'static {}
|
2023-02-21 19:33:32 +08:00
|
|
|
|
2023-03-09 18:12:31 +08:00
|
|
|
/// A type describing a placeholder search
|
2023-02-21 16:44:03 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlaceholderQuery;
|
|
|
|
impl RankingRuleQueryTrait for PlaceholderQuery {}
|
|
|
|
impl RankingRuleQueryTrait for QueryGraph {}
|
|
|
|
|
2023-03-09 18:12:31 +08:00
|
|
|
/// A trait that must be implemented by all ranking rules.
|
|
|
|
///
|
2023-03-13 21:03:48 +08:00
|
|
|
/// It is generic over `'ctx`, the lifetime of the search context
|
2023-03-09 18:12:31 +08:00
|
|
|
/// (i.e. the read transaction and the cache) and over `Query`, which
|
|
|
|
/// can be either [`PlaceholderQuery`] or [`QueryGraph`].
|
2023-03-13 21:03:48 +08:00
|
|
|
pub trait RankingRule<'ctx, Query: RankingRuleQueryTrait> {
|
2023-02-22 22:34:37 +08:00
|
|
|
fn id(&self) -> String;
|
|
|
|
|
2023-02-21 19:33:32 +08: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 16:44:03 +08:00
|
|
|
fn start_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<Query>,
|
2023-02-21 16:44:03 +08:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
query: &Query,
|
|
|
|
) -> Result<()>;
|
|
|
|
|
2023-02-21 19:33:32 +08: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 16:44:03 +08:00
|
|
|
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<Query>,
|
2023-02-21 16:44:03 +08:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<Query>>>;
|
|
|
|
|
2023-02-21 19:33:32 +08: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 16:44:03 +08:00
|
|
|
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<Query>,
|
2023-02-21 16:44:03 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:12:31 +08: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 16:44:03 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RankingRuleOutput<Q> {
|
2023-02-21 19:33:32 +08:00
|
|
|
/// The query corresponding to the current bucket for the child ranking rule
|
2023-02-21 16:44:03 +08:00
|
|
|
pub query: Q,
|
|
|
|
/// The allowed candidates for the child ranking rule
|
|
|
|
pub candidates: RoaringBitmap,
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:03:48 +08:00
|
|
|
pub fn bucket_sort<'ctx, Q: RankingRuleQueryTrait>(
|
|
|
|
ctx: &mut SearchContext<'ctx>,
|
|
|
|
mut ranking_rules: Vec<Box<dyn RankingRule<'ctx, Q>>>,
|
2023-03-13 16:52:17 +08:00
|
|
|
query: &Q,
|
2023-03-06 15:35:01 +08:00
|
|
|
universe: &RoaringBitmap,
|
2023-02-27 23:14:53 +08:00
|
|
|
from: usize,
|
|
|
|
length: usize,
|
2023-03-09 18:12:31 +08:00
|
|
|
logger: &mut dyn SearchLogger<Q>,
|
2023-02-21 16:44:03 +08:00
|
|
|
) -> Result<Vec<u32>> {
|
2023-03-13 21:03:48 +08:00
|
|
|
logger.initial_query(query);
|
2023-02-22 22:34:37 +08:00
|
|
|
logger.ranking_rules(&ranking_rules);
|
2023-03-13 16:52:17 +08:00
|
|
|
logger.initial_universe(universe);
|
2023-02-22 22:34:37 +08:00
|
|
|
|
2023-03-09 22:20:29 +08:00
|
|
|
let distinct_fid = if let Some(field) = ctx.index.distinct_field(ctx.txn)? {
|
|
|
|
ctx.index.fields_ids_map(ctx.txn)?.id(field)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-02-27 23:45:07 +08:00
|
|
|
if universe.len() < from as u64 {
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:44:03 +08:00
|
|
|
let ranking_rules_len = ranking_rules.len();
|
2023-03-13 16:52:17 +08:00
|
|
|
logger.start_iteration_ranking_rule(0, ranking_rules[0].as_ref(), query, universe);
|
|
|
|
ranking_rules[0].start_iteration(ctx, logger, universe, query)?;
|
2023-02-21 16:44:03 +08:00
|
|
|
|
2023-03-09 22:20:29 +08:00
|
|
|
let mut ranking_rule_universes: Vec<RoaringBitmap> =
|
|
|
|
vec![RoaringBitmap::default(); ranking_rules_len];
|
|
|
|
ranking_rule_universes[0] = universe.clone();
|
2023-02-21 16:44:03 +08:00
|
|
|
|
|
|
|
let mut cur_ranking_rule_index = 0;
|
2023-02-27 22:04:40 +08:00
|
|
|
|
2023-03-09 18:12:31 +08:00
|
|
|
/// Finish iterating over the current ranking rule, yielding
|
|
|
|
/// control to the parent (or finishing the search if not possible).
|
|
|
|
/// Update the candidates accordingly and inform the logger.
|
2023-02-21 16:44:03 +08:00
|
|
|
macro_rules! back {
|
|
|
|
() => {
|
2023-03-09 22:20:29 +08:00
|
|
|
assert!(ranking_rule_universes[cur_ranking_rule_index].is_empty());
|
2023-02-22 22:34:37 +08:00
|
|
|
logger.end_iteration_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-03-13 16:52:17 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].as_ref(),
|
2023-03-09 22:20:29 +08:00
|
|
|
&ranking_rule_universes[cur_ranking_rule_index],
|
2023-02-22 22:34:37 +08:00
|
|
|
);
|
2023-03-09 22:20:29 +08:00
|
|
|
ranking_rule_universes[cur_ranking_rule_index].clear();
|
2023-03-07 02:21:55 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].end_iteration(ctx, logger);
|
2023-02-21 16:44:03 +08:00
|
|
|
if cur_ranking_rule_index == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
cur_ranking_rule_index -= 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut results = vec![];
|
2023-02-27 23:14:53 +08:00
|
|
|
let mut cur_offset = 0usize;
|
|
|
|
|
2023-03-09 22:20:29 +08:00
|
|
|
/// Add the candidates to the results. Take `distinct`, `from`, `limit`, and `cur_offset`
|
2023-03-09 18:12:31 +08:00
|
|
|
/// into account and inform the logger.
|
2023-02-27 23:45:07 +08:00
|
|
|
macro_rules! maybe_add_to_results {
|
2023-02-27 22:04:40 +08:00
|
|
|
($candidates:expr) => {
|
2023-03-09 22:20:29 +08:00
|
|
|
// First apply the distinct rule on the candidates, reducing the universes if necessary
|
|
|
|
let candidates = if let Some(distinct_fid) = distinct_fid {
|
|
|
|
let DistinctOutput { remaining, excluded } = apply_distinct_rule(ctx, distinct_fid, $candidates)?;
|
|
|
|
for universe in ranking_rule_universes.iter_mut() {
|
|
|
|
*universe -= &excluded;
|
|
|
|
}
|
|
|
|
remaining
|
|
|
|
} else {
|
|
|
|
$candidates.clone()
|
|
|
|
};
|
2023-02-27 23:14:53 +08:00
|
|
|
let len = candidates.len();
|
2023-02-27 23:45:07 +08:00
|
|
|
// if the candidates are empty, there is nothing to do;
|
2023-02-27 23:14:53 +08:00
|
|
|
if !candidates.is_empty() {
|
2023-03-09 22:20:29 +08:00
|
|
|
// if we still haven't reached the first document to return
|
2023-02-27 23:14:53 +08:00
|
|
|
if cur_offset < from {
|
2023-03-09 22:20:29 +08:00
|
|
|
// and if no document from this bucket can be returned
|
2023-02-27 23:14:53 +08:00
|
|
|
if cur_offset + (candidates.len() as usize) < from {
|
2023-03-09 22:20:29 +08:00
|
|
|
// then just skip the bucket
|
2023-02-27 23:14:53 +08:00
|
|
|
logger.skip_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-03-13 16:52:17 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].as_ref(),
|
2023-02-27 23:14:53 +08:00
|
|
|
&candidates,
|
|
|
|
);
|
|
|
|
} else {
|
2023-03-09 22:20:29 +08:00
|
|
|
// otherwise, skip some of the documents and add some of the rest, in order of ids
|
2023-02-27 23:14:53 +08:00
|
|
|
let all_candidates = candidates.iter().collect::<Vec<_>>();
|
|
|
|
let (skipped_candidates, candidates) =
|
|
|
|
all_candidates.split_at(from - cur_offset);
|
|
|
|
logger.skip_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-03-13 16:52:17 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].as_ref(),
|
2023-02-27 23:14:53 +08:00
|
|
|
&skipped_candidates.into_iter().collect(),
|
|
|
|
);
|
|
|
|
let candidates = candidates
|
|
|
|
.iter()
|
|
|
|
.take(length - results.len())
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
logger.add_to_results(&candidates);
|
|
|
|
results.extend(&candidates);
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-09 22:20:29 +08:00
|
|
|
// if we have passed the offset already, add some of the documents (up to the limit)
|
2023-02-27 23:14:53 +08:00
|
|
|
let candidates =
|
2023-03-08 20:26:29 +08:00
|
|
|
candidates.iter().take(length - results.len()).collect::<Vec<u32>>();
|
2023-02-27 23:14:53 +08:00
|
|
|
logger.add_to_results(&candidates);
|
|
|
|
results.extend(&candidates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur_offset += len as usize;
|
2023-02-27 22:04:40 +08:00
|
|
|
};
|
|
|
|
}
|
2023-02-27 23:14:53 +08:00
|
|
|
while results.len() < length {
|
2023-02-21 16:44:03 +08:00
|
|
|
// The universe for this bucket is zero or one element, so we don't need to sort
|
|
|
|
// anything, just extend the results and go back to the parent ranking rule.
|
2023-03-09 22:20:29 +08:00
|
|
|
if ranking_rule_universes[cur_ranking_rule_index].len() <= 1 {
|
|
|
|
maybe_add_to_results!(&ranking_rule_universes[cur_ranking_rule_index]);
|
2023-03-14 23:37:47 +08:00
|
|
|
ranking_rule_universes[cur_ranking_rule_index].clear();
|
2023-02-21 16:44:03 +08:00
|
|
|
back!();
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-22 22:34:37 +08:00
|
|
|
|
2023-03-09 22:20:29 +08:00
|
|
|
let Some(next_bucket) = ranking_rules[cur_ranking_rule_index].next_bucket(ctx, logger, &ranking_rule_universes[cur_ranking_rule_index])? else {
|
2023-02-21 16:44:03 +08:00
|
|
|
back!();
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2023-03-03 04:27:42 +08:00
|
|
|
logger.next_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-03-13 16:52:17 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].as_ref(),
|
2023-03-09 22:20:29 +08:00
|
|
|
&ranking_rule_universes[cur_ranking_rule_index],
|
2023-03-03 04:27:42 +08:00
|
|
|
&next_bucket.candidates,
|
|
|
|
);
|
|
|
|
|
2023-03-09 22:20:29 +08:00
|
|
|
assert!(ranking_rule_universes[cur_ranking_rule_index].is_superset(&next_bucket.candidates));
|
|
|
|
ranking_rule_universes[cur_ranking_rule_index] -= &next_bucket.candidates;
|
2023-02-21 16:44:03 +08:00
|
|
|
|
2023-02-27 23:45:07 +08:00
|
|
|
if cur_ranking_rule_index == ranking_rules_len - 1
|
|
|
|
|| next_bucket.candidates.len() <= 1
|
|
|
|
|| cur_offset + (next_bucket.candidates.len() as usize) < from
|
|
|
|
{
|
|
|
|
maybe_add_to_results!(&next_bucket.candidates);
|
2023-02-21 16:44:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-27 23:45:07 +08:00
|
|
|
|
|
|
|
cur_ranking_rule_index += 1;
|
2023-03-09 22:20:29 +08:00
|
|
|
ranking_rule_universes[cur_ranking_rule_index] = next_bucket.candidates.clone();
|
2023-02-27 23:45:07 +08:00
|
|
|
logger.start_iteration_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-03-13 16:52:17 +08:00
|
|
|
ranking_rules[cur_ranking_rule_index].as_ref(),
|
2023-02-27 23:45:07 +08:00
|
|
|
&next_bucket.query,
|
2023-03-09 22:20:29 +08:00
|
|
|
&ranking_rule_universes[cur_ranking_rule_index],
|
2023-02-27 23:45:07 +08:00
|
|
|
);
|
|
|
|
ranking_rules[cur_ranking_rule_index].start_iteration(
|
2023-03-07 02:21:55 +08:00
|
|
|
ctx,
|
2023-02-27 23:45:07 +08:00
|
|
|
logger,
|
|
|
|
&next_bucket.candidates,
|
|
|
|
&next_bucket.query,
|
|
|
|
)?;
|
2023-02-21 16:44:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(results)
|
|
|
|
}
|