From 22fbff98d41c5e31fd692dd029e87a9a8fa6d710 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Fri, 10 Apr 2020 18:39:52 +0200 Subject: [PATCH] add stop-word and synonym endpoints --- meilisearch-http/src/main.rs | 6 ++ meilisearch-http/src/routes/mod.rs | 4 +- meilisearch-http/src/routes/stop_words.rs | 86 +++++++++++--------- meilisearch-http/src/routes/synonym.rs | 95 +++++++++++++---------- 4 files changed, 109 insertions(+), 82 deletions(-) diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index 430a56ec8..edddb37d6 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -89,6 +89,12 @@ async fn main() -> Result<(), MainError> { .service(routes::setting::delete_displayed) .service(routes::setting::get_accept_new_fields) .service(routes::setting::update_accept_new_fields) + .service(routes::stop_words::get) + .service(routes::stop_words::update) + .service(routes::stop_words::delete) + .service(routes::synonym::get) + .service(routes::synonym::update) + .service(routes::synonym::delete) .service(routes::key::list) .service(routes::stats::index_stats) .service(routes::stats::get_stats) diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 78fd1a25c..c8401e905 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -12,8 +12,8 @@ pub mod key; pub mod search; pub mod stats; pub mod setting; -// pub mod stop_words; -// pub mod synonym; +pub mod stop_words; +pub mod synonym; #[derive(Default, Deserialize)] pub struct IndexParam { diff --git a/meilisearch-http/src/routes/stop_words.rs b/meilisearch-http/src/routes/stop_words.rs index f9d0154a7..2234497bf 100644 --- a/meilisearch-http/src/routes/stop_words.rs +++ b/meilisearch-http/src/routes/stop_words.rs @@ -1,63 +1,73 @@ use std::collections::BTreeSet; use meilisearch_core::settings::{SettingsUpdate, UpdateState}; -use tide::{Request, Response}; +use actix_web::{web, get, post, delete, HttpResponse}; +use actix_web as aweb; -use crate::error::{ResponseError, SResult}; -use crate::helpers::tide::RequestExt; -use crate::helpers::tide::ACL::*; -use crate::routes::document::IndexUpdateResponse; +use crate::error::{ResponseError}; use crate::Data; +use crate::routes::{IndexUpdateResponse, IndexParam}; -pub async fn get(ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - let index = ctx.index()?; - let db = &ctx.state().db; - let reader = db.main_read_txn()?; - let stop_words_fst = index.main.stop_words_fst(&reader)?; - let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; +#[get("/indexes/{index_uid}/settings/stop-words")] +pub async fn get( + data: web::Data, + path: web::Path, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; + let reader = data.db.main_read_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let stop_words_fst = index.main.stop_words_fst(&reader) + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - Ok(tide::Response::new(200).body_json(&stop_words).unwrap()) + Ok(HttpResponse::Ok().json(stop_words)) } -pub async fn update(mut ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - let index = ctx.index()?; - - let data: BTreeSet = ctx.body_json().await.map_err(ResponseError::bad_request)?; - - let db = &ctx.state().db; - let mut writer = db.update_write_txn()?; +#[post("/indexes/{index_uid}/settings/stop-words")] +pub async fn update( + data: web::Data, + path: web::Path, + body: web::Json>, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { - stop_words: UpdateState::Update(data), + stop_words: UpdateState::Update(body.into_inner()), ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings)?; + let mut writer = data.db.update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index.settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; + writer.commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit()?; - - let response_body = IndexUpdateResponse { update_id }; - Ok(tide::Response::new(202).body_json(&response_body)?) + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } -pub async fn delete(ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - let index = ctx.index()?; - - let db = &ctx.state().db; - let mut writer = db.update_write_txn()?; +#[delete("/indexes/{index_uid}/settings/stop-words")] +pub async fn delete( + data: web::Data, + path: web::Path, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { stop_words: UpdateState::Clear, ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings)?; + let mut writer = data.db.update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index.settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; + writer.commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit()?; - - let response_body = IndexUpdateResponse { update_id }; - Ok(tide::Response::new(202).body_json(&response_body)?) + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } diff --git a/meilisearch-http/src/routes/synonym.rs b/meilisearch-http/src/routes/synonym.rs index 60fca7e98..587582ee9 100644 --- a/meilisearch-http/src/routes/synonym.rs +++ b/meilisearch-http/src/routes/synonym.rs @@ -2,81 +2,92 @@ use std::collections::BTreeMap; use indexmap::IndexMap; use meilisearch_core::settings::{SettingsUpdate, UpdateState}; -use tide::{Request, Response}; +use actix_web::{web, get, post, delete, HttpResponse}; +use actix_web as aweb; -use crate::error::{ResponseError, SResult}; -use crate::helpers::tide::RequestExt; -use crate::helpers::tide::ACL::*; -use crate::routes::document::IndexUpdateResponse; +use crate::error::{ResponseError}; use crate::Data; +use crate::routes::{IndexUpdateResponse, IndexParam}; -pub async fn get(ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - let index = ctx.index()?; +#[get("/indexes/{index_uid}/settings/synonyms")] +pub async fn get( + data: web::Data, + path: web::Path, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; - let db = &ctx.state().db; - let reader = db.main_read_txn()?; + let reader = data.db.main_read_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs()?; + let synonyms_fst = index.main.synonyms_fst(&reader) + .map_err(|err| ResponseError::Internal(err.to_string()))? + .unwrap_or_default(); + let synonyms_list = synonyms_fst.stream().into_strs() + .map_err(|err| ResponseError::Internal(err.to_string()))?; let mut synonyms = IndexMap::new(); let index_synonyms = &index.synonyms; for synonym in synonyms_list { - let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?; + let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes()) + .map_err(|err| ResponseError::Internal(err.to_string()))?; if let Some(list) = alternative_list { - let list = list.stream().into_strs()?; + let list = list.stream().into_strs() + .map_err(|err| ResponseError::Internal(err.to_string()))?; synonyms.insert(synonym, list); } } - Ok(tide::Response::new(200).body_json(&synonyms).unwrap()) + Ok(HttpResponse::Ok().json(synonyms)) } -pub async fn update(mut ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - - let data: BTreeMap> = - ctx.body_json().await.map_err(ResponseError::bad_request)?; - - let index = ctx.index()?; - - let db = &ctx.state().db; - let mut writer = db.update_write_txn()?; +#[post("/indexes/{index_uid}/settings/synonyms")] +pub async fn update( + data: web::Data, + path: web::Path, + body: web::Json>>, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { - synonyms: UpdateState::Update(data), + synonyms: UpdateState::Update(body.into_inner()), ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings)?; + let mut writer = data.db.update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index.settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; + writer.commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit()?; - - let response_body = IndexUpdateResponse { update_id }; - Ok(tide::Response::new(202).body_json(&response_body)?) + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) } -pub async fn delete(ctx: Request) -> SResult { - ctx.is_allowed(Private)?; - - let index = ctx.index()?; - - let db = &ctx.state().db; - let mut writer = db.update_write_txn()?; +#[delete("/indexes/{index_uid}/settings/synonyms")] +pub async fn delete( + data: web::Data, + path: web::Path, +) -> aweb::Result { + let index = data.db.open_index(&path.index_uid) + .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?; let settings = SettingsUpdate { synonyms: UpdateState::Clear, ..SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings)?; + let mut writer = data.db.update_write_txn() + .map_err(|err| ResponseError::Internal(err.to_string()))?; + let update_id = index.settings_update(&mut writer, settings) + .map_err(|err| ResponseError::Internal(err.to_string()))?; - writer.commit()?; + writer.commit() + .map_err(|err| ResponseError::Internal(err.to_string()))?; - let response_body = IndexUpdateResponse { update_id }; - Ok(tide::Response::new(202).body_json(&response_body)?) + Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id))) }