MeiliSearch/meilisearch-http/src/routes/stop_words.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

2020-04-10 19:05:05 +02:00
use actix_web::{delete, get, post, web, HttpResponse};
use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use std::collections::BTreeSet;
2019-10-31 15:00:36 +01:00
2020-04-10 19:05:05 +02:00
use crate::error::ResponseError;
use crate::routes::{IndexParam, IndexUpdateResponse};
2019-10-31 15:00:36 +01:00
use crate::Data;
2020-04-10 18:39:52 +02:00
#[get("/indexes/{index_uid}/settings/stop-words")]
pub async fn get(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
2020-04-10 19:05:05 +02:00
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
let reader = data.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()?;
2020-04-10 18:39:52 +02:00
Ok(HttpResponse::Ok().json(stop_words))
2019-10-31 15:00:36 +01:00
}
2020-04-10 18:39:52 +02:00
#[post("/indexes/{index_uid}/settings/stop-words")]
pub async fn update(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<BTreeSet<String>>,
) -> Result<HttpResponse, ResponseError> {
2020-04-10 19:05:05 +02:00
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::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
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?;
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-10 18:39:52 +02:00
#[delete("/indexes/{index_uid}/settings/stop-words")]
pub async fn delete(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
2020-04-10 19:05:05 +02:00
let index = data
.db
.open_index(&path.index_uid)
.ok_or(ResponseError::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
let mut writer = data.db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?;
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
}