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

87 lines
2.3 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;
2020-01-16 23:58:57 +08:00
let reader = db.main_read_txn()?;
2019-10-31 22:00:36 +08:00
let synonyms_fst = index
.main
2020-01-16 23:58:57 +08:00
.synonyms_fst(&reader)?;
2019-10-31 22:00:36 +08:00
let synonyms_fst = synonyms_fst.unwrap_or_default();
2020-01-16 23:58:57 +08:00
let synonyms_list = synonyms_fst.stream().into_strs()?;
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 {
2020-01-16 23:58:57 +08:00
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
if let Some(list) = alternative_list {
2020-01-16 23:58:57 +08:00
let list = list.stream().into_strs()?;
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;
2020-01-16 23:58:57 +08:00
let mut writer = db.update_write_txn()?;
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-16 23:58:57 +08:00
let update_id = index.settings_update(&mut writer, settings)?;
2019-10-31 22:00:36 +08:00
2020-01-16 23:58:57 +08:00
writer.commit()?;
2019-10-31 22:00:36 +08:00
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;
2020-01-16 23:58:57 +08:00
let mut writer = db.update_write_txn()?;
2020-01-08 21:17:38 +08:00
let settings = SettingsUpdate {
synonyms: UpdateState::Clear,
.. SettingsUpdate::default()
};
2020-01-16 23:58:57 +08:00
let update_id = index.settings_update(&mut writer, settings)?;
2020-01-16 23:58:57 +08:00
writer.commit()?;
let response_body = IndexUpdateResponse { update_id };
2020-01-16 00:10:33 +08:00
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
}