2021-09-24 14:55:57 +02:00
|
|
|
use log::debug;
|
|
|
|
|
2021-10-13 20:56:28 +02:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_error::ResponseError;
|
2021-09-24 14:55:57 +02:00
|
|
|
use meilisearch_lib::index::{Settings, Unchecked};
|
|
|
|
use meilisearch_lib::index_controller::Update;
|
2021-09-28 22:22:59 +02:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-10-13 14:10:22 +02:00
|
|
|
use serde_json::json;
|
2021-09-24 14:55:57 +02:00
|
|
|
|
2021-10-13 14:10:22 +02:00
|
|
|
use crate::analytics::Analytics;
|
2021-09-28 22:22:59 +02:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2021-12-02 16:03:26 +01:00
|
|
|
use crate::task::SummarizedTaskView;
|
2021-09-24 14:55:57 +02:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! make_setting_route {
|
2021-10-13 14:56:54 +02:00
|
|
|
($route:literal, $type:ty, $attr:ident, $camelcase_attr:literal, $analytics_var:ident, $analytics:expr) => {
|
2021-09-24 14:55:57 +02:00
|
|
|
pub mod $attr {
|
2021-12-02 16:03:26 +01:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse, Resource};
|
2021-09-24 14:55:57 +02:00
|
|
|
use log::debug;
|
|
|
|
|
2021-09-28 22:08:03 +02:00
|
|
|
use meilisearch_lib::milli::update::Setting;
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_lib::{index::Settings, index_controller::Update, MeiliSearch};
|
2021-09-24 14:55:57 +02:00
|
|
|
|
2021-10-13 14:56:54 +02:00
|
|
|
use crate::analytics::Analytics;
|
2021-12-02 16:03:26 +01:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2022-03-04 20:12:44 +01:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2021-12-02 16:03:26 +01:00
|
|
|
use crate::task::SummarizedTaskView;
|
|
|
|
use meilisearch_error::ResponseError;
|
2021-09-24 14:55:57 +02:00
|
|
|
|
|
|
|
pub async fn delete(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = Settings {
|
|
|
|
$attr: Setting::Reset,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-12-15 14:52:33 +01:00
|
|
|
|
|
|
|
let allow_index_creation = meilisearch.filters().allow_index_creation;
|
2021-12-02 16:03:26 +01:00
|
|
|
let update = Update::Settings {
|
|
|
|
settings,
|
|
|
|
is_deletion: true,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(index_uid.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
body: actix_web::web::Json<Option<$type>>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-12-02 16:03:26 +01:00
|
|
|
$analytics_var: web::Data<dyn Analytics>,
|
2021-09-24 14:55:57 +02:00
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
2021-10-13 14:56:54 +02:00
|
|
|
let body = body.into_inner();
|
|
|
|
|
2021-10-13 20:56:28 +02:00
|
|
|
$analytics(&body, &req);
|
2021-10-13 14:56:54 +02:00
|
|
|
|
2021-09-24 14:55:57 +02:00
|
|
|
let settings = Settings {
|
2021-10-13 14:56:54 +02:00
|
|
|
$attr: match body {
|
2021-09-24 14:55:57 +02:00
|
|
|
Some(inner_body) => Setting::Set(inner_body),
|
2021-12-02 16:03:26 +01:00
|
|
|
None => Setting::Reset,
|
2021-09-24 14:55:57 +02:00
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2021-12-15 14:52:33 +01:00
|
|
|
let allow_index_creation = meilisearch.filters().allow_index_creation;
|
2021-12-02 16:03:26 +01:00
|
|
|
let update = Update::Settings {
|
|
|
|
settings,
|
|
|
|
is_deletion: false,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(index_uid.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SETTINGS_GET }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = meilisearch.settings(index_uid.into_inner()).await?;
|
|
|
|
debug!("returns: {:?}", settings);
|
|
|
|
let mut json = serde_json::json!(&settings);
|
|
|
|
let val = json[$camelcase_attr].take();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2021-09-24 14:55:57 +02:00
|
|
|
Ok(HttpResponse::Ok().json(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resources() -> Resource {
|
|
|
|
Resource::new($route)
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::get().to(SeqHandler(get)))
|
|
|
|
.route(web::post().to(SeqHandler(update)))
|
|
|
|
.route(web::delete().to(SeqHandler(delete)))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-10-13 14:56:54 +02:00
|
|
|
($route:literal, $type:ty, $attr:ident, $camelcase_attr:literal) => {
|
2021-10-13 20:56:28 +02:00
|
|
|
make_setting_route!($route, $type, $attr, $camelcase_attr, _analytics, |_, _| {});
|
2021-10-13 14:56:54 +02:00
|
|
|
};
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/filterable-attributes",
|
|
|
|
std::collections::BTreeSet<String>,
|
|
|
|
filterable_attributes,
|
2021-10-13 14:56:54 +02:00
|
|
|
"filterableAttributes",
|
|
|
|
analytics,
|
2021-10-13 20:56:28 +02:00
|
|
|
|setting: &Option<std::collections::BTreeSet<String>>, req: &HttpRequest| {
|
2021-10-13 14:56:54 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"FilterableAttributes Updated".to_string(),
|
|
|
|
json!({
|
2021-10-28 13:02:48 +02:00
|
|
|
"filterable_attributes": {
|
|
|
|
"total": setting.as_ref().map(|filter| filter.len()).unwrap_or(0),
|
|
|
|
"has_geo": setting.as_ref().map(|filter| filter.contains("_geo")).unwrap_or(false),
|
|
|
|
}
|
2021-10-13 14:56:54 +02:00
|
|
|
}),
|
2021-10-26 13:43:49 +02:00
|
|
|
Some(req),
|
2021-10-13 14:56:54 +02:00
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/sortable-attributes",
|
|
|
|
std::collections::BTreeSet<String>,
|
|
|
|
sortable_attributes,
|
2021-10-13 14:56:54 +02:00
|
|
|
"sortableAttributes",
|
|
|
|
analytics,
|
2021-10-13 20:56:28 +02:00
|
|
|
|setting: &Option<std::collections::BTreeSet<String>>, req: &HttpRequest| {
|
2021-10-13 14:56:54 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"SortableAttributes Updated".to_string(),
|
|
|
|
json!({
|
2021-10-28 13:02:48 +02:00
|
|
|
"sortable_attributes": {
|
|
|
|
"total": setting.as_ref().map(|sort| sort.len()).unwrap_or(0),
|
|
|
|
"has_geo": setting.as_ref().map(|sort| sort.contains("_geo")).unwrap_or(false),
|
|
|
|
},
|
2021-10-13 14:56:54 +02:00
|
|
|
}),
|
2021-10-26 13:43:49 +02:00
|
|
|
Some(req),
|
2021-10-13 14:56:54 +02:00
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/displayed-attributes",
|
|
|
|
Vec<String>,
|
|
|
|
displayed_attributes,
|
|
|
|
"displayedAttributes"
|
|
|
|
);
|
|
|
|
|
2022-03-17 11:59:35 +01:00
|
|
|
make_setting_route!(
|
|
|
|
"/typo",
|
|
|
|
meilisearch_lib::index::updates::TypoSettings,
|
|
|
|
typo,
|
|
|
|
"typo"
|
|
|
|
);
|
|
|
|
|
2021-09-24 14:55:57 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/searchable-attributes",
|
|
|
|
Vec<String>,
|
|
|
|
searchable_attributes,
|
2021-10-28 13:02:48 +02:00
|
|
|
"searchableAttributes",
|
|
|
|
analytics,
|
|
|
|
|setting: &Option<Vec<String>>, req: &HttpRequest| {
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"SearchableAttributes Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"searchable_attributes": {
|
2021-12-02 16:03:26 +01:00
|
|
|
"total": setting.as_ref().map(|searchable| searchable.len()).unwrap_or(0),
|
2021-10-28 13:02:48 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/stop-words",
|
|
|
|
std::collections::BTreeSet<String>,
|
|
|
|
stop_words,
|
|
|
|
"stopWords"
|
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/synonyms",
|
|
|
|
std::collections::BTreeMap<String, Vec<String>>,
|
|
|
|
synonyms,
|
|
|
|
"synonyms"
|
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/distinct-attribute",
|
|
|
|
String,
|
|
|
|
distinct_attribute,
|
|
|
|
"distinctAttribute"
|
|
|
|
);
|
|
|
|
|
2021-10-13 14:56:54 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/ranking-rules",
|
|
|
|
Vec<String>,
|
|
|
|
ranking_rules,
|
|
|
|
"rankingRules",
|
|
|
|
analytics,
|
2021-10-13 20:56:28 +02:00
|
|
|
|setting: &Option<Vec<String>>, req: &HttpRequest| {
|
2021-10-13 14:56:54 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
2021-10-27 14:27:29 +02:00
|
|
|
"RankingRules Updated".to_string(),
|
|
|
|
json!({
|
2021-10-28 13:25:26 +02:00
|
|
|
"ranking_rules": {
|
|
|
|
"sort_position": setting.as_ref().map(|sort| sort.iter().position(|s| s == "sort")),
|
|
|
|
}
|
2021-10-27 14:27:29 +02:00
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
2021-10-13 14:56:54 +02:00
|
|
|
}
|
|
|
|
);
|
2021-09-24 14:55:57 +02:00
|
|
|
|
|
|
|
macro_rules! generate_configure {
|
|
|
|
($($mod:ident),*) => {
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2022-03-04 20:12:44 +01:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2021-09-24 14:55:57 +02:00
|
|
|
cfg.service(
|
|
|
|
web::resource("")
|
2022-03-04 20:12:44 +01:00
|
|
|
.route(web::post().to(SeqHandler(update_all)))
|
|
|
|
.route(web::get().to(SeqHandler(get_all)))
|
|
|
|
.route(web::delete().to(SeqHandler(delete_all))))
|
2021-09-24 14:55:57 +02:00
|
|
|
$(.service($mod::resources()))*;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_configure!(
|
|
|
|
filterable_attributes,
|
|
|
|
sortable_attributes,
|
|
|
|
displayed_attributes,
|
|
|
|
searchable_attributes,
|
|
|
|
distinct_attribute,
|
|
|
|
stop_words,
|
|
|
|
synonyms,
|
2022-03-17 11:59:35 +01:00
|
|
|
ranking_rules,
|
|
|
|
typo
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
pub async fn update_all(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
body: web::Json<Settings<Unchecked>>,
|
2021-10-13 20:56:28 +02:00
|
|
|
req: HttpRequest,
|
2021-10-29 16:10:58 +02:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2021-09-24 14:55:57 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = body.into_inner();
|
|
|
|
|
2021-10-13 14:10:22 +02:00
|
|
|
analytics.publish(
|
|
|
|
"Settings Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"ranking_rules": {
|
2021-10-27 14:27:29 +02:00
|
|
|
"sort_position": settings.ranking_rules.as_ref().set().map(|sort| sort.iter().position(|s| s == "sort")),
|
2021-10-13 14:10:22 +02:00
|
|
|
},
|
2021-12-02 16:03:26 +01:00
|
|
|
"searchable_attributes": {
|
|
|
|
"total": settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()).unwrap_or(0),
|
|
|
|
},
|
2021-10-13 14:10:22 +02:00
|
|
|
"sortable_attributes": {
|
2021-10-28 12:34:39 +02:00
|
|
|
"total": settings.sortable_attributes.as_ref().set().map(|sort| sort.len()).unwrap_or(0),
|
2021-10-13 14:10:22 +02:00
|
|
|
"has_geo": settings.sortable_attributes.as_ref().set().map(|sort| sort.iter().any(|s| s == "_geo")).unwrap_or(false),
|
|
|
|
},
|
|
|
|
"filterable_attributes": {
|
2021-10-28 12:34:39 +02:00
|
|
|
"total": settings.filterable_attributes.as_ref().set().map(|filter| filter.len()).unwrap_or(0),
|
2021-10-13 14:10:22 +02:00
|
|
|
"has_geo": settings.filterable_attributes.as_ref().set().map(|filter| filter.iter().any(|s| s == "_geo")).unwrap_or(false),
|
|
|
|
},
|
|
|
|
}),
|
2021-10-13 20:56:28 +02:00
|
|
|
Some(&req),
|
2021-10-13 14:10:22 +02:00
|
|
|
);
|
|
|
|
|
2021-12-15 14:52:33 +01:00
|
|
|
let allow_index_creation = meilisearch.filters().allow_index_creation;
|
2021-12-02 16:03:26 +01:00
|
|
|
let update = Update::Settings {
|
|
|
|
settings,
|
|
|
|
is_deletion: false,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
|
|
|
let task: SummarizedTaskView = meilisearch
|
|
|
|
.register_update(index_uid.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_all(
|
2021-11-08 18:31:27 +01:00
|
|
|
data: GuardedData<ActionPolicy<{ actions::SETTINGS_GET }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = data.settings(index_uid.into_inner()).await?;
|
|
|
|
debug!("returns: {:?}", settings);
|
|
|
|
Ok(HttpResponse::Ok().json(settings))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_all(
|
2021-11-08 18:31:27 +01:00
|
|
|
data: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, MeiliSearch>,
|
2021-09-24 14:55:57 +02:00
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-12-02 16:03:26 +01:00
|
|
|
let settings = Settings::cleared().into_unchecked();
|
|
|
|
|
2021-12-15 14:52:33 +01:00
|
|
|
let allow_index_creation = data.filters().allow_index_creation;
|
2021-12-02 16:03:26 +01:00
|
|
|
let update = Update::Settings {
|
|
|
|
settings,
|
|
|
|
is_deletion: true,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
|
|
|
let task: SummarizedTaskView = data
|
|
|
|
.register_update(index_uid.into_inner(), update)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|