mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 13:24:27 +01:00
Merge pull request #766 from MarinPostma/empty-facet-attributes-error
Empty facet attributes error
This commit is contained in:
commit
0ee46f773e
@ -183,6 +183,7 @@ pub enum FacetError {
|
|||||||
AttributeNotFound(String),
|
AttributeNotFound(String),
|
||||||
AttributeNotSet { expected: Vec<String>, found: String },
|
AttributeNotSet { expected: Vec<String>, found: String },
|
||||||
InvalidDocumentAttribute(String),
|
InvalidDocumentAttribute(String),
|
||||||
|
NoAttributesForFaceting,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FacetError {
|
impl FacetError {
|
||||||
@ -207,6 +208,7 @@ impl fmt::Display for FacetError {
|
|||||||
AttributeNotFound(attr) => write!(f, "unknown {:?} attribute", attr),
|
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(", ")),
|
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),
|
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"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,9 @@ impl FacetFilter {
|
|||||||
schema: &Schema,
|
schema: &Schema,
|
||||||
attributes_for_faceting: &[FieldId],
|
attributes_for_faceting: &[FieldId],
|
||||||
) -> MResult<FacetFilter> {
|
) -> MResult<FacetFilter> {
|
||||||
|
if attributes_for_faceting.is_empty() {
|
||||||
|
return Err(FacetError::NoAttributesForFaceting.into());
|
||||||
|
}
|
||||||
let parsed = serde_json::from_str::<Value>(s).map_err(|e| FacetError::ParsingError(e.to_string()))?;
|
let parsed = serde_json::from_str::<Value>(s).map_err(|e| FacetError::ParsingError(e.to_string()))?;
|
||||||
let mut filter = Vec::new();
|
let mut filter = Vec::new();
|
||||||
match parsed {
|
match parsed {
|
||||||
|
@ -87,11 +87,9 @@ async fn search_with_url_query(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref facet_filters) = params.facet_filters {
|
if let Some(ref facet_filters) = params.facet_filters {
|
||||||
let attrs = index.main.attributes_for_faceting(&reader)?;
|
let attrs = index.main.attributes_for_faceting(&reader)?.unwrap_or_default();
|
||||||
if let Some(attrs) = attrs {
|
|
||||||
search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?);
|
search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, &attrs)?);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(facets) = ¶ms.facets_distribution {
|
if let Some(facets) = ¶ms.facets_distribution {
|
||||||
match index.main.attributes_for_faceting(&reader)? {
|
match index.main.attributes_for_faceting(&reader)? {
|
||||||
|
@ -1257,8 +1257,9 @@ async fn test_faceted_search_invalid() {
|
|||||||
|
|
||||||
//no faceted attributes set
|
//no faceted attributes set
|
||||||
let query = "q=a&facetFilters=%5B%22color%3Ablue%22,%20%22tags%3Abug%22%20%5D";
|
let query = "q=a&facetFilters=%5B%22color%3Ablue%22,%20%22tags%3Abug%22%20%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
|
|
||||||
let body = json!({
|
let body = json!({
|
||||||
"attributesForFaceting": ["color", "tags"]
|
"attributesForFaceting": ["color", "tags"]
|
||||||
@ -1268,29 +1269,35 @@ async fn test_faceted_search_invalid() {
|
|||||||
// []
|
// []
|
||||||
let query = "q=a&facetFilters=%5B%5D";
|
let query = "q=a&facetFilters=%5B%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
// [[]]
|
// [[]]
|
||||||
let query = "q=a&facetFilters=%5B%5B%5D";
|
let query = "q=a&facetFilters=%5B%5B%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
// ["color:green", []]
|
// ["color:green", []]
|
||||||
let query = "q=a&facetFilters=%5B%22color%3Agreen%22,%20%5B%5D";
|
let query = "q=a&facetFilters=%5B%22color%3Agreen%22,%20%5B%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
|
|
||||||
// too much depth
|
// too much depth
|
||||||
// [[[]]]
|
// [[[]]]
|
||||||
let query = "q=a&facetFilters=%5B%5B%5B%5D%5D%5D";
|
let query = "q=a&facetFilters=%5B%5B%5B%5D%5D%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
// [["color:green", ["color:blue"]]]
|
// [["color:green", ["color:blue"]]]
|
||||||
let query = "q=a&facetFilters=%5B%5B%22color%3Agreen%22,%20%5B%22color%3Ablue%22%5D%5D%5D";
|
let query = "q=a&facetFilters=%5B%5B%22color%3Agreen%22,%20%5B%22color%3Ablue%22%5D%5D%5D";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
// "color:green"
|
// "color:green"
|
||||||
let query = "q=a&facetFilters=%22color%3Agreen%22";
|
let query = "q=a&facetFilters=%22color%3Agreen%22";
|
||||||
let (_response, status_code) = server.search(query).await;
|
let (_response, status_code) = server.search(query).await;
|
||||||
assert_ne!(status_code, 202);
|
assert_eq!(status_code, 400);
|
||||||
|
assert_eq!(response["errorCode"], "invalid_facet");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
Loading…
Reference in New Issue
Block a user