From 3f3462ab62abbf5d8af7621755ab2b7e87f1ea8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 10 Jan 2024 16:34:40 +0100 Subject: [PATCH] Limit the number of values returned by the facet search --- meilisearch/src/search.rs | 3 +++ meilisearch/tests/search/facet_search.rs | 18 ++++++++++++++++++ milli/src/search/mod.rs | 22 +++++++++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/meilisearch/src/search.rs b/meilisearch/src/search.rs index b5dba8a58..399c7b95d 100644 --- a/meilisearch/src/search.rs +++ b/meilisearch/src/search.rs @@ -735,6 +735,9 @@ pub fn perform_facet_search( if let Some(facet_query) = &facet_query { facet_search.query(facet_query); } + if let Some(max_facets) = index.max_values_per_facet(&rtxn)? { + facet_search.max_values(max_facets as usize); + } Ok(FacetSearchResult { facet_hits: facet_search.execute()?, diff --git a/meilisearch/tests/search/facet_search.rs b/meilisearch/tests/search/facet_search.rs index 8c1229f1a..5f9f631f9 100644 --- a/meilisearch/tests/search/facet_search.rs +++ b/meilisearch/tests/search/facet_search.rs @@ -105,6 +105,24 @@ async fn more_advanced_facet_search() { snapshot!(response["facetHits"].as_array().unwrap().len(), @"1"); } +#[actix_rt::test] +async fn simple_facet_search_with_max_values() { + let server = Server::new().await; + let index = server.index("test"); + + let documents = DOCUMENTS.clone(); + index.update_settings_faceting(json!({ "maxValuesPerFacet": 1 })).await; + index.update_settings_filterable_attributes(json!(["genres"])).await; + index.add_documents(documents, None).await; + index.wait_task(2).await; + + let (response, code) = + index.facet_search(json!({"facetName": "genres", "facetQuery": "a"})).await; + + assert_eq!(code, 200, "{}", response); + assert_eq!(dbg!(response)["facetHits"].as_array().unwrap().len(), 1); +} + #[actix_rt::test] async fn non_filterable_facet_search_error() { let server = Server::new().await; diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index 3e4849578..7bac5ea0c 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -27,8 +27,8 @@ 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 = 100; +/// The maximum number of values per facet returned by the facet search route. +const DEFAULT_MAX_NUMBER_OF_VALUES_PER_FACET: usize = 100; pub mod facet; mod fst_utils; @@ -306,6 +306,7 @@ pub struct SearchForFacetValues<'a> { query: Option, facet: String, search_query: Search<'a>, + max_values: usize, is_hybrid: bool, } @@ -315,7 +316,13 @@ impl<'a> SearchForFacetValues<'a> { search_query: Search<'a>, is_hybrid: bool, ) -> SearchForFacetValues<'a> { - SearchForFacetValues { query: None, facet, search_query, is_hybrid } + SearchForFacetValues { + query: None, + facet, + search_query, + max_values: DEFAULT_MAX_NUMBER_OF_VALUES_PER_FACET, + is_hybrid, + } } pub fn query(&mut self, query: impl Into) -> &mut Self { @@ -323,6 +330,11 @@ impl<'a> SearchForFacetValues<'a> { self } + pub fn max_values(&mut self, max: usize) -> &mut Self { + self.max_values = max; + self + } + fn one_original_value_of( &self, field_id: FieldId, @@ -462,7 +474,7 @@ impl<'a> SearchForFacetValues<'a> { .unwrap_or_else(|| left_bound.to_string()); results.push(FacetValueHit { value, count }); } - if results.len() >= MAX_NUMBER_OF_FACETS { + if results.len() >= self.max_values { break; } } @@ -507,7 +519,7 @@ impl<'a> SearchForFacetValues<'a> { .unwrap_or_else(|| query.to_string()); results.push(FacetValueHit { value, count }); } - if results.len() >= MAX_NUMBER_OF_FACETS { + if results.len() >= self.max_values { return Ok(ControlFlow::Break(())); } }