diff --git a/milli/src/search/new/db_cache.rs b/milli/src/search/new/db_cache.rs index 4985f55e9..4fa0765e0 100644 --- a/milli/src/search/new/db_cache.rs +++ b/milli/src/search/new/db_cache.rs @@ -159,36 +159,58 @@ impl<'ctx> SearchContext<'ctx> { /// Retrieve or insert the given value in the `word_docids` database. fn get_db_word_docids(&mut self, word: Interned) -> Result> { - let interned = self.word_interner.get(word).as_str(); - let keys: Vec<_> = - self.searchable_fids.tolerant.iter().map(|(fid, _weight)| (interned, *fid)).collect(); + match &self.restricted_fids { + Some(restricted_fids) => { + let interned = self.word_interner.get(word).as_str(); + let keys: Vec<_> = + restricted_fids.tolerant.iter().map(|(fid, _)| (interned, *fid)).collect(); - DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( - self.txn, - word, - &keys[..], - &mut self.db_cache.word_docids, - self.index.word_fid_docids.remap_data_type::(), - merge_cbo_roaring_bitmaps, - ) + DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( + self.txn, + word, + &keys[..], + &mut self.db_cache.word_docids, + self.index.word_fid_docids.remap_data_type::(), + merge_cbo_roaring_bitmaps, + ) + } + None => DatabaseCache::get_value::<_, _, CboRoaringBitmapCodec>( + self.txn, + word, + self.word_interner.get(word).as_str(), + &mut self.db_cache.word_docids, + self.index.word_docids.remap_data_type::(), + ), + } } fn get_db_exact_word_docids( &mut self, word: Interned, ) -> Result> { - let interned = self.word_interner.get(word).as_str(); - let keys: Vec<_> = - self.searchable_fids.exact.iter().map(|(fid, _weight)| (interned, *fid)).collect(); + match &self.restricted_fids { + Some(restricted_fids) => { + let interned = self.word_interner.get(word).as_str(); + let keys: Vec<_> = + restricted_fids.exact.iter().map(|(fid, _)| (interned, *fid)).collect(); - DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( - self.txn, - word, - &keys[..], - &mut self.db_cache.exact_word_docids, - self.index.word_fid_docids.remap_data_type::(), - merge_cbo_roaring_bitmaps, - ) + DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( + self.txn, + word, + &keys[..], + &mut self.db_cache.exact_word_docids, + self.index.word_fid_docids.remap_data_type::(), + merge_cbo_roaring_bitmaps, + ) + } + None => DatabaseCache::get_value::<_, _, CboRoaringBitmapCodec>( + self.txn, + word, + self.word_interner.get(word).as_str(), + &mut self.db_cache.exact_word_docids, + self.index.exact_word_docids.remap_data_type::(), + ), + } } pub fn word_prefix_docids(&mut self, prefix: Word) -> Result> { @@ -216,36 +238,58 @@ impl<'ctx> SearchContext<'ctx> { &mut self, prefix: Interned, ) -> Result> { - let interned = self.word_interner.get(prefix).as_str(); - let keys: Vec<_> = - self.searchable_fids.tolerant.iter().map(|(fid, _weight)| (interned, *fid)).collect(); + match &self.restricted_fids { + Some(restricted_fids) => { + let interned = self.word_interner.get(prefix).as_str(); + let keys: Vec<_> = + restricted_fids.tolerant.iter().map(|(fid, _)| (interned, *fid)).collect(); - DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( - self.txn, - prefix, - &keys[..], - &mut self.db_cache.word_prefix_docids, - self.index.word_prefix_fid_docids.remap_data_type::(), - merge_cbo_roaring_bitmaps, - ) + DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( + self.txn, + prefix, + &keys[..], + &mut self.db_cache.word_prefix_docids, + self.index.word_prefix_fid_docids.remap_data_type::(), + merge_cbo_roaring_bitmaps, + ) + } + None => DatabaseCache::get_value::<_, _, CboRoaringBitmapCodec>( + self.txn, + prefix, + self.word_interner.get(prefix).as_str(), + &mut self.db_cache.word_prefix_docids, + self.index.word_prefix_docids.remap_data_type::(), + ), + } } fn get_db_exact_word_prefix_docids( &mut self, prefix: Interned, ) -> Result> { - let interned = self.word_interner.get(prefix).as_str(); - let keys: Vec<_> = - self.searchable_fids.exact.iter().map(|(fid, _weight)| (interned, *fid)).collect(); + match &self.restricted_fids { + Some(restricted_fids) => { + let interned = self.word_interner.get(prefix).as_str(); + let keys: Vec<_> = + restricted_fids.exact.iter().map(|(fid, _)| (interned, *fid)).collect(); - DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( - self.txn, - prefix, - &keys[..], - &mut self.db_cache.exact_word_prefix_docids, - self.index.word_prefix_fid_docids.remap_data_type::(), - merge_cbo_roaring_bitmaps, - ) + DatabaseCache::get_value_from_keys::<_, _, CboRoaringBitmapCodec>( + self.txn, + prefix, + &keys[..], + &mut self.db_cache.exact_word_prefix_docids, + self.index.word_prefix_fid_docids.remap_data_type::(), + merge_cbo_roaring_bitmaps, + ) + } + None => DatabaseCache::get_value::<_, _, CboRoaringBitmapCodec>( + self.txn, + prefix, + self.word_interner.get(prefix).as_str(), + &mut self.db_cache.exact_word_prefix_docids, + self.index.exact_word_prefix_docids.remap_data_type::(), + ), + } } pub fn get_db_word_pair_proximity_docids( @@ -421,8 +465,8 @@ impl<'ctx> SearchContext<'ctx> { word: Interned, fid: u16, ) -> Result> { - // if the requested fid isn't in the list of searchable, return None. - if !self.searchable_fids.contains(&fid) { + // if the requested fid isn't in the restricted list, return None. + if self.restricted_fids.as_ref().map_or(false, |fids| !fids.contains(&fid)) { return Ok(None); } @@ -440,8 +484,8 @@ impl<'ctx> SearchContext<'ctx> { word_prefix: Interned, fid: u16, ) -> Result> { - // if the requested fid isn't in the searchable list, return None. - if !self.searchable_fids.contains(&fid) { + // if the requested fid isn't in the restricted list, return None. + if self.restricted_fids.as_ref().map_or(false, |fids| !fids.contains(&fid)) { return Ok(None); } diff --git a/milli/src/search/new/mod.rs b/milli/src/search/new/mod.rs index b7514cbb5..2cea96fce 100644 --- a/milli/src/search/new/mod.rs +++ b/milli/src/search/new/mod.rs @@ -66,7 +66,7 @@ pub struct SearchContext<'ctx> { pub phrase_interner: DedupInterner, pub term_interner: Interner, pub phrase_docids: PhraseDocIdsCache, - pub searchable_fids: SearchableFids, + pub restricted_fids: Option, } impl<'ctx> SearchContext<'ctx> { @@ -92,7 +92,7 @@ impl<'ctx> SearchContext<'ctx> { phrase_interner: <_>::default(), term_interner: <_>::default(), phrase_docids: <_>::default(), - searchable_fids: SearchableFids { tolerant, exact }, + restricted_fids: None, }) } @@ -103,7 +103,7 @@ impl<'ctx> SearchContext<'ctx> { let mut wildcard = false; - let mut restricted_fids = SearchableFids::default(); + let mut restricted_fids = RestrictedFids::default(); for field_name in attributes_to_search_on { if field_name == "*" { wildcard = true; @@ -141,14 +141,9 @@ impl<'ctx> SearchContext<'ctx> { } if wildcard { - let (exact, tolerant) = searchable_names - .iter() - .map(|(_name, fid, weight)| (*fid, *weight)) - .partition(|(fid, _weight)| exact_attributes_ids.contains(fid)); - - self.searchable_fids = SearchableFids { tolerant, exact }; + self.restricted_fids = None; } else { - self.searchable_fids = restricted_fids; + self.restricted_fids = Some(restricted_fids); } Ok(()) @@ -171,12 +166,12 @@ impl Word { } #[derive(Debug, Clone, Default)] -pub struct SearchableFids { +pub struct RestrictedFids { pub tolerant: Vec<(FieldId, Weight)>, pub exact: Vec<(FieldId, Weight)>, } -impl SearchableFids { +impl RestrictedFids { pub fn contains(&self, fid: &FieldId) -> bool { self.tolerant.iter().any(|(id, _)| id == fid) || self.exact.iter().any(|(id, _)| id == fid) }