diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index d2fa9a389..9ed19a3cb 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -33,6 +33,9 @@ static LEVDIST0: Lazy = Lazy::new(|| LevBuilder::new(0, true)); static LEVDIST1: Lazy = Lazy::new(|| LevBuilder::new(1, true)); static LEVDIST2: Lazy = Lazy::new(|| LevBuilder::new(2, true)); +/// The maximum number of facets returned by the facet search route. +const MAX_NUMBER_OF_FACETS: usize = 1000; + mod criteria; mod distinct; pub mod facet; @@ -501,6 +504,7 @@ impl<'a> SearchForFacetValue<'a> { let mut stream = fst.search(automaton).into_stream(); let mut result = 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, level: 0, left_bound: value }; @@ -511,6 +515,10 @@ impl<'a> SearchForFacetValue<'a> { let count = search_candidates.intersection_len(&docids); if count != 0 { result.push(FacetSearchResult { value: value.to_string(), count }); + length += 1; + } + if length >= MAX_NUMBER_OF_FACETS { + break; } } @@ -519,6 +527,7 @@ impl<'a> SearchForFacetValue<'a> { None => { let mut stream = fst.stream(); let mut result = 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, level: 0, left_bound: value }; @@ -529,6 +538,10 @@ impl<'a> SearchForFacetValue<'a> { let count = search_candidates.intersection_len(&docids); if count != 0 { result.push(FacetSearchResult { value: value.to_string(), count }); + length += 1; + } + if length >= MAX_NUMBER_OF_FACETS { + break; } }