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

94 lines
3.2 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
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};
2020-04-10 18:39:52 +02:00
use actix_web::{web, get, post, delete, HttpResponse};
use actix_web as aweb;
2019-10-31 15:00:36 +01:00
2020-04-10 18:39:52 +02:00
use crate::error::{ResponseError};
2019-10-31 15:00:36 +01:00
use crate::Data;
2020-04-10 18:39:52 +02:00
use crate::routes::{IndexUpdateResponse, IndexParam};
2019-10-31 15:00:36 +01:00
2020-04-10 18:39:52 +02:00
#[get("/indexes/{index_uid}/settings/synonyms")]
pub async fn get(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
2019-10-31 15:00:36 +01:00
2020-04-10 18:39:52 +02:00
let reader = data.db.main_read_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 15:00:36 +01:00
2020-04-10 18:39:52 +02:00
let synonyms_fst = index.main.synonyms_fst(&reader)
.map_err(|err| ResponseError::Internal(err.to_string()))?
.unwrap_or_default();
let synonyms_list = synonyms_fst.stream().into_strs()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 15:00:36 +01:00
2020-01-16 19:19:44 +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-04-10 18:39:52 +02:00
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())
.map_err(|err| ResponseError::Internal(err.to_string()))?;
if let Some(list) = alternative_list {
2020-04-10 18:39:52 +02:00
let list = list.stream().into_strs()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2020-01-16 19:19:44 +01:00
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
}
2020-04-10 18:39:52 +02:00
#[post("/indexes/{index_uid}/settings/synonyms")]
pub async fn update(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<BTreeMap<String, Vec<String>>>,
) -> aweb::Result<HttpResponse> {
let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
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-10 18:39:52 +02:00
let mut writer = data.db.update_write_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let update_id = index.settings_update(&mut writer, settings)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
writer.commit()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
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/synonyms")]
pub async fn delete(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> aweb::Result<HttpResponse> {
let index = data.db.open_index(&path.index_uid)
.ok_or(ResponseError::IndexNotFound(path.index_uid.clone()))?;
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-10 18:39:52 +02:00
let mut writer = data.db.update_write_txn()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let update_id = index.settings_update(&mut writer, settings)
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2020-04-10 18:39:52 +02:00
writer.commit()
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2020-04-10 18:39:52 +02:00
Ok(HttpResponse::Accepted().json(IndexUpdateResponse::with_id(update_id)))
}