From 4822fe1beb4825abe416a04148993b9fdcfec21b Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Tue, 15 Mar 2022 18:12:51 -0300 Subject: [PATCH 1/2] Add a better error message when the filterable attrs are empty Fixes https://github.com/meilisearch/meilisearch/issues/2140 --- milli/src/search/facet/filter.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/milli/src/search/facet/filter.rs b/milli/src/search/facet/filter.rs index edc86d0ca..932fd21d9 100644 --- a/milli/src/search/facet/filter.rs +++ b/milli/src/search/facet/filter.rs @@ -39,12 +39,22 @@ impl<'a> std::error::Error for FilterError<'a> {} impl<'a> Display for FilterError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::AttributeNotFilterable { attribute, filterable } => write!( - f, - "Attribute `{}` is not filterable. Available filterable attributes are: `{}`.", - attribute, - filterable, - ), + Self::AttributeNotFilterable { attribute, filterable } => { + if filterable.is_empty() { + write!( + f, + "Attribute `{}` is not filterable. This index does not have configured filterable attributes.", + attribute, + ) + } else { + write!( + f, + "Attribute `{}` is not filterable. Available filterable attributes are: `{}`.", + attribute, + filterable, + ) + } + }, Self::TooDeep => write!(f, "Too many filter conditions, can't process more than {} filters.", MAX_FILTER_DEPTH @@ -554,13 +564,13 @@ mod tests { let filter = Filter::from_str("_geoRadius(42, 150, 10)").unwrap().unwrap(); let error = filter.evaluate(&rtxn, &index).unwrap_err(); assert!(error.to_string().starts_with( - "Attribute `_geo` is not filterable. Available filterable attributes are: ``." + "Attribute `_geo` is not filterable. This index does not have configured filterable attributes." )); let filter = Filter::from_str("dog = \"bernese mountain\"").unwrap().unwrap(); let error = filter.evaluate(&rtxn, &index).unwrap_err(); assert!(error.to_string().starts_with( - "Attribute `dog` is not filterable. Available filterable attributes are: ``." + "Attribute `dog` is not filterable. This index does not have configured filterable attributes." )); drop(rtxn); From adc71742c8dca54e731f23e96e594718ee7fe95f Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Tue, 15 Mar 2022 18:36:10 -0300 Subject: [PATCH 2/2] Move string concat to the struct instead of in the calling --- milli/src/search/facet/filter.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/milli/src/search/facet/filter.rs b/milli/src/search/facet/filter.rs index 932fd21d9..9388cfa33 100644 --- a/milli/src/search/facet/filter.rs +++ b/milli/src/search/facet/filter.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::fmt::{Debug, Display}; use std::ops::Bound::{self, Excluded, Included}; use std::ops::Deref; @@ -27,7 +28,7 @@ pub struct Filter<'a> { #[derive(Debug)] enum FilterError<'a> { - AttributeNotFilterable { attribute: &'a str, filterable: String }, + AttributeNotFilterable { attribute: &'a str, filterable_fields: HashSet }, BadGeo(&'a str), BadGeoLat(f64), BadGeoLng(f64), @@ -39,19 +40,21 @@ impl<'a> std::error::Error for FilterError<'a> {} impl<'a> Display for FilterError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::AttributeNotFilterable { attribute, filterable } => { - if filterable.is_empty() { + Self::AttributeNotFilterable { attribute, filterable_fields } => { + if filterable_fields.is_empty() { write!( f, "Attribute `{}` is not filterable. This index does not have configured filterable attributes.", attribute, ) } else { + let filterables_list = filterable_fields.iter().map(AsRef::as_ref).collect::>().join(" "); + write!( f, "Attribute `{}` is not filterable. Available filterable attributes are: `{}`.", attribute, - filterable, + filterables_list, ) } }, @@ -372,10 +375,7 @@ impl<'a> Filter<'a> { return Err(fid.as_external_error( FilterError::AttributeNotFilterable { attribute, - filterable: filterable_fields - .into_iter() - .collect::>() - .join(" "), + filterable_fields, }, ))?; } @@ -426,7 +426,7 @@ impl<'a> Filter<'a> { } else { return Err(point[0].as_external_error(FilterError::AttributeNotFilterable { attribute: "_geo", - filterable: filterable_fields.into_iter().collect::>().join(" "), + filterable_fields, }))?; } }