diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index c7abd6ce4..46268694d 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -25,7 +25,10 @@ pub fn services(cfg: &mut web::ServiceConfig) { .service(update_displayed) .service(delete_displayed) .service(get_accept_new_fields) - .service(update_accept_new_fields); + .service(update_accept_new_fields) + .service(get_attributes_for_faceting) + .service(delete_attributes_for_faceting) + .service(update_attributes_for_faceting); } #[post("/indexes/{index_uid}/settings", wrap = "Authentication::Private")] @@ -94,7 +97,7 @@ async fn get_all( (Some(schema), Some(attrs)) => { Some(attrs .iter() - .filter_map(|&id| schema .name(id)) + .filter_map(|&id| schema.name(id)) .map(str::to_string) .collect()) } @@ -481,3 +484,85 @@ async fn update_accept_new_fields( Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } + +#[get( + "/indexes/{index_uid}/settings/attributes-for-faceting", + wrap = "Authentication::Private" +)] +async fn get_attributes_for_faceting( + data: web::Data, + path: web::Path, +) -> Result { + let index = data + .db + .open_index(&path.index_uid) + .ok_or(Error::index_not_found(&path.index_uid))?; + + let attributes_for_faceting = data + .db + .main_read::<_, Option>, ResponseError>(|reader| { + let schema = index.main.schema(reader)?; + let attrs = index.main.attributes_for_faceting(reader)?; + let attr_names = match (&schema, &attrs) { + (Some(schema), Some(attrs)) => { + Some(attrs + .iter() + .filter_map(|&id| schema.name(id)) + .map(str::to_string) + .collect()) + } + _ => None, + }; + Ok(attr_names) + })?; + + Ok(HttpResponse::Ok().json(attributes_for_faceting)) +} + +#[post( + "/indexes/{index_uid}/settings/attributes-for-faceting", + wrap = "Authentication::Private" +)] +async fn update_attributes_for_faceting( + data: web::Data, + path: web::Path, + body: web::Json>>, +) -> Result { + let index = data + .db + .open_index(&path.index_uid) + .ok_or(Error::index_not_found(&path.index_uid))?; + + let settings = Settings { + attributes_for_faceting: Some(body.into_inner()), + ..Settings::default() + }; + + let settings = settings.into_update().map_err(Error::bad_request)?; + let update_id = data.db.update_write(|w| index.settings_update(w, settings))?; + + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) +} + +#[delete( + "/indexes/{index_uid}/settings/attributes-for-faceting", + wrap = "Authentication::Private" +)] +async fn delete_attributes_for_faceting( + data: web::Data, + path: web::Path, +) -> Result { + let index = data + .db + .open_index(&path.index_uid) + .ok_or(Error::index_not_found(&path.index_uid))?; + + let settings = SettingsUpdate { + attributes_for_faceting: UpdateState::Clear, + ..SettingsUpdate::default() + }; + + let update_id = data.db.update_write(|w| index.settings_update(w, settings))?; + + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) +}