2020-04-22 17:43:51 +02:00
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use actix_web_macros::{delete, get, post};
|
2020-04-10 19:05:05 +02:00
|
|
|
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
|
2020-04-17 14:52:13 +02:00
|
|
|
use std::collections::BTreeSet;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-05-22 12:03:57 +02:00
|
|
|
use crate::error::{Error, ResponseError};
|
2020-04-22 17:43:51 +02:00
|
|
|
use crate::helpers::Authentication;
|
2020-04-10 19:05:05 +02:00
|
|
|
use crate::routes::{IndexParam, IndexUpdateResponse};
|
2019-10-31 15:00:36 +01:00
|
|
|
use crate::Data;
|
2020-04-10 18:39:52 +02:00
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(get).service(update).service(delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get(
|
|
|
|
"/indexes/{index_uid}/settings/stop-words",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn get(
|
2020-04-17 14:52:13 +02:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-05-22 12:03:57 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-05-19 18:20:29 +02:00
|
|
|
.ok_or(Error::index_not_found(&path.index_uid))?;
|
2020-04-17 14:52:13 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
2020-05-27 12:04:35 +02:00
|
|
|
let stop_words = index.main.stop_words(&reader)?;
|
2020-04-10 18:39:52 +02:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(stop_words))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[post(
|
|
|
|
"/indexes/{index_uid}/settings/stop-words",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn update(
|
2020-04-10 18:39:52 +02:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<BTreeSet<String>>,
|
2020-05-22 12:03:57 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-05-19 18:20:29 +02:00
|
|
|
.ok_or(Error::index_not_found(&path.index_uid))?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-10 18:20:30 +01:00
|
|
|
let settings = SettingsUpdate {
|
2020-04-10 18:39:52 +02:00
|
|
|
stop_words: UpdateState::Update(body.into_inner()),
|
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-05-28 16:12:24 +02:00
|
|
|
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-10 18:39:52 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[delete(
|
|
|
|
"/indexes/{index_uid}/settings/stop-words",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn delete(
|
2020-04-10 18:39:52 +02:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-05-22 12:03:57 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-05-19 18:20:29 +02:00
|
|
|
.ok_or(Error::index_not_found(&path.index_uid))?;
|
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-05-28 16:12:24 +02:00
|
|
|
let update_id = data.db.update_write(|w| index.settings_update(w, settings))?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-10 18:39:52 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|