add stop-word and synonym endpoints

This commit is contained in:
Quentin de Quelen 2020-04-10 18:39:52 +02:00 committed by qdequele
parent 85833e3a0a
commit 22fbff98d4
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
4 changed files with 109 additions and 82 deletions

View File

@ -89,6 +89,12 @@ async fn main() -> Result<(), MainError> {
.service(routes::setting::delete_displayed) .service(routes::setting::delete_displayed)
.service(routes::setting::get_accept_new_fields) .service(routes::setting::get_accept_new_fields)
.service(routes::setting::update_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::key::list)
.service(routes::stats::index_stats) .service(routes::stats::index_stats)
.service(routes::stats::get_stats) .service(routes::stats::get_stats)

View File

@ -12,8 +12,8 @@ pub mod key;
pub mod search; pub mod search;
pub mod stats; pub mod stats;
pub mod setting; pub mod setting;
// pub mod stop_words; pub mod stop_words;
// pub mod synonym; pub mod synonym;
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
pub struct IndexParam { pub struct IndexParam {

View File

@ -1,63 +1,73 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; 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::error::{ResponseError};
use crate::helpers::tide::RequestExt;
use crate::helpers::tide::ACL::*;
use crate::routes::document::IndexUpdateResponse;
use crate::Data; use crate::Data;
use crate::routes::{IndexUpdateResponse, IndexParam};
pub async fn get(ctx: Request<Data>) -> SResult<Response> { #[get("/indexes/{index_uid}/settings/stop-words")]
ctx.is_allowed(Private)?; pub async fn get(
let index = ctx.index()?; data: web::Data<Data>,
let db = &ctx.state().db; path: web::Path<IndexParam>,
let reader = db.main_read_txn()?; ) -> aweb::Result<HttpResponse> {
let stop_words_fst = index.main.stop_words_fst(&reader)?; let index = data.db.open_index(&path.index_uid)
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; .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<Data>) -> SResult<Response> { #[post("/indexes/{index_uid}/settings/stop-words")]
ctx.is_allowed(Private)?; pub async fn update(
let index = ctx.index()?; data: web::Data<Data>,
path: web::Path<IndexParam>,
let data: BTreeSet<String> = ctx.body_json().await.map_err(ResponseError::bad_request)?; body: web::Json<BTreeSet<String>>,
) -> aweb::Result<HttpResponse> {
let db = &ctx.state().db; let index = data.db.open_index(&path.index_uid)
let mut writer = db.update_write_txn()?; .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Update(data), stop_words: UpdateState::Update(body.into_inner()),
..SettingsUpdate::default() ..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()?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { #[delete("/indexes/{index_uid}/settings/stop-words")]
ctx.is_allowed(Private)?; pub async fn delete(
let index = ctx.index()?; data: web::Data<Data>,
path: web::Path<IndexParam>,
let db = &ctx.state().db; ) -> aweb::Result<HttpResponse> {
let mut writer = db.update_write_txn()?; let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Clear, stop_words: UpdateState::Clear,
..SettingsUpdate::default() ..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()?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -2,81 +2,92 @@ use std::collections::BTreeMap;
use indexmap::IndexMap; use indexmap::IndexMap;
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; 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::error::{ResponseError};
use crate::helpers::tide::RequestExt;
use crate::helpers::tide::ACL::*;
use crate::routes::document::IndexUpdateResponse;
use crate::Data; use crate::Data;
use crate::routes::{IndexUpdateResponse, IndexParam};
pub async fn get(ctx: Request<Data>) -> SResult<Response> { #[get("/indexes/{index_uid}/settings/synonyms")]
ctx.is_allowed(Private)?; pub async fn get(
let index = ctx.index()?; data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let db = &ctx.state().db; let reader = data.db.main_read_txn()
let reader = 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_fst = index.main.synonyms_fst(&reader)
let synonyms_list = synonyms_fst.stream().into_strs()?; .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 mut synonyms = IndexMap::new();
let index_synonyms = &index.synonyms; let index_synonyms = &index.synonyms;
for synonym in synonyms_list { 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 { 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); 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<Data>) -> SResult<Response> { #[post("/indexes/{index_uid}/settings/synonyms")]
ctx.is_allowed(Private)?; pub async fn update(
data: web::Data<Data>,
let data: BTreeMap<String, Vec<String>> = path: web::Path<IndexParam>,
ctx.body_json().await.map_err(ResponseError::bad_request)?; body: web::Json<BTreeMap<String, Vec<String>>>,
) -> aweb::Result<HttpResponse> {
let index = ctx.index()?; let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Update(data), synonyms: UpdateState::Update(body.into_inner()),
..SettingsUpdate::default() ..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()?; Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { #[delete("/indexes/{index_uid}/settings/synonyms")]
ctx.is_allowed(Private)?; pub async fn delete(
data: web::Data<Data>,
let index = ctx.index()?; path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
let db = &ctx.state().db; let index = data.db.open_index(&path.index_uid)
let mut writer = db.update_write_txn()?; .ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Clear, synonyms: UpdateState::Clear,
..SettingsUpdate::default() ..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(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
Ok(tide::Response::new(202).body_json(&response_body)?)
} }