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;
|
2023-01-12 13:55:53 +01:00
|
|
|
use meilisearch_types::error::{DeserrJsonError, ResponseError};
|
2022-10-22 15:18:32 +02:00
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2023-01-11 12:33:56 +01:00
|
|
|
use meilisearch_types::settings::{settings, RankingRuleView, 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-12-14 13:00:43 +01:00
|
|
|
use crate::extractors::json::ValidatedJson;
|
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 {
|
2023-01-11 14:31:34 +01:00
|
|
|
($route:literal, $update_verb:ident, $type:ty, $err_ty: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;
|
2021-12-02 16:03:26 +01:00
|
|
|
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;
|
2022-10-22 15:18:32 +02:00
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2022-10-11 17:42:43 +02:00
|
|
|
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;
|
2022-04-12 15:22:47 +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-04-12 15:22:47 +02:00
|
|
|
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-12-14 13:00:43 +01:00
|
|
|
let new_settings = Settings { $attr: Setting::Reset.into(), ..Default::default() };
|
2021-12-15 14:52:33 +01:00
|
|
|
|
2022-09-27 16:33:37 +02:00
|
|
|
let allow_index_creation = index_scheduler.filters().allow_index_creation;
|
2022-10-22 15:18:32 +02:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?.into_inner();
|
2022-10-21 18:03:10 +02:00
|
|
|
let task = KindWithContent::SettingsUpdate {
|
2022-10-22 15:18:32 +02:00
|
|
|
index_uid,
|
2022-10-22 16:35:42 +02:00
|
|
|
new_settings: Box::new(new_settings),
|
2021-12-02 16:03:26 +01:00
|
|
|
is_deletion: true,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
2022-10-13 15:02:59 +02:00
|
|
|
let task: SummarizedTaskView =
|
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task))
|
|
|
|
.await??
|
|
|
|
.into();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
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>,
|
2023-01-11 14:31:34 +01:00
|
|
|
body: $crate::routes::indexes::ValidatedJson<Option<$type>, $err_ty>,
|
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
|
|
|
|
2022-09-22 12:14:51 +02:00
|
|
|
let new_settings = Settings {
|
2021-10-13 14:56:54 +02:00
|
|
|
$attr: match body {
|
2022-12-14 13:00:43 +01:00
|
|
|
Some(inner_body) => Setting::Set(inner_body).into(),
|
|
|
|
None => Setting::Reset.into(),
|
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;
|
2022-10-22 15:18:32 +02:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?.into_inner();
|
2022-10-21 18:03:10 +02:00
|
|
|
let task = KindWithContent::SettingsUpdate {
|
2022-10-22 15:18:32 +02:00
|
|
|
index_uid,
|
2022-10-22 16:35:42 +02:00
|
|
|
new_settings: Box::new(new_settings),
|
2021-12-02 16:03:26 +01:00
|
|
|
is_deletion: false,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
2022-10-13 15:02:59 +02:00
|
|
|
let task: SummarizedTaskView =
|
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task))
|
|
|
|
.await??
|
|
|
|
.into();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
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)?;
|
2022-10-04 11:06:48 +02:00
|
|
|
let rtxn = index.read_txn()?;
|
2022-10-11 17:42:43 +02:00
|
|
|
let settings = settings(&index, &rtxn)?;
|
2022-09-22 12:14:51 +02:00
|
|
|
|
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-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)))
|
2022-06-02 11:53:16 +02:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/filterable-attributes",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
std::collections::BTreeSet<String>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsFilterableAttributes,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
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",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
std::collections::BTreeSet<String>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsSortableAttributes,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
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": {
|
2022-04-29 16:38:21 +02:00
|
|
|
"total": setting.as_ref().map(|sort| sort.len()),
|
|
|
|
"has_geo": setting.as_ref().map(|sort| sort.contains("_geo")),
|
2021-10-28 13:02:48 +02:00
|
|
|
},
|
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",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
Vec<String>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsDisplayedAttributes,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
displayed_attributes,
|
2022-11-28 16:27:41 +01:00
|
|
|
"displayedAttributes",
|
|
|
|
analytics,
|
|
|
|
|displayed: &Option<Vec<String>>, req: &HttpRequest| {
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"DisplayedAttributes Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"displayed_attributes": {
|
|
|
|
"total": displayed.as_ref().map(|displayed| displayed.len()),
|
|
|
|
"with_wildcard": displayed.as_ref().map(|displayed| displayed.iter().any(|displayed| displayed == "*")),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
2022-03-17 11:59:35 +01:00
|
|
|
make_setting_route!(
|
2022-04-14 10:42:06 +02:00
|
|
|
"/typo-tolerance",
|
2022-06-02 11:53:16 +02:00
|
|
|
patch,
|
2022-10-11 17:42:43 +02:00
|
|
|
meilisearch_types::settings::TypoSettings,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsTypoTolerance,
|
|
|
|
>,
|
2022-04-14 10:42:06 +02:00
|
|
|
typo_tolerance,
|
2022-04-26 14:59:48 +02:00
|
|
|
"typoTolerance",
|
|
|
|
analytics,
|
2022-10-11 17:42:43 +02:00
|
|
|
|setting: &Option<meilisearch_types::settings::TypoSettings>, req: &HttpRequest| {
|
2022-04-26 14:59:48 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
2022-04-27 14:49:21 +02:00
|
|
|
"TypoTolerance Updated".to_string(),
|
2022-04-26 14:59:48 +02:00
|
|
|
json!({
|
|
|
|
"typo_tolerance": {
|
2023-01-11 12:33:56 +01:00
|
|
|
"enabled": setting.as_ref().map(|s| !matches!(s.enabled, Setting::Set(false))),
|
2022-04-26 14:59:48 +02:00
|
|
|
"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())),
|
2022-04-26 14:59:48 +02:00
|
|
|
"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())),
|
2022-04-26 14:59:48 +02:00
|
|
|
"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
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
|
|
|
.map(|s| s.one_typo.set()))
|
2022-04-29 16:38:21 +02:00
|
|
|
.flatten(),
|
2022-04-26 14:59:48 +02:00
|
|
|
"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
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
|
|
|
.map(|s| s.two_typos.set()))
|
2022-04-29 16:38:21 +02:00
|
|
|
.flatten(),
|
2022-04-26 14:59:48 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2022-03-17 11:59:35 +01:00
|
|
|
);
|
|
|
|
|
2021-09-24 14:55:57 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/searchable-attributes",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
Vec<String>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsSearchableAttributes,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
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": {
|
2022-04-29 16:38:21 +02:00
|
|
|
"total": setting.as_ref().map(|searchable| searchable.len()),
|
2022-11-28 16:27:41 +01:00
|
|
|
"with_wildcard": setting.as_ref().map(|searchable| searchable.iter().any(|searchable| searchable == "*")),
|
2021-10-28 13:02:48 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/stop-words",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
std::collections::BTreeSet<String>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsStopWords,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
stop_words,
|
2022-11-28 16:27:41 +01:00
|
|
|
"stopWords",
|
|
|
|
analytics,
|
|
|
|
|stop_words: &Option<std::collections::BTreeSet<String>>, req: &HttpRequest| {
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"StopWords Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"stop_words": {
|
|
|
|
"total": stop_words.as_ref().map(|stop_words| stop_words.len()),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/synonyms",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
std::collections::BTreeMap<String, Vec<String>>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsSynonyms,
|
|
|
|
>,
|
2021-09-24 14:55:57 +02:00
|
|
|
synonyms,
|
2022-11-28 16:27:41 +01:00
|
|
|
"synonyms",
|
|
|
|
analytics,
|
|
|
|
|synonyms: &Option<std::collections::BTreeMap<String, Vec<String>>>, req: &HttpRequest| {
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"Synonyms Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"synonyms": {
|
|
|
|
"total": synonyms.as_ref().map(|synonyms| synonyms.len()),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
2021-09-24 14:55:57 +02:00
|
|
|
);
|
|
|
|
|
2022-11-28 16:27:41 +01:00
|
|
|
make_setting_route!(
|
|
|
|
"/distinct-attribute",
|
|
|
|
put,
|
|
|
|
String,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsDistinctAttribute,
|
|
|
|
>,
|
2022-11-28 16:27:41 +01:00
|
|
|
distinct_attribute,
|
|
|
|
"distinctAttribute",
|
|
|
|
analytics,
|
|
|
|
|distinct: &Option<String>, req: &HttpRequest| {
|
|
|
|
use serde_json::json;
|
|
|
|
analytics.publish(
|
|
|
|
"DistinctAttribute Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"distinct_attribute": {
|
|
|
|
"set": distinct.is_some(),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2021-09-24 14:55:57 +02:00
|
|
|
|
2021-10-13 14:56:54 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/ranking-rules",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2023-01-11 12:33:56 +01:00
|
|
|
Vec<meilisearch_types::settings::RankingRuleView>,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsRankingRules,
|
|
|
|
>,
|
2021-10-13 14:56:54 +02:00
|
|
|
ranking_rules,
|
|
|
|
"rankingRules",
|
|
|
|
analytics,
|
2023-01-11 12:33:56 +01:00
|
|
|
|setting: &Option<Vec<meilisearch_types::settings::RankingRuleView>>, 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": {
|
2023-01-11 12:33:56 +01:00
|
|
|
"words_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Words))),
|
|
|
|
"typo_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Typo))),
|
|
|
|
"proximity_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Proximity))),
|
|
|
|
"attribute_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Attribute))),
|
|
|
|
"sort_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Sort))),
|
|
|
|
"exactness_position": setting.as_ref().map(|rr| rr.iter().position(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Exactness))),
|
|
|
|
"values": setting.as_ref().map(|rr| rr.iter().filter(|s| matches!(s, meilisearch_types::settings::RankingRuleView::Asc(_) | meilisearch_types::settings::RankingRuleView::Desc(_)) ).map(|x| x.to_string()).collect::<Vec<_>>().join(", ")),
|
2021-10-28 13:25:26 +02:00
|
|
|
}
|
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
|
|
|
|
2022-06-08 17:08:10 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/faceting",
|
|
|
|
patch,
|
2022-10-11 17:42:43 +02:00
|
|
|
meilisearch_types::settings::FacetingSettings,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsFaceting,
|
|
|
|
>,
|
2022-06-08 17:08:10 +02:00
|
|
|
faceting,
|
|
|
|
"faceting",
|
|
|
|
analytics,
|
2022-10-11 17:42:43 +02:00
|
|
|
|setting: &Option<meilisearch_types::settings::FacetingSettings>, req: &HttpRequest| {
|
2022-06-08 17:08:10 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
analytics.publish(
|
|
|
|
"Faceting Updated".to_string(),
|
|
|
|
json!({
|
|
|
|
"faceting": {
|
2022-06-08 18:03:56 +02:00
|
|
|
"max_values_per_facet": setting.as_ref().and_then(|s| s.max_values_per_facet.set()),
|
2022-06-08 17:08:10 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
Some(req),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-06-09 10:17:55 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/pagination",
|
|
|
|
patch,
|
2022-10-11 17:42:43 +02:00
|
|
|
meilisearch_types::settings::PaginationSettings,
|
2023-01-12 13:55:53 +01:00
|
|
|
meilisearch_types::error::DeserrJsonError<
|
2023-01-11 14:31:34 +01:00
|
|
|
meilisearch_types::error::deserr_codes::InvalidSettingsPagination,
|
|
|
|
>,
|
2022-06-09 10:17:55 +02:00
|
|
|
pagination,
|
|
|
|
"pagination",
|
|
|
|
analytics,
|
2022-10-11 17:42:43 +02:00
|
|
|
|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": {
|
2022-06-22 17:24:25 +02:00
|
|
|
"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("")
|
2022-06-02 11:49:46 +02:00
|
|
|
.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,
|
2022-06-15 15:27:06 +02:00
|
|
|
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>,
|
2023-01-12 13:55:53 +01:00
|
|
|
body: ValidatedJson<Settings<Unchecked>, DeserrJsonError>,
|
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> {
|
2022-09-22 12:14:51 +02:00
|
|
|
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": {
|
2023-01-11 12:33:56 +01:00
|
|
|
"words_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Words))),
|
|
|
|
"typo_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Typo))),
|
|
|
|
"proximity_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Proximity))),
|
|
|
|
"attribute_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Attribute))),
|
|
|
|
"sort_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Sort))),
|
|
|
|
"exactness_position": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().position(|s| matches!(s, RankingRuleView::Exactness))),
|
|
|
|
"values": new_settings.ranking_rules.as_ref().set().map(|rr| rr.iter().filter(|s| !matches!(s, RankingRuleView::Asc(_) | RankingRuleView::Desc(_)) ).map(|x| x.to_string()).collect::<Vec<_>>().join(", ")),
|
2021-10-13 14:10:22 +02:00
|
|
|
},
|
2021-12-02 16:03:26 +01:00
|
|
|
"searchable_attributes": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"total": new_settings.searchable_attributes.as_ref().set().map(|searchable| searchable.len()),
|
2022-11-28 16:27:41 +01:00
|
|
|
"with_wildcard": new_settings.searchable_attributes.as_ref().set().map(|searchable| searchable.iter().any(|searchable| searchable == "*")),
|
|
|
|
},
|
|
|
|
"displayed_attributes": {
|
|
|
|
"total": new_settings.displayed_attributes.as_ref().set().map(|displayed| displayed.len()),
|
|
|
|
"with_wildcard": new_settings.displayed_attributes.as_ref().set().map(|displayed| displayed.iter().any(|displayed| displayed == "*")),
|
2021-12-02 16:03:26 +01:00
|
|
|
},
|
2021-10-13 14:10:22 +02:00
|
|
|
"sortable_attributes": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"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": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"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
|
|
|
},
|
2022-11-28 16:27:41 +01:00
|
|
|
"distinct_attribute": {
|
|
|
|
"set": new_settings.distinct_attribute.as_ref().set().is_some()
|
|
|
|
},
|
2022-04-26 14:59:48 +02:00
|
|
|
"typo_tolerance": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"enabled": new_settings.typo_tolerance
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
2022-05-04 11:33:43 +02:00
|
|
|
.and_then(|s| s.enabled.as_ref().set())
|
2022-04-29 16:38:21 +02:00
|
|
|
.copied(),
|
2022-09-22 12:14:51 +02:00
|
|
|
"disable_on_attributes": new_settings.typo_tolerance
|
2022-04-26 14:59:48 +02:00
|
|
|
.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())),
|
2022-09-22 12:14:51 +02:00
|
|
|
"disable_on_words": new_settings.typo_tolerance
|
2022-04-26 14:59:48 +02:00
|
|
|
.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())),
|
2022-09-22 12:14:51 +02:00
|
|
|
"min_word_size_for_one_typo": new_settings.typo_tolerance
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
2022-05-04 11:33:43 +02:00
|
|
|
.and_then(|s| s.min_word_size_for_typos
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
|
|
|
.map(|s| s.one_typo.set()))
|
2022-04-29 16:38:21 +02:00
|
|
|
.flatten(),
|
2022-09-22 12:14:51 +02:00
|
|
|
"min_word_size_for_two_typos": new_settings.typo_tolerance
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
2022-05-04 11:33:43 +02:00
|
|
|
.and_then(|s| s.min_word_size_for_typos
|
2022-04-26 14:59:48 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
|
|
|
.map(|s| s.two_typos.set()))
|
2022-04-29 16:38:21 +02:00
|
|
|
.flatten(),
|
2022-04-26 14:59:48 +02:00
|
|
|
},
|
2022-06-15 15:27:06 +02:00
|
|
|
"faceting": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"max_values_per_facet": new_settings.faceting
|
2022-06-15 15:27:06 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
|
|
|
.and_then(|s| s.max_values_per_facet.as_ref().set()),
|
|
|
|
},
|
|
|
|
"pagination": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"max_total_hits": new_settings.pagination
|
2022-06-15 15:27:06 +02:00
|
|
|
.as_ref()
|
|
|
|
.set()
|
2022-06-22 17:24:25 +02:00
|
|
|
.and_then(|s| s.max_total_hits.as_ref().set()),
|
2022-06-15 15:27:06 +02:00
|
|
|
},
|
2022-11-28 16:27:41 +01:00
|
|
|
"stop_words": {
|
|
|
|
"total": new_settings.stop_words.as_ref().set().map(|stop_words| stop_words.len()),
|
|
|
|
},
|
|
|
|
"synonyms": {
|
|
|
|
"total": new_settings.synonyms.as_ref().set().map(|synonyms| synonyms.len()),
|
|
|
|
},
|
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;
|
2022-10-22 15:18:32 +02:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?.into_inner();
|
2022-10-21 18:03:10 +02:00
|
|
|
let task = KindWithContent::SettingsUpdate {
|
2022-10-22 15:18:32 +02:00
|
|
|
index_uid,
|
2022-10-22 16:35:42 +02:00
|
|
|
new_settings: Box::new(new_settings),
|
2021-12-02 16:03:26 +01:00
|
|
|
is_deletion: false,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
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();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
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)?;
|
2022-10-04 11:06:48 +02:00
|
|
|
let rtxn = index.read_txn()?;
|
2022-10-11 17:42:43 +02:00
|
|
|
let new_settings = settings(&index, &rtxn)?;
|
2022-09-22 12:14:51 +02:00
|
|
|
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> {
|
2022-09-22 12:14:51 +02:00
|
|
|
let new_settings = Settings::cleared().into_unchecked();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-09-27 16:33:37 +02:00
|
|
|
let allow_index_creation = index_scheduler.filters().allow_index_creation;
|
2022-10-22 15:18:32 +02:00
|
|
|
let index_uid = IndexUid::try_from(index_uid.into_inner())?.into_inner();
|
2022-10-21 18:03:10 +02:00
|
|
|
let task = KindWithContent::SettingsUpdate {
|
2022-10-22 16:35:42 +02:00
|
|
|
index_uid,
|
|
|
|
new_settings: Box::new(new_settings),
|
2021-12-02 16:03:26 +01:00
|
|
|
is_deletion: true,
|
2021-12-15 14:52:33 +01:00
|
|
|
allow_index_creation,
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
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();
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|