From addb21f110697c1efc3082db449e21d294d73cf3 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Thu, 13 Apr 2023 18:16:08 +0200 Subject: [PATCH] Restrict the number of facet search results to 1000 --- milli/src/search/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index 1e648d241..184ba409e 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -21,6 +21,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; + pub mod facet; mod fst_utils; pub mod new; @@ -291,6 +294,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 }; @@ -301,6 +305,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; } } @@ -309,6 +317,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 }; @@ -319,6 +328,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; } }