Restrict the number of facet search results to 1000

This commit is contained in:
Kerollmops
2023-04-13 18:16:08 +02:00
parent cde36923c5
commit 077797b50a

View File

@@ -33,6 +33,9 @@ static LEVDIST0: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(0, true));
static LEVDIST1: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(1, true));
static LEVDIST2: Lazy<LevBuilder> = 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;
}
}