82 lines
2.4 KiB
Rust
Raw Normal View History

2020-01-10 18:20:30 +01:00
use std::collections::BTreeSet;
2019-10-31 15:00:36 +01:00
use http::StatusCode;
use tide::response::IntoResponse;
use tide::{Context, Response};
2020-01-10 18:20:30 +01:00
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
2019-10-31 15:00:36 +01:00
use crate::error::{ResponseError, SResult};
use crate::helpers::tide::ContextExt;
use crate::models::token::ACL::*;
use crate::routes::document::IndexUpdateResponse;
use crate::Data;
pub async fn get(ctx: Context<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01: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 15:00:36 +01:00
let stop_words_fst = index
.main
.stop_words_fst(&reader)
.map_err(ResponseError::internal)?;
let stop_words = stop_words_fst
.unwrap_or_default()
.stream()
.into_strs()
.map_err(ResponseError::internal)?;
Ok(tide::response::json(stop_words))
}
pub async fn update(mut ctx: Context<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
let db = &ctx.state().db;
let mut writer = db.update_write_txn().map_err(ResponseError::internal)?;
2019-10-31 15:00:36 +01:00
2020-01-10 18:20:30 +01:00
let settings = SettingsUpdate {
stop_words: UpdateState::Update(data),
.. SettingsUpdate::default()
};
2019-10-31 15:00:36 +01:00
2020-01-10 18:20:30 +01:00
let update_id = index.settings_update(&mut writer, settings)
2019-10-31 15:00:36 +01:00
.map_err(ResponseError::internal)?;
writer.commit().map_err(ResponseError::internal)?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::response::json(response_body)
.with_status(StatusCode::ACCEPTED)
.into_response())
}
pub async fn delete(ctx: Context<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01:00
ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?;
let db = &ctx.state().db;
let mut writer = db.update_write_txn().map_err(ResponseError::internal)?;
2019-10-31 15:00:36 +01:00
2020-01-10 18:20:30 +01:00
let settings = SettingsUpdate {
stop_words: UpdateState::Clear,
.. SettingsUpdate::default()
};
2019-10-31 15:00:36 +01:00
2020-01-10 18:20:30 +01:00
let update_id = index.settings_update(&mut writer, settings)
2019-10-31 15:00:36 +01:00
.map_err(ResponseError::internal)?;
writer.commit().map_err(ResponseError::internal)?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::response::json(response_body)
.with_status(StatusCode::ACCEPTED)
.into_response())
}