From 0252cfe8b6d67b931f9c228a2e8a1ab477347569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 27 Apr 2023 17:10:58 +0200 Subject: [PATCH] Simplify the placeholder search of the facet-search route --- milli/src/search/mod.rs | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index 97efb94a1..c872e3ee4 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -298,7 +298,7 @@ impl<'a> SearchForFacetValues<'a> { !self.search_query.index.exact_attributes_ids(rtxn)?.contains(&fid); if authorize_typos && field_authorizes_typos { - let mut result = vec![]; + let mut results = vec![]; let exact_words_fst = self.search_query.index.exact_words(rtxn)?; if exact_words_fst.map_or(false, |fst| fst.contains(query)) { @@ -309,7 +309,7 @@ impl<'a> SearchForFacetValues<'a> { { let count = search_candidates.intersection_len(&bitmap); if count != 0 { - result.push(FacetValueHit { value: query.to_string(), count }); + results.push(FacetValueHit { value: query.to_string(), count }); } } } else { @@ -337,7 +337,7 @@ impl<'a> SearchForFacetValues<'a> { }; let count = search_candidates.intersection_len(&docids); if count != 0 { - result.push(FacetValueHit { value: value.to_string(), count }); + results.push(FacetValueHit { value: value.to_string(), count }); length += 1; } if length >= MAX_NUMBER_OF_FACETS { @@ -346,11 +346,11 @@ impl<'a> SearchForFacetValues<'a> { } } - Ok(result) + Ok(results) } else { let automaton = StartsWith(Str::new(query)); let mut stream = fst.search(automaton).into_stream(); - let mut result = vec![]; + let mut results = vec![]; let mut length = 0; while let Some(facet_value) = stream.next() { let value = std::str::from_utf8(facet_value)?; @@ -366,7 +366,7 @@ impl<'a> SearchForFacetValues<'a> { }; let count = search_candidates.intersection_len(&docids); if count != 0 { - result.push(FacetValueHit { value: value.to_string(), count }); + results.push(FacetValueHit { value: value.to_string(), count }); length += 1; } if length >= MAX_NUMBER_OF_FACETS { @@ -374,34 +374,26 @@ impl<'a> SearchForFacetValues<'a> { } } - Ok(result) + Ok(results) } } None => { - let mut stream = fst.stream(); - let mut result = vec![]; + let mut results = vec![]; let mut length = 0; - while let Some(facet_value) = stream.next() { - let value = std::str::from_utf8(facet_value)?; - let key = FacetGroupKey { field_id: fid, level: 0, left_bound: value }; - let docids = match index.facet_id_string_docids.get(rtxn, &key)? { - Some(FacetGroupValue { bitmap, .. }) => bitmap, - None => { - error!("the facet value is missing from the facet database: {key:?}"); - continue; - } - }; - let count = search_candidates.intersection_len(&docids); + let prefix = FacetGroupKey { field_id: fid, level: 0, left_bound: "" }; + for result in index.facet_id_string_docids.prefix_iter(rtxn, &prefix)? { + let (FacetGroupKey { left_bound, .. }, FacetGroupValue { bitmap, .. }) = + result?; + let count = search_candidates.intersection_len(&bitmap); if count != 0 { - result.push(FacetValueHit { value: value.to_string(), count }); + results.push(FacetValueHit { value: left_bound.to_string(), count }); length += 1; } if length >= MAX_NUMBER_OF_FACETS { break; } } - - Ok(result) + Ok(results) } } }