MeiliSearch/meilisearch-http/src/routes/indexes/settings.rs

482 lines
16 KiB
Rust
Raw Normal View History

2022-09-27 16:33:37 +02:00
use actix_web::web::Data;
2021-10-13 20:56:28 +02:00
use actix_web::{web, HttpRequest, HttpResponse};
2022-10-12 03:21:25 +02:00
use index_scheduler::IndexScheduler;
2022-10-20 18:00:07 +02:00
use log::debug;
use meilisearch_types::error::ResponseError;
2022-10-13 15:02:59 +02:00
use meilisearch_types::settings::{settings, Settings, Unchecked};
2022-10-12 03:21:25 +02:00
use meilisearch_types::tasks::KindWithContent;
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;
2022-10-20 18:00:07 +02:00
use crate::extractors::authentication::policies::*;
use crate::extractors::authentication::GuardedData;
2022-10-13 15:02:59 +02:00
use crate::routes::SummarizedTaskView;
2021-09-24 14:55:57 +02:00
#[macro_export]
macro_rules! make_setting_route {
($route:literal, $update_verb:ident, $type:ty, $attr:ident, $camelcase_attr:literal, $analytics_var:ident, $analytics:expr) => {
2021-09-24 14:55:57 +02:00
pub mod $attr {
2022-09-27 16:33:37 +02:00
use actix_web::web::Data;
use actix_web::{web, HttpRequest, HttpResponse, Resource};
2022-10-12 03:21:25 +02:00
use index_scheduler::IndexScheduler;
2022-10-20 18:00:07 +02:00
use log::debug;
use meilisearch_types::error::ResponseError;
use meilisearch_types::milli::update::Setting;
2022-10-13 15:02:59 +02:00
use meilisearch_types::settings::{settings, Settings};
2022-10-12 03:21:25 +02:00
use meilisearch_types::tasks::KindWithContent;
use $crate::analytics::Analytics;
2022-10-20 18:00:07 +02:00
use $crate::extractors::authentication::policies::*;
use $crate::extractors::authentication::GuardedData;
use $crate::extractors::sequential_extractor::SeqHandler;
2022-10-13 15:02:59 +02:00
use $crate::routes::SummarizedTaskView;
2021-09-24 14:55:57 +02:00
pub async fn delete(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<
ActionPolicy<{ actions::SETTINGS_UPDATE }>,
Data<IndexScheduler>,
>,
2021-09-24 14:55:57 +02:00
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
2022-10-20 18:00:07 +02:00
let new_settings = Settings { $attr: Setting::Reset, ..Default::default() };
2022-09-27 16:33:37 +02:00
let allow_index_creation = index_scheduler.filters().allow_index_creation;
let task = KindWithContent::Settings {
index_uid: index_uid.into_inner(),
new_settings,
is_deletion: true,
allow_index_creation,
};
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
tokio::task::spawn_blocking(move || index_scheduler.register(task))
.await??
.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2021-09-24 14:55:57 +02:00
}
pub async fn update(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<
ActionPolicy<{ actions::SETTINGS_UPDATE }>,
Data<IndexScheduler>,
>,
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,
$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
let new_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),
None => Setting::Reset,
2021-09-24 14:55:57 +02:00
},
..Default::default()
};
2022-09-27 16:33:37 +02:00
let allow_index_creation = index_scheduler.filters().allow_index_creation;
let task = KindWithContent::Settings {
index_uid: index_uid.into_inner(),
new_settings,
is_deletion: false,
allow_index_creation,
};
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
tokio::task::spawn_blocking(move || index_scheduler.register(task))
.await??
.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2021-09-24 14:55:57 +02:00
}
pub async fn get(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<
ActionPolicy<{ actions::SETTINGS_GET }>,
Data<IndexScheduler>,
>,
2021-09-24 14:55:57 +02:00
index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> {
2022-09-27 16:33:37 +02:00
let index = index_scheduler.index(&index_uid)?;
let rtxn = index.read_txn()?;
let settings = settings(&index, &rtxn)?;
2021-09-24 14:55:57 +02:00
debug!("returns: {:?}", settings);
let mut json = serde_json::json!(&settings);
let val = json[$camelcase_attr].take();
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::$update_verb().to(SeqHandler(update)))
2022-03-04 20:12:44 +01:00
.route(web::delete().to(SeqHandler(delete)))
2021-09-24 14:55:57 +02:00
}
}
};
($route:literal, $update_verb:ident, $type:ty, $attr:ident, $camelcase_attr:literal) => {
make_setting_route!(
$route,
$update_verb,
$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",
put,
2021-09-24 14:55:57 +02:00
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!({
"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",
put,
2021-09-24 14:55:57 +02:00
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!({
"sortable_attributes": {
"total": setting.as_ref().map(|sort| sort.len()),
"has_geo": setting.as_ref().map(|sort| sort.contains("_geo")),
},
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",
put,
2021-09-24 14:55:57 +02:00
Vec<String>,
displayed_attributes,
"displayedAttributes"
);
2022-03-17 11:59:35 +01:00
make_setting_route!(
"/typo-tolerance",
patch,
meilisearch_types::settings::TypoSettings,
typo_tolerance,
"typoTolerance",
analytics,
|setting: &Option<meilisearch_types::settings::TypoSettings>, req: &HttpRequest| {
use serde_json::json;
analytics.publish(
"TypoTolerance Updated".to_string(),
json!({
"typo_tolerance": {
"enabled": setting.as_ref().map(|s| !matches!(s.enabled, Setting::Set(false))),
"disable_on_attributes": setting
.as_ref()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.disable_on_attributes.as_ref().set().map(|m| !m.is_empty())),
"disable_on_words": setting
.as_ref()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.disable_on_words.as_ref().set().map(|m| !m.is_empty())),
"min_word_size_for_one_typo": setting
.as_ref()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.min_word_size_for_typos
.as_ref()
.set()
.map(|s| s.one_typo.set()))
.flatten(),
"min_word_size_for_two_typos": setting
.as_ref()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.min_word_size_for_typos
.as_ref()
.set()
.map(|s| s.two_typos.set()))
.flatten(),
},
}),
Some(req),
);
}
2022-03-17 11:59:35 +01:00
);
2021-09-24 14:55:57 +02:00
make_setting_route!(
"/searchable-attributes",
put,
2021-09-24 14:55:57 +02:00
Vec<String>,
searchable_attributes,
"searchableAttributes",
analytics,
|setting: &Option<Vec<String>>, req: &HttpRequest| {
use serde_json::json;
analytics.publish(
"SearchableAttributes Updated".to_string(),
json!({
"searchable_attributes": {
"total": setting.as_ref().map(|searchable| searchable.len()),
},
}),
Some(req),
);
}
2021-09-24 14:55:57 +02:00
);
make_setting_route!(
"/stop-words",
put,
2021-09-24 14:55:57 +02:00
std::collections::BTreeSet<String>,
stop_words,
"stopWords"
);
make_setting_route!(
"/synonyms",
put,
2021-09-24 14:55:57 +02:00
std::collections::BTreeMap<String, Vec<String>>,
synonyms,
"synonyms"
);
2022-10-20 18:00:07 +02:00
make_setting_route!("/distinct-attribute", put, String, distinct_attribute, "distinctAttribute");
2021-09-24 14:55:57 +02:00
2021-10-13 14:56:54 +02:00
make_setting_route!(
"/ranking-rules",
put,
2021-10-13 14:56:54 +02:00
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!({
"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
make_setting_route!(
"/faceting",
patch,
meilisearch_types::settings::FacetingSettings,
faceting,
"faceting",
analytics,
|setting: &Option<meilisearch_types::settings::FacetingSettings>, req: &HttpRequest| {
use serde_json::json;
analytics.publish(
"Faceting Updated".to_string(),
json!({
"faceting": {
"max_values_per_facet": setting.as_ref().and_then(|s| s.max_values_per_facet.set()),
},
}),
Some(req),
);
}
);
2022-06-09 10:17:55 +02:00
make_setting_route!(
"/pagination",
patch,
meilisearch_types::settings::PaginationSettings,
2022-06-09 10:17:55 +02:00
pagination,
"pagination",
analytics,
|setting: &Option<meilisearch_types::settings::PaginationSettings>, req: &HttpRequest| {
2022-06-09 10:17:55 +02:00
use serde_json::json;
analytics.publish(
"Pagination Updated".to_string(),
json!({
"pagination": {
"max_total_hits": setting.as_ref().and_then(|s| s.max_total_hits.set()),
2022-06-09 10:17:55 +02:00
},
}),
Some(req),
);
}
);
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("")
.route(web::patch().to(SeqHandler(update_all)))
2022-03-04 20:12:44 +01:00
.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_tolerance,
pagination,
faceting
2021-09-24 14:55:57 +02:00
);
pub async fn update_all(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, Data<IndexScheduler>>,
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 new_settings = body.into_inner();
2021-09-24 14:55:57 +02:00
2021-10-13 14:10:22 +02:00
analytics.publish(
"Settings Updated".to_string(),
json!({
"ranking_rules": {
"sort_position": new_settings.ranking_rules.as_ref().set().map(|sort| sort.iter().position(|s| s == "sort")),
2021-10-13 14:10:22 +02:00
},
"searchable_attributes": {
"total": new_settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()),
},
2021-10-13 14:10:22 +02:00
"sortable_attributes": {
"total": new_settings.sortable_attributes.as_ref().set().map(|sort| sort.len()),
"has_geo": new_settings.sortable_attributes.as_ref().set().map(|sort| sort.iter().any(|s| s == "_geo")),
2021-10-13 14:10:22 +02:00
},
"filterable_attributes": {
"total": new_settings.filterable_attributes.as_ref().set().map(|filter| filter.len()),
"has_geo": new_settings.filterable_attributes.as_ref().set().map(|filter| filter.iter().any(|s| s == "_geo")),
2021-10-13 14:10:22 +02:00
},
"typo_tolerance": {
"enabled": new_settings.typo_tolerance
.as_ref()
.set()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.enabled.as_ref().set())
.copied(),
"disable_on_attributes": new_settings.typo_tolerance
.as_ref()
.set()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.disable_on_attributes.as_ref().set().map(|m| !m.is_empty())),
"disable_on_words": new_settings.typo_tolerance
.as_ref()
.set()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.disable_on_words.as_ref().set().map(|m| !m.is_empty())),
"min_word_size_for_one_typo": new_settings.typo_tolerance
.as_ref()
.set()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.min_word_size_for_typos
.as_ref()
.set()
.map(|s| s.one_typo.set()))
.flatten(),
"min_word_size_for_two_typos": new_settings.typo_tolerance
.as_ref()
.set()
2022-05-04 11:33:43 +02:00
.and_then(|s| s.min_word_size_for_typos
.as_ref()
.set()
.map(|s| s.two_typos.set()))
.flatten(),
},
"faceting": {
"max_values_per_facet": new_settings.faceting
.as_ref()
.set()
.and_then(|s| s.max_values_per_facet.as_ref().set()),
},
"pagination": {
"max_total_hits": new_settings.pagination
.as_ref()
.set()
.and_then(|s| s.max_total_hits.as_ref().set()),
},
2021-10-13 14:10:22 +02:00
}),
2021-10-13 20:56:28 +02:00
Some(&req),
2021-10-13 14:10:22 +02:00
);
2022-09-27 16:33:37 +02:00
let allow_index_creation = index_scheduler.filters().allow_index_creation;
let task = KindWithContent::Settings {
index_uid: index_uid.into_inner(),
new_settings,
is_deletion: false,
allow_index_creation,
};
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
2022-10-20 18:00:07 +02:00
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2021-09-24 14:55:57 +02:00
}
pub async fn get_all(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::SETTINGS_GET }>, Data<IndexScheduler>>,
2021-09-24 14:55:57 +02:00
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
2022-09-27 16:33:37 +02:00
let index = index_scheduler.index(&index_uid)?;
let rtxn = index.read_txn()?;
let new_settings = settings(&index, &rtxn)?;
debug!("returns: {:?}", new_settings);
Ok(HttpResponse::Ok().json(new_settings))
2021-09-24 14:55:57 +02:00
}
pub async fn delete_all(
2022-09-27 16:33:37 +02:00
index_scheduler: GuardedData<ActionPolicy<{ actions::SETTINGS_UPDATE }>, Data<IndexScheduler>>,
2021-09-24 14:55:57 +02:00
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
let new_settings = Settings::cleared().into_unchecked();
2022-09-27 16:33:37 +02:00
let allow_index_creation = index_scheduler.filters().allow_index_creation;
let task = KindWithContent::Settings {
index_uid: index_uid.into_inner(),
new_settings,
is_deletion: true,
allow_index_creation,
};
2022-10-13 15:02:59 +02:00
let task: SummarizedTaskView =
2022-10-20 18:00:07 +02:00
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??.into();
debug!("returns: {:?}", task);
Ok(HttpResponse::Accepted().json(task))
2021-09-24 14:55:57 +02:00
}