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

91 lines
2.4 KiB
Rust
Raw Normal View History

2020-01-10 18:20:30 +01:00
use std::collections::BTreeMap;
2019-10-31 15:00:36 +01:00
use actix_web::{web, HttpResponse};
2020-09-12 04:29:17 +02:00
use actix_web::{delete, get, post};
2019-12-11 16:49:09 +01:00
use indexmap::IndexMap;
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::{Error, ResponseError};
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;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get).service(update).service(delete);
}
#[get(
"/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
)]
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)
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
let reader = data.db.main_read_txn()?;
2019-10-31 15:00:36 +01:00
2020-05-27 12:04:35 +02:00
let synonyms_list = index.main.synonyms(&reader)?;
2019-10-31 15:00:36 +01:00
let mut synonyms = IndexMap::new();
2019-10-31 15:00:36 +01:00
let index_synonyms = &index.synonyms;
for synonym in synonyms_list {
2020-05-22 18:04:23 +02:00
let list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
synonyms.insert(synonym, list);
2019-10-31 15:00:36 +01:00
}
2020-04-10 18:39:52 +02:00
Ok(HttpResponse::Ok().json(synonyms))
2019-10-31 15:00:36 +01:00
}
#[post(
"/indexes/{index_uid}/settings/synonyms",
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<BTreeMap<String, Vec<String>>>,
) -> Result<HttpResponse, ResponseError> {
let update_id = data.get_or_create_index(&path.index_uid, |index| {
let settings = SettingsUpdate {
synonyms: UpdateState::Update(body.into_inner()),
..SettingsUpdate::default()
};
2019-10-31 15:00:36 +01:00
Ok(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
}
#[delete(
"/indexes/{index_uid}/settings/synonyms",
wrap = "Authentication::Private"
)]
async fn delete(
2020-04-10 18:39:52 +02:00
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)
2020-05-19 18:20:29 +02:00
.ok_or(Error::index_not_found(&path.index_uid))?;
2020-01-08 14:17:38 +01:00
let settings = SettingsUpdate {
synonyms: UpdateState::Clear,
2020-01-23 11:30:18 +01:00
..SettingsUpdate::default()
2020-01-08 14:17:38 +01:00
};
let update_id = data
.db
.update_write(|w| index.settings_update(w, settings))?;
2020-04-10 18:39:52 +02:00
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}