2020-01-10 18:20:30 +01:00
|
|
|
use std::collections::BTreeMap;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use actix_web_macros::{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
|
|
|
|
2020-04-10 19:05:05 +02:00
|
|
|
use crate::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-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/synonyms",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn get(
|
2020-04-17 14:52:13 +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-04-17 14:52:13 +02:00
|
|
|
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
|
|
|
|
let synonyms_list = synonyms_fst.stream().into_strs()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-17 14:52:13 +02: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-04-17 14:52:13 +02:00
|
|
|
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
|
2019-12-09 16:54:44 +01:00
|
|
|
|
|
|
|
if let Some(list) = alternative_list {
|
2020-04-17 14:52:13 +02:00
|
|
|
let list = list.stream().into_strs()?;
|
2020-01-16 19:19:44 +01:00
|
|
|
synonyms.insert(synonym, list);
|
2019-12-09 16:54:44 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02: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>>>,
|
2020-04-17 14:52:13 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-17 14:52:13 +02:00
|
|
|
.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
|
|
|
synonyms: 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-04-17 14:52:13 +02: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-01-02 17:10:49 +01:00
|
|
|
|
2020-04-22 17:43:51 +02: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>,
|
2020-04-17 14:52:13 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-04-10 19:05:05 +02:00
|
|
|
let index = data
|
|
|
|
.db
|
|
|
|
.open_index(&path.index_uid)
|
2020-04-17 14:52:13 +02:00
|
|
|
.ok_or(ResponseError::index_not_found(&path.index_uid))?;
|
2020-01-02 17:10:49 +01:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-04-17 14:52:13 +02:00
|
|
|
let mut writer = data.db.update_write_txn()?;
|
|
|
|
let update_id = index.settings_update(&mut writer, settings)?;
|
|
|
|
|
|
|
|
writer.commit()?;
|
2020-01-02 17:10:49 +01:00
|
|
|
|
2020-04-10 18:39:52 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
|
2020-01-02 17:10:49 +01:00
|
|
|
}
|