2020-01-10 18:20:30 +01:00
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
|
|
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
|
2020-01-23 11:30:18 +01:00
|
|
|
use tide::{Request, Response};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
use crate::error::{ResponseError, SResult};
|
2020-01-15 17:10:33 +01:00
|
|
|
use crate::helpers::tide::RequestExt;
|
2019-10-31 15:00:36 +01:00
|
|
|
use crate::models::token::ACL::*;
|
|
|
|
use crate::routes::document::IndexUpdateResponse;
|
|
|
|
use crate::Data;
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn get(ctx: Request<Data>) -> SResult<Response> {
|
2019-10-31 15:00:36 +01:00
|
|
|
ctx.is_allowed(SettingsRead)?;
|
|
|
|
let index = ctx.index()?;
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
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()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&stop_words).unwrap())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
|
2019-10-31 15:00:36 +01:00
|
|
|
ctx.is_allowed(SettingsRead)?;
|
|
|
|
let index = ctx.index()?;
|
|
|
|
|
2020-01-10 18:20:30 +01:00
|
|
|
let data: BTreeSet<String> = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let mut writer = db.update_write_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-10 18:20:30 +01:00
|
|
|
let settings = SettingsUpdate {
|
|
|
|
stop_words: UpdateState::Update(data),
|
2020-01-23 11:30:18 +01:00
|
|
|
..SettingsUpdate::default()
|
2020-01-10 18:20:30 +01:00
|
|
|
};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
let update_id = index.settings_update(&mut writer, settings)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let response_body = IndexUpdateResponse { update_id };
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(202).body_json(&response_body)?)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
|
2019-10-31 15:00:36 +01:00
|
|
|
ctx.is_allowed(SettingsRead)?;
|
|
|
|
let index = ctx.index()?;
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let mut writer = db.update_write_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-10 18:20:30 +01:00
|
|
|
let settings = SettingsUpdate {
|
|
|
|
stop_words: UpdateState::Clear,
|
2020-01-23 11:30:18 +01:00
|
|
|
..SettingsUpdate::default()
|
2020-01-10 18:20:30 +01:00
|
|
|
};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
let update_id = index.settings_update(&mut writer, settings)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let response_body = IndexUpdateResponse { update_id };
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(202).body_json(&response_body)?)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|