2021-06-24 21:50:46 +08:00
|
|
|
use actix_web::{web, HttpResponse};
|
2021-06-29 21:25:18 +08:00
|
|
|
use log::debug;
|
2021-01-05 07:21:42 +08:00
|
|
|
|
2021-06-24 22:25:52 +08:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2021-03-16 01:11:10 +08:00
|
|
|
use crate::index::Settings;
|
|
|
|
use crate::Data;
|
2021-05-31 22:03:39 +08:00
|
|
|
use crate::{error::ResponseError, index::Unchecked};
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! make_setting_route {
|
2021-06-23 21:25:56 +08:00
|
|
|
($route:literal, $type:ty, $attr:ident, $camelcase_attr:literal) => {
|
2021-01-05 07:21:42 +08:00
|
|
|
mod $attr {
|
2021-06-23 18:18:34 +08:00
|
|
|
use log::debug;
|
2021-06-24 21:50:46 +08:00
|
|
|
use actix_web::{web, HttpResponse, Resource};
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
use crate::data;
|
|
|
|
use crate::error::ResponseError;
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::Settings;
|
2021-06-24 21:50:46 +08:00
|
|
|
use crate::extractors::authentication::{GuardedData, policies::*};
|
2021-01-05 07:21:42 +08:00
|
|
|
|
2021-06-24 21:50:46 +08:00
|
|
|
async fn delete(
|
|
|
|
data: GuardedData<Private, data::Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::Settings;
|
2021-01-05 07:21:42 +08:00
|
|
|
let settings = Settings {
|
|
|
|
$attr: Some(None),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-06-15 22:15:27 +08:00
|
|
|
let update_status = data.update_settings(index_uid.into_inner(), settings, false).await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", update_status);
|
2021-06-15 22:15:27 +08:00
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 21:50:46 +08:00
|
|
|
async fn update(
|
|
|
|
data: GuardedData<Private, data::Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
body: actix_web::web::Json<Option<$type>>,
|
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = Settings {
|
|
|
|
$attr: Some(body.into_inner()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2021-06-15 22:15:27 +08:00
|
|
|
let update_status = data.update_settings(index_uid.into_inner(), settings, true).await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", update_status);
|
2021-06-15 22:15:27 +08:00
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 21:50:46 +08:00
|
|
|
async fn get(
|
|
|
|
data: GuardedData<Private, data::Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
2021-06-15 22:15:27 +08:00
|
|
|
let settings = data.settings(index_uid.into_inner()).await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", settings);
|
2021-06-23 21:25:56 +08:00
|
|
|
let mut json = serde_json::json!(&settings);
|
|
|
|
let val = json[$camelcase_attr].take();
|
|
|
|
Ok(HttpResponse::Ok().json(val))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
2021-06-24 21:50:46 +08:00
|
|
|
|
|
|
|
pub fn resources() -> Resource {
|
|
|
|
Resource::new($route)
|
|
|
|
.route(web::get().to(get))
|
|
|
|
.route(web::post().to(update))
|
|
|
|
.route(web::delete().to(delete))
|
|
|
|
}
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
make_setting_route!(
|
2021-06-04 05:47:16 +08:00
|
|
|
"/indexes/{index_uid}/settings/filterable-attributes",
|
2021-06-04 01:36:25 +08:00
|
|
|
std::collections::HashSet<String>,
|
2021-06-23 21:25:56 +08:00
|
|
|
filterable_attributes,
|
|
|
|
"filterableAttributes"
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/displayed-attributes",
|
|
|
|
Vec<String>,
|
2021-06-23 21:25:56 +08:00
|
|
|
displayed_attributes,
|
|
|
|
"displayedAttributes"
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/searchable-attributes",
|
|
|
|
Vec<String>,
|
2021-06-23 21:25:56 +08:00
|
|
|
searchable_attributes,
|
|
|
|
"searchableAttributes"
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
2021-04-06 21:41:03 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/stop-words",
|
|
|
|
std::collections::BTreeSet<String>,
|
2021-06-23 21:25:56 +08:00
|
|
|
stop_words,
|
|
|
|
"stopWords"
|
2021-04-06 21:41:03 +08:00
|
|
|
);
|
|
|
|
|
2021-06-03 20:19:56 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/synonyms",
|
|
|
|
std::collections::BTreeMap<String, Vec<String>>,
|
2021-06-23 21:25:56 +08:00
|
|
|
synonyms,
|
|
|
|
"synonyms"
|
2021-06-03 20:19:56 +08:00
|
|
|
);
|
|
|
|
|
2021-04-20 17:28:16 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/distinct-attribute",
|
|
|
|
String,
|
2021-06-23 21:25:56 +08:00
|
|
|
distinct_attribute,
|
|
|
|
"distinctAttribute"
|
2021-04-20 17:28:16 +08:00
|
|
|
);
|
2021-01-05 07:21:42 +08:00
|
|
|
|
2021-04-27 21:29:00 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/ranking-rules",
|
|
|
|
Vec<String>,
|
2021-06-23 21:25:56 +08:00
|
|
|
ranking_rules,
|
|
|
|
"rankingRules"
|
2021-04-27 21:29:00 +08:00
|
|
|
);
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
macro_rules! create_services {
|
|
|
|
($($mod:ident),*) => {
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg
|
2021-06-24 21:50:46 +08:00
|
|
|
.service(web::resource("/indexes/{index_uid}/settings")
|
|
|
|
.route(web::post().to(update_all))
|
|
|
|
.route(web::get().to(get_all))
|
|
|
|
.route(web::delete().to(delete_all)))
|
|
|
|
$(.service($mod::resources()))*;
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
create_services!(
|
2021-06-04 05:47:16 +08:00
|
|
|
filterable_attributes,
|
2021-01-05 07:21:42 +08:00
|
|
|
displayed_attributes,
|
2021-04-06 21:41:03 +08:00
|
|
|
searchable_attributes,
|
2021-04-20 17:28:16 +08:00
|
|
|
distinct_attribute,
|
2021-04-27 21:29:00 +08:00
|
|
|
stop_words,
|
2021-06-03 20:19:56 +08:00
|
|
|
synonyms,
|
2021-04-27 21:29:00 +08:00
|
|
|
ranking_rules
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
async fn update_all(
|
2021-06-24 21:50:46 +08:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: web::Path<String>,
|
2021-05-10 23:30:09 +08:00
|
|
|
body: web::Json<Settings<Unchecked>>,
|
2021-01-05 07:21:42 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-05-10 23:30:09 +08:00
|
|
|
let settings = body.into_inner().check();
|
2021-06-15 22:15:27 +08:00
|
|
|
let update_result = data
|
2021-05-10 23:30:09 +08:00
|
|
|
.update_settings(index_uid.into_inner(), settings, true)
|
2021-06-15 22:15:27 +08:00
|
|
|
.await?;
|
|
|
|
let json = serde_json::json!({ "updateId": update_result.id() });
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", json);
|
2021-06-15 22:15:27 +08:00
|
|
|
Ok(HttpResponse::Accepted().json(json))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_all(
|
2021-06-24 21:50:46 +08:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-15 22:15:27 +08:00
|
|
|
let settings = data.settings(index_uid.into_inner()).await?;
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", settings);
|
2021-06-15 22:15:27 +08:00
|
|
|
Ok(HttpResponse::Ok().json(settings))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_all(
|
2021-06-24 21:50:46 +08:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-01-05 07:21:42 +08:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = Settings::cleared();
|
2021-06-15 22:15:27 +08:00
|
|
|
let update_result = data
|
2021-03-16 01:11:10 +08:00
|
|
|
.update_settings(index_uid.into_inner(), settings, false)
|
2021-06-15 22:15:27 +08:00
|
|
|
.await?;
|
|
|
|
let json = serde_json::json!({ "updateId": update_result.id() });
|
2021-06-23 18:18:34 +08:00
|
|
|
debug!("returns: {:?}", json);
|
2021-06-15 22:15:27 +08:00
|
|
|
Ok(HttpResponse::Accepted().json(json))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|