meilisearch/meilisearch-http/src/routes/synonym.rs

92 lines
2.7 KiB
Rust
Raw Normal View History

2020-01-11 01:20:30 +08:00
use std::collections::BTreeMap;
2019-10-31 22:00:36 +08:00
2020-01-16 00:10:33 +08:00
use tide::{Request, Response};
2019-12-11 23:49:09 +08:00
use indexmap::IndexMap;
2020-01-11 01:20:30 +08:00
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
2019-10-31 22:00:36 +08:00
use crate::error::{ResponseError, SResult};
2020-01-16 00:10:33 +08:00
use crate::helpers::tide::RequestExt;
2019-10-31 22:00:36 +08:00
use crate::models::token::ACL::*;
use crate::routes::document::IndexUpdateResponse;
use crate::Data;
2020-01-16 00:10:33 +08:00
pub async fn get(ctx: Request<Data>) -> SResult<Response> {
2019-10-31 22:00:36 +08:00
ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?;
let db = &ctx.state().db;
let reader = db.main_read_txn().map_err(ResponseError::internal)?;
2019-10-31 22:00:36 +08:00
let synonyms_fst = index
.main
.synonyms_fst(&reader)
.map_err(ResponseError::internal)?;
let synonyms_fst = synonyms_fst.unwrap_or_default();
let synonyms_list = synonyms_fst.stream().into_strs().map_err(ResponseError::internal)?;
2019-10-31 22:00:36 +08:00
2019-12-11 23:49:09 +08:00
let mut response = IndexMap::new();
2019-10-31 22:00:36 +08:00
let index_synonyms = &index.synonyms;
for synonym in synonyms_list {
let alternative_list = index_synonyms
.synonyms(&reader, synonym.as_bytes())
.map_err(ResponseError::internal)?;
if let Some(list) = alternative_list {
let list = list.stream().into_strs().map_err(ResponseError::internal)?;
response.insert(synonym, list);
}
2019-10-31 22:00:36 +08:00
}
2020-01-16 00:10:33 +08:00
Ok(tide::Response::new(200).body_json(&response).unwrap())
2019-10-31 22:00:36 +08:00
}
2020-01-16 00:10:33 +08:00
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
2019-10-31 22:00:36 +08:00
ctx.is_allowed(SettingsWrite)?;
2020-01-11 01:20:30 +08:00
let data: BTreeMap<String, Vec<String>> = ctx.body_json().await.map_err(ResponseError::bad_request)?;
2019-10-31 22:00:36 +08:00
let index = ctx.index()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn().map_err(ResponseError::internal)?;
2019-10-31 22:00:36 +08:00
2020-01-11 01:20:30 +08:00
let settings = SettingsUpdate {
synonyms: UpdateState::Update(data),
.. SettingsUpdate::default()
};
2019-10-31 22:00:36 +08:00
2020-01-11 01:20:30 +08:00
let update_id = index.settings_update(&mut writer, settings)
2019-10-31 22:00:36 +08:00
.map_err(ResponseError::internal)?;
writer.commit().map_err(ResponseError::internal)?;
let response_body = IndexUpdateResponse { update_id };
2020-01-16 00:10:33 +08:00
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
2019-10-31 22:00:36 +08:00
}
2020-01-16 00:10:33 +08:00
pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn().map_err(ResponseError::internal)?;
2020-01-08 21:17:38 +08:00
let settings = SettingsUpdate {
synonyms: UpdateState::Clear,
.. SettingsUpdate::default()
};
2020-01-11 01:20:30 +08:00
let update_id = index.settings_update(&mut writer, settings)
.map_err(ResponseError::internal)?;
writer.commit().map_err(ResponseError::internal)?;
let response_body = IndexUpdateResponse { update_id };
2020-01-16 00:10:33 +08:00
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
}