2021-03-23 15:25:46 +01:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
use super::{Criterion, CriterionParameters, CriterionResult};
|
2022-07-12 17:56:50 +02:00
|
|
|
use crate::search::criteria::{resolve_query_tree, Context};
|
2021-03-23 15:25:46 +01:00
|
|
|
use crate::search::query_tree::Operation;
|
2022-07-18 16:52:45 +02:00
|
|
|
use crate::search::Distinct;
|
2021-06-16 18:33:33 +02:00
|
|
|
use crate::Result;
|
2022-10-17 14:41:57 +02:00
|
|
|
/// Initial is a mandatory criterion, it is always the first
|
|
|
|
/// and is meant to initalize the CriterionResult used by the other criteria.
|
|
|
|
/// It behave like an [Once Iterator](https://doc.rust-lang.org/std/iter/struct.Once.html) and will return Some(CriterionResult) only one time.
|
2022-07-18 16:52:45 +02:00
|
|
|
pub struct Initial<'t, D> {
|
2022-07-12 17:56:50 +02:00
|
|
|
ctx: &'t dyn Context<'t>,
|
2021-06-16 18:33:33 +02:00
|
|
|
answer: Option<CriterionResult>,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits: bool,
|
2022-07-18 16:52:45 +02:00
|
|
|
distinct: Option<D>,
|
2021-03-23 15:25:46 +01:00
|
|
|
}
|
|
|
|
|
2022-07-18 16:52:45 +02:00
|
|
|
impl<'t, D> Initial<'t, D> {
|
2021-06-16 18:33:33 +02:00
|
|
|
pub fn new(
|
2022-07-12 17:56:50 +02:00
|
|
|
ctx: &'t dyn Context<'t>,
|
2021-06-16 18:33:33 +02:00
|
|
|
query_tree: Option<Operation>,
|
|
|
|
filtered_candidates: Option<RoaringBitmap>,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits: bool,
|
2022-07-18 16:52:45 +02:00
|
|
|
distinct: Option<D>,
|
|
|
|
) -> Initial<D> {
|
2021-03-23 15:25:46 +01:00
|
|
|
let answer = CriterionResult {
|
|
|
|
query_tree,
|
2021-05-10 12:33:37 +02:00
|
|
|
candidates: None,
|
|
|
|
filtered_candidates,
|
2021-05-05 20:46:56 +02:00
|
|
|
bucket_candidates: None,
|
2021-03-23 15:25:46 +01:00
|
|
|
};
|
2022-07-18 16:52:45 +02:00
|
|
|
Initial { ctx, answer: Some(answer), exhaustive_number_hits, distinct }
|
2021-03-23 15:25:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 16:52:45 +02:00
|
|
|
impl<D: Distinct> Criterion for Initial<'_, D> {
|
2021-03-23 15:25:46 +01:00
|
|
|
#[logging_timer::time("Initial::{}")]
|
2022-07-12 17:56:50 +02:00
|
|
|
fn next(&mut self, params: &mut CriterionParameters) -> Result<Option<CriterionResult>> {
|
|
|
|
self.answer
|
|
|
|
.take()
|
|
|
|
.map(|mut answer| {
|
|
|
|
if self.exhaustive_number_hits && answer.query_tree.is_some() {
|
2022-10-17 18:23:15 +02:00
|
|
|
// resolve the whole query tree to retrieve an exhaustive list of documents matching the query.
|
2022-07-20 15:58:26 +02:00
|
|
|
let mut candidates = resolve_query_tree(
|
2022-07-12 17:56:50 +02:00
|
|
|
self.ctx,
|
|
|
|
answer.query_tree.as_ref().unwrap(),
|
|
|
|
&mut params.wdcache,
|
|
|
|
)?;
|
|
|
|
|
2022-10-17 14:41:57 +02:00
|
|
|
// Apply the filters on the documents retrieved with the query tree.
|
2022-07-20 15:58:26 +02:00
|
|
|
if let Some(ref filtered_candidates) = answer.filtered_candidates {
|
|
|
|
candidates &= filtered_candidates;
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:23:20 +02:00
|
|
|
// because the bucket_candidates should be an exhaustive count of the matching documents,
|
2022-10-17 14:41:57 +02:00
|
|
|
// we precompute the distinct attributes.
|
2022-07-18 16:52:45 +02:00
|
|
|
let bucket_candidates = match &mut self.distinct {
|
|
|
|
Some(distinct) => {
|
|
|
|
let mut bucket_candidates = RoaringBitmap::new();
|
|
|
|
for c in distinct.distinct(candidates.clone(), RoaringBitmap::new()) {
|
|
|
|
bucket_candidates.insert(c?);
|
|
|
|
}
|
|
|
|
bucket_candidates
|
|
|
|
}
|
|
|
|
None => candidates.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
answer.candidates = Some(candidates);
|
|
|
|
answer.bucket_candidates = Some(bucket_candidates);
|
2022-07-12 17:56:50 +02:00
|
|
|
}
|
|
|
|
Ok(answer)
|
|
|
|
})
|
|
|
|
.transpose()
|
2021-03-23 15:25:46 +01:00
|
|
|
}
|
|
|
|
}
|