implement distinct attribute

distinct can return error

facet distinct on numbers

return distinct error

review fixes

make get_facet_value more generic

fixes
This commit is contained in:
Marin Postma
2021-04-07 12:38:48 +02:00
parent 6e126c96a9
commit 45c45e11dd
13 changed files with 525 additions and 53 deletions

View File

@ -17,7 +17,7 @@ use crate::search::facet::FacetIter;
use crate::search::query_tree::Operation;
use crate::search::WordDerivationsCache;
use crate::{FieldsIdsMap, FieldId, Index};
use super::{Criterion, CriterionResult};
use super::{Criterion, CriterionResult, CriterionContext};
/// Threshold on the number of candidates that will make
/// the system to choose between one algorithm or another.
@ -151,7 +151,7 @@ impl<'t> AscDesc<'t> {
impl<'t> Criterion for AscDesc<'t> {
#[logging_timer::time("AscDesc::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
fn next(&mut self, context: CriterionContext) -> anyhow::Result<Option<CriterionResult>> {
loop {
debug!("Facet {}({}) iteration",
if self.ascending { "Asc" } else { "Desc" }, self.field_name
@ -163,7 +163,8 @@ impl<'t> Criterion for AscDesc<'t> {
let bucket_candidates = take(&mut self.bucket_candidates);
match self.parent.as_mut() {
Some(parent) => {
match parent.next(wdcache)? {
let CriterionContext { word_cache, exclude } = context;
match parent.next(CriterionContext { exclude, word_cache })? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_tree = query_tree;
let candidates = match (&self.query_tree, candidates) {
@ -173,7 +174,7 @@ impl<'t> Criterion for AscDesc<'t> {
},
(Some(qt), None) => {
let context = CriteriaBuilder::new(&self.rtxn, &self.index)?;
let mut candidates = resolve_query_tree(&context, qt, &mut HashMap::new(), wdcache)?;
let mut candidates = resolve_query_tree(&context, qt, &mut HashMap::new(), word_cache)?;
candidates.intersect_with(&self.faceted_candidates);
candidates
},

View File

@ -6,7 +6,7 @@ use roaring::RoaringBitmap;
use crate::search::query_tree::Operation;
use crate::search::WordDerivationsCache;
use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context};
use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context, CriterionContext};
/// The result of a call to the fetcher.
#[derive(Debug, Clone, PartialEq)]
@ -61,7 +61,7 @@ impl<'t> Fetcher<'t> {
}
#[logging_timer::time("Fetcher::{}")]
pub fn next(&mut self) -> anyhow::Result<Option<FetcherResult>> {
pub fn next(&mut self, exclude: &RoaringBitmap) -> anyhow::Result<Option<FetcherResult>> {
use Candidates::{Allowed, Forbidden};
loop {
debug!("Fetcher iteration (should_get_documents_ids: {}) ({:?})",
@ -90,7 +90,11 @@ impl<'t> Fetcher<'t> {
Forbidden(_) => {
match self.parent.as_mut() {
Some(parent) => {
match parent.next(&mut self.wdcache)? {
let context = CriterionContext {
word_cache: &mut self.wdcache,
exclude
};
match parent.next(context)? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
let candidates = match (&query_tree, candidates) {
(_, Some(candidates)) => candidates,

View File

@ -20,8 +20,13 @@ mod asc_desc;
mod proximity;
pub mod fetcher;
pub struct CriterionContext<'a, 'b> {
exclude: &'a RoaringBitmap,
word_cache: &'b mut WordDerivationsCache,
}
pub trait Criterion {
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>>;
fn next(&mut self, wdcache: CriterionContext) -> anyhow::Result<Option<CriterionResult>>;
}
/// The result of a call to the parent criterion.

View File

@ -8,7 +8,7 @@ use log::debug;
use crate::{DocumentId, Position, search::{query_tree::QueryKind}};
use crate::search::query_tree::{maximum_proximity, Operation, Query};
use crate::search::{build_dfa, WordDerivationsCache};
use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids, resolve_query_tree};
use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids, resolve_query_tree, CriterionContext};
pub struct Proximity<'t> {
ctx: &'t dyn Context,
@ -56,8 +56,9 @@ impl<'t> Proximity<'t> {
impl<'t> Criterion for Proximity<'t> {
#[logging_timer::time("Proximity::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
fn next(&mut self, context: CriterionContext) -> anyhow::Result<Option<CriterionResult>> {
use Candidates::{Allowed, Forbidden};
let CriterionContext { word_cache, exclude } = context;
loop {
debug!("Proximity at iteration {} (max {:?}) ({:?})",
self.proximity,
@ -98,7 +99,7 @@ impl<'t> Criterion for Proximity<'t> {
self.ctx,
query_tree,
candidates,
wdcache,
word_cache,
)?;
self.plane_sweep_cache = Some(cache.into_iter());
@ -110,7 +111,7 @@ impl<'t> Criterion for Proximity<'t> {
&query_tree,
self.proximity,
&mut self.candidates_cache,
wdcache,
word_cache,
)?
};
@ -140,7 +141,7 @@ impl<'t> Criterion for Proximity<'t> {
&query_tree,
self.proximity,
&mut self.candidates_cache,
wdcache,
word_cache,
)?;
new_candidates.difference_with(&candidates);
@ -170,11 +171,11 @@ impl<'t> Criterion for Proximity<'t> {
(None, Forbidden(_)) => {
match self.parent.as_mut() {
Some(parent) => {
match parent.next(wdcache)? {
match parent.next(CriterionContext { exclude, word_cache })? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
let candidates = match (&query_tree, candidates) {
(_, Some(candidates)) => candidates,
(Some(qt), None) => resolve_query_tree(self.ctx, qt, &mut HashMap::new(), wdcache)?,
(Some(qt), None) => resolve_query_tree(self.ctx, qt, &mut HashMap::new(), word_cache)?,
(None, None) => RoaringBitmap::new(),
};

View File

@ -6,7 +6,7 @@ use roaring::RoaringBitmap;
use crate::search::query_tree::{maximum_typo, Operation, Query, QueryKind};
use crate::search::{word_derivations, WordDerivationsCache};
use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids};
use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids, CriterionContext};
pub struct Typo<'t> {
ctx: &'t dyn Context,
@ -51,8 +51,9 @@ impl<'t> Typo<'t> {
impl<'t> Criterion for Typo<'t> {
#[logging_timer::time("Typo::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
fn next(&mut self, context: CriterionContext) -> anyhow::Result<Option<CriterionResult>> {
use Candidates::{Allowed, Forbidden};
let CriterionContext { word_cache, exclude } = context;
loop {
debug!("Typo at iteration {} ({:?})", self.number_typos, self.candidates);
@ -71,9 +72,9 @@ impl<'t> Criterion for Typo<'t> {
} else {
let fst = self.ctx.words_fst();
let new_query_tree = if self.number_typos < 2 {
alterate_query_tree(&fst, query_tree.clone(), self.number_typos, wdcache)?
alterate_query_tree(&fst, query_tree.clone(), self.number_typos, word_cache)?
} else if self.number_typos == 2 {
*query_tree = alterate_query_tree(&fst, query_tree.clone(), self.number_typos, wdcache)?;
*query_tree = alterate_query_tree(&fst, query_tree.clone(), self.number_typos, word_cache)?;
query_tree.clone()
} else {
query_tree.clone()
@ -84,7 +85,7 @@ impl<'t> Criterion for Typo<'t> {
&new_query_tree,
self.number_typos,
&mut self.candidates_cache,
wdcache,
word_cache,
)?;
new_candidates.intersect_with(&candidates);
candidates.difference_with(&new_candidates);
@ -109,9 +110,9 @@ impl<'t> Criterion for Typo<'t> {
} else {
let fst = self.ctx.words_fst();
let new_query_tree = if self.number_typos < 2 {
alterate_query_tree(&fst, query_tree.clone(), self.number_typos, wdcache)?
alterate_query_tree(&fst, query_tree.clone(), self.number_typos, word_cache)?
} else if self.number_typos == 2 {
*query_tree = alterate_query_tree(&fst, query_tree.clone(), self.number_typos, wdcache)?;
*query_tree = alterate_query_tree(&fst, query_tree.clone(), self.number_typos, word_cache)?;
query_tree.clone()
} else {
query_tree.clone()
@ -122,7 +123,7 @@ impl<'t> Criterion for Typo<'t> {
&new_query_tree,
self.number_typos,
&mut self.candidates_cache,
wdcache,
word_cache,
)?;
new_candidates.difference_with(&candidates);
candidates.union_with(&new_candidates);
@ -147,7 +148,7 @@ impl<'t> Criterion for Typo<'t> {
(None, Forbidden(_)) => {
match self.parent.as_mut() {
Some(parent) => {
match parent.next(wdcache)? {
match parent.next(CriterionContext { exclude, word_cache })? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_tree = query_tree.map(|op| (maximum_typo(&op), op));
self.number_typos = 0;
@ -346,8 +347,12 @@ mod test {
let mut wdcache = WordDerivationsCache::new();
let mut criteria = Typo::initial(&context, query_tree, facet_candidates);
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
assert!(criteria.next(&mut wdcache).unwrap().is_none());
assert!(criteria.next(sort_context).unwrap().is_none());
}
#[test]
@ -381,7 +386,12 @@ mod test {
bucket_candidates: candidates_1,
};
assert_eq!(criteria.next(&mut wdcache).unwrap(), Some(expected_1));
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
assert_eq!(criteria.next(sort_context).unwrap(), Some(expected_1));
let candidates_2 = (
context.word_docids("split").unwrap().unwrap()
@ -403,7 +413,12 @@ mod test {
bucket_candidates: candidates_2,
};
assert_eq!(criteria.next(&mut wdcache).unwrap(), Some(expected_2));
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
assert_eq!(criteria.next(sort_context).unwrap(), Some(expected_2));
}
#[test]
@ -421,11 +436,19 @@ mod test {
bucket_candidates: facet_candidates,
};
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
// first iteration, returns the facet candidates
assert_eq!(criteria.next(&mut wdcache).unwrap(), Some(expected));
assert_eq!(criteria.next(sort_context).unwrap(), Some(expected));
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
// second iteration, returns None because there is no more things to do
assert!(criteria.next(&mut wdcache).unwrap().is_none());
assert!(criteria.next(sort_context ).unwrap().is_none());
}
#[test]
@ -459,7 +482,12 @@ mod test {
bucket_candidates: candidates_1 & &facet_candidates,
};
assert_eq!(criteria.next(&mut wdcache).unwrap(), Some(expected_1));
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
assert_eq!(criteria.next(sort_context).unwrap(), Some(expected_1));
let candidates_2 = (
context.word_docids("split").unwrap().unwrap()
@ -481,7 +509,12 @@ mod test {
bucket_candidates: candidates_2 & &facet_candidates,
};
assert_eq!(criteria.next(&mut wdcache).unwrap(), Some(expected_2));
let sort_context = CriterionContext {
word_cache: &mut wdcache,
exclude: &RoaringBitmap::new(),
};
assert_eq!(criteria.next(sort_context).unwrap(), Some(expected_2));
}
}

View File

@ -5,8 +5,7 @@ use log::debug;
use roaring::RoaringBitmap;
use crate::search::query_tree::Operation;
use crate::search::WordDerivationsCache;
use super::{resolve_query_tree, Criterion, CriterionResult, Context};
use super::{resolve_query_tree, Criterion, CriterionResult, Context, CriterionContext};
pub struct Words<'t> {
ctx: &'t dyn Context,
@ -48,7 +47,8 @@ impl<'t> Words<'t> {
impl<'t> Criterion for Words<'t> {
#[logging_timer::time("Words::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
fn next(&mut self, context: CriterionContext) -> anyhow::Result<Option<CriterionResult>> {
let CriterionContext { word_cache, exclude } = context;
loop {
debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates);
@ -62,7 +62,7 @@ impl<'t> Criterion for Words<'t> {
}));
},
(Some(qt), Some(candidates)) => {
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?;
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, word_cache)?;
found_candidates.intersect_with(&candidates);
candidates.difference_with(&found_candidates);
@ -100,7 +100,7 @@ impl<'t> Criterion for Words<'t> {
(None, None) => {
match self.parent.as_mut() {
Some(parent) => {
match parent.next(wdcache)? {
match parent.next(CriterionContext { word_cache, exclude })? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
self.candidates = candidates;