From 2ada9c5d72f770c085e71da9186a46d3e0ac8d39 Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 10 Jun 2020 12:03:19 +0200 Subject: [PATCH] add error on search with empty facets --- meilisearch-core/src/error.rs | 2 ++ meilisearch-core/src/facets.rs | 3 +++ meilisearch-http/src/routes/search.rs | 6 ++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/meilisearch-core/src/error.rs b/meilisearch-core/src/error.rs index 62840c2a8..bbb33526a 100644 --- a/meilisearch-core/src/error.rs +++ b/meilisearch-core/src/error.rs @@ -183,6 +183,7 @@ pub enum FacetError { AttributeNotFound(String), AttributeNotSet { expected: Vec, found: String }, InvalidDocumentAttribute(String), + NoAttributesForFaceting, } impl FacetError { @@ -207,6 +208,7 @@ impl fmt::Display for FacetError { AttributeNotFound(attr) => write!(f, "unknown {:?} attribute", attr), AttributeNotSet { found, expected } => write!(f, "`{}` is not set as a faceted attribute. available facet attributes: {}", found, expected.join(", ")), InvalidDocumentAttribute(attr) => write!(f, "invalid document attribute {}, accepted types: String and [String]", attr), + NoAttributesForFaceting => write!(f, "impossible to perform faceted search, no attributes for faceting are set"), } } } diff --git a/meilisearch-core/src/facets.rs b/meilisearch-core/src/facets.rs index cc1737eaf..2f632dd94 100644 --- a/meilisearch-core/src/facets.rs +++ b/meilisearch-core/src/facets.rs @@ -35,6 +35,9 @@ impl FacetFilter { schema: &Schema, attributes_for_faceting: &[FieldId], ) -> MResult { + if attributes_for_faceting.is_empty() { + return Err(FacetError::NoAttributesForFaceting.into()); + } let parsed = serde_json::from_str::(s).map_err(|e| FacetError::ParsingError(e.to_string()))?; let mut filter = Vec::new(); match parsed { diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index 1a2521386..0d698eafb 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -87,10 +87,8 @@ async fn search_with_url_query( } if let Some(ref facet_filters) = params.facet_filters { - let attrs = index.main.attributes_for_faceting(&reader)?; - if let Some(attrs) = attrs { - search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?); - } + let attrs = index.main.attributes_for_faceting(&reader)?.unwrap_or_default(); + search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?); } if let Some(facets) = ¶ms.facets_distribution {