mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 12:27:13 +02:00
enables facet count
This commit is contained in:
parent
effbb7f7f1
commit
e5126af458
8 changed files with 88 additions and 58 deletions
|
@ -157,7 +157,7 @@ impl<'a> SearchBuilder<'a> {
|
|||
query_builder.set_facets(self.facets.as_deref());
|
||||
|
||||
let start = Instant::now();
|
||||
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
|
||||
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit), &schema);
|
||||
let search_result = result.map_err(ResponseError::search_documents)?;
|
||||
let time_ms = start.elapsed().as_millis() as usize;
|
||||
|
||||
|
@ -247,7 +247,7 @@ impl<'a> SearchBuilder<'a> {
|
|||
exhaustive_nb_hits: search_result.is_exhaustive,
|
||||
processing_time_ms: time_ms,
|
||||
query: self.query.to_string(),
|
||||
facets: search_result.facets
|
||||
facets: search_result.facets,
|
||||
};
|
||||
|
||||
Ok(results)
|
||||
|
@ -332,6 +332,7 @@ pub struct SearchResult {
|
|||
pub exhaustive_nb_hits: bool,
|
||||
pub processing_time_ms: usize,
|
||||
pub query: String,
|
||||
pub facets: Option<HashMap<String, HashMap<String, usize>>>,
|
||||
}
|
||||
|
||||
/// returns the start index and the length on the crop.
|
||||
|
|
|
@ -33,7 +33,7 @@ struct SearchQuery {
|
|||
filters: Option<String>,
|
||||
matches: Option<bool>,
|
||||
facet_filters: Option<String>,
|
||||
facets: Option<String>
|
||||
facets: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
|
||||
|
@ -94,9 +94,12 @@ async fn search_with_url_query(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(ref facets) = params.facets {
|
||||
if let Some(facets) = ¶ms.facets {
|
||||
match index.main.attributes_for_faceting(&reader)? {
|
||||
Some(ref attrs) => { search_builder.add_facets(prepare_facet_list(facets, &schema, attrs)?); },
|
||||
Some(ref attrs) => {
|
||||
let field_ids = prepare_facet_list(&facets, &schema, attrs)?;
|
||||
search_builder.add_facets(field_ids);
|
||||
},
|
||||
None => return Err(ResponseError::FacetExpression("can't return facets count, as no facet is set".to_string()))
|
||||
}
|
||||
|
||||
|
@ -162,26 +165,35 @@ async fn search_with_url_query(
|
|||
Ok(HttpResponse::Ok().json(search_builder.search(&reader)?))
|
||||
}
|
||||
|
||||
fn prepare_facet_list<'fa>(facets: &str, schema: &Schema, facet_attrs: &'fa [FieldId]) -> Result<Vec<FieldId>, ResponseError> {
|
||||
let facet_array = serde_json::from_str(facets).expect("do error handling"); // TODO
|
||||
match facet_array {
|
||||
Value::Array(facet_array) => {
|
||||
let wild_card = Value::String("*".to_string());
|
||||
if facet_array.iter().any(|it| it == &wild_card) {
|
||||
return Ok(Vec::from(facet_attrs)); // TODO can make cow?
|
||||
/// Parses the incoming string into an array of attributes for which to return a count. It returns
|
||||
/// a Vec of attribute names ascociated with their id.
|
||||
///
|
||||
/// An error is returned if the array is malformed, or if it contains attributes that are
|
||||
/// unexisting, or not set as facets.
|
||||
fn prepare_facet_list(facets: &str, schema: &Schema, facet_attrs: &[FieldId]) -> Result<Vec<(FieldId, String)>, FacetCountError> {
|
||||
let json_array = serde_json::from_str(facets)?;
|
||||
match json_array {
|
||||
Value::Array(vals) => {
|
||||
let wildcard = Value::String("*".to_string());
|
||||
if vals.iter().any(|f| f == &wildcard) {
|
||||
return Ok(Vec::from(facet_attrs));
|
||||
}
|
||||
let mut fields = Vec::with_capacity(facet_attrs.len());
|
||||
for v in facet_array {
|
||||
match v {
|
||||
Value::String(name) => {
|
||||
let id = schema.id(&name).expect("not found error"); // TODO
|
||||
fields.push(id);
|
||||
let mut field_ids = Vec::new();
|
||||
for facet in vals {
|
||||
match facet {
|
||||
Value::String(facet) => {
|
||||
if let Some(id) = schema.id(&facet) {
|
||||
if !facet_attrs.contains(&id) {
|
||||
return Err(ResponseError::FacetExpression("Only attributes set as facet can be counted".to_string())); // TODO make special error
|
||||
}
|
||||
field_ids.push(id);
|
||||
}
|
||||
}
|
||||
_ => todo!("expected string, found {}", v),
|
||||
bad_val => return Err(ResponseError::FacetExpression(format!("expected String found {}", bad_val)))
|
||||
}
|
||||
}
|
||||
return Ok(fields);
|
||||
Ok(field_ids)
|
||||
}
|
||||
_ => todo!("error, bad syntax, expected array")
|
||||
bad_val => return Err(ResponseError::FacetExpression(format!("expected Array found {}", bad_val)))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue