diff --git a/meilisearch-http/src/index/search.rs b/meilisearch-http/src/index/search.rs index bf559eb91..db0700d89 100644 --- a/meilisearch-http/src/index/search.rs +++ b/meilisearch-http/src/index/search.rs @@ -33,9 +33,8 @@ pub struct SearchQuery { pub attributes_to_crop: Option>, pub crop_length: Option, pub attributes_to_highlight: Option>, - pub filters: Option, pub matches: Option, - pub facet_filters: Option, + pub filter: Option, pub facet_distributions: Option>, } @@ -75,8 +74,8 @@ impl Index { search.limit(query.limit); search.offset(query.offset.unwrap_or_default()); - if let Some(ref facets) = query.facet_filters { - if let Some(facets) = parse_facets(facets, self, &rtxn)? { + if let Some(ref filter) = query.filter { + if let Some(facets) = parse_facets(filter, self, &rtxn)? { search.facet_condition(facets); } } @@ -277,35 +276,6 @@ impl Matcher for MatchingWords { } } -fn parse_facets_array( - txn: &RoTxn, - index: &Index, - arr: &[Value], -) -> anyhow::Result> { - let mut ands = Vec::new(); - for value in arr { - match value { - Value::String(s) => ands.push(Either::Right(s.clone())), - Value::Array(arr) => { - let mut ors = Vec::new(); - for value in arr { - match value { - Value::String(s) => ors.push(s.clone()), - v => bail!("Invalid facet expression, expected String, found: {:?}", v), - } - } - ands.push(Either::Left(ors)); - } - v => bail!( - "Invalid facet expression, expected String or [String], found: {:?}", - v - ), - } - } - - FacetCondition::from_array(txn, &index.0, ands) -} - struct Highlighter<'a, A> { analyzer: Analyzer<'a, A>, marks: (String, String), @@ -367,13 +337,41 @@ fn parse_facets( txn: &RoTxn, ) -> anyhow::Result> { match facets { - // Disabled for now - //Value::String(expr) => Ok(Some(FacetCondition::from_str(txn, index, expr)?)), + Value::String(expr) => Ok(Some(FacetCondition::from_str(txn, index, expr)?)), Value::Array(arr) => parse_facets_array(txn, index, arr), v => bail!("Invalid facet expression, expected Array, found: {:?}", v), } } +fn parse_facets_array( + txn: &RoTxn, + index: &Index, + arr: &[Value], +) -> anyhow::Result> { + let mut ands = Vec::new(); + for value in arr { + match value { + Value::String(s) => ands.push(Either::Right(s.clone())), + Value::Array(arr) => { + let mut ors = Vec::new(); + for value in arr { + match value { + Value::String(s) => ors.push(s.clone()), + v => bail!("Invalid facet expression, expected String, found: {:?}", v), + } + } + ands.push(Either::Left(ors)); + } + v => bail!( + "Invalid facet expression, expected String or [String], found: {:?}", + v + ), + } + } + + FacetCondition::from_array(txn, &index.0, ands) +} + #[cfg(test)] mod test { use std::iter::FromIterator; diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index 86beb2750..be06960cf 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -2,6 +2,7 @@ use std::collections::HashSet; use std::convert::{TryFrom, TryInto}; use actix_web::{get, post, web, HttpResponse}; +use serde_json::Value; use serde::Deserialize; use crate::error::ResponseError; @@ -24,9 +25,8 @@ pub struct SearchQueryGet { attributes_to_crop: Option, crop_length: Option, attributes_to_highlight: Option, - filters: Option, + filter: Option, matches: Option, - facet_filters: Option, facet_distributions: Option, } @@ -50,8 +50,13 @@ impl TryFrom for SearchQuery { .facet_distributions .map(|attrs| attrs.split(',').map(String::from).collect::>()); - let facet_filters = match other.facet_filters { - Some(ref f) => Some(serde_json::from_str(f)?), + let filter = match other.filter { + Some(f) => { + match serde_json::from_str(&f) { + Ok(v) => Some(v), + _ => Some(Value::String(f)), + } + }, None => None, }; @@ -63,9 +68,8 @@ impl TryFrom for SearchQuery { attributes_to_crop, crop_length: other.crop_length, attributes_to_highlight, - filters: other.filters, + filter, matches: other.matches, - facet_filters, facet_distributions, }) }