2022-10-11 17:42:43 +02:00
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2022-09-27 16:33:37 +02:00
|
|
|
use actix_web::web::Data;
|
2022-10-11 17:42:43 +02:00
|
|
|
use fst::IntoStreamer;
|
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};
|
2022-10-12 03:21:25 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2022-10-11 17:42:43 +02:00
|
|
|
use meilisearch_types::heed::RoTxn;
|
|
|
|
use meilisearch_types::milli::update::Setting;
|
|
|
|
use meilisearch_types::milli::{self, DEFAULT_VALUES_PER_FACET};
|
|
|
|
use meilisearch_types::settings::{
|
|
|
|
Checked, FacetingSettings, MinWordSizeTyposSetting, PaginationSettings, Settings, TypoSettings,
|
|
|
|
Unchecked,
|
|
|
|
};
|
2022-10-12 03:21:25 +02:00
|
|
|
use meilisearch_types::tasks::KindWithContent;
|
2022-10-11 17:42:43 +02:00
|
|
|
use meilisearch_types::Index;
|
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};
|
2022-10-11 17:42:43 +02:00
|
|
|
use crate::search::DEFAULT_PAGINATION_MAX_TOTAL_HITS;
|
2021-09-24 14:55:57 +02:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! make_setting_route {
|
2022-06-02 11:53:16 +02:00
|
|
|
($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;
|
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;
|
|
|
|
|
2022-10-12 03:21:25 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2022-10-11 17:42:43 +02:00
|
|
|
use meilisearch_types::milli::update::Setting;
|
|
|
|
use meilisearch_types::settings::Settings;
|
2022-10-12 03:21:25 +02:00
|
|
|
use meilisearch_types::tasks::KindWithContent;
|
2021-09-24 14:55:57 +02:00
|
|
|
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2022-04-12 15:22:47 +02:00
|
|
|
use $crate::analytics::Analytics;
|
|
|
|
use $crate::extractors::authentication::{policies::*, GuardedData};
|
|
|
|
use $crate::extractors::sequential_extractor::SeqHandler;
|
2022-10-11 17:42:43 +02:00
|
|
|
use $crate::routes::indexes::settings::settings;
|
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-09-22 12:14:51 +02:00
|
|
|
let new_settings = Settings {
|
2021-09-24 14:55:57 +02:00
|
|
|
$attr: Setting::Reset,
|
|
|
|
..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-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::Settings {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
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-09-27 16:33:37 +02:00
|
|
|
let task =
|
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
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>,
|
|
|
|
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
|
|
|
|
2022-09-22 12:14:51 +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),
|
2021-12-02 16:03:26 +01:00
|
|
|
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;
|
2022-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::Settings {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
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-09-27 16:33:37 +02:00
|
|
|
let task =
|
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-06-02 11:53:16 +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",
|
2022-06-02 11:53:16 +02:00
|
|
|
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!({
|
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>,
|
|
|
|
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>,
|
|
|
|
displayed_attributes,
|
|
|
|
"displayedAttributes"
|
|
|
|
);
|
|
|
|
|
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,
|
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": {
|
2022-04-29 16:38:21 +02: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>,
|
|
|
|
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()),
|
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>,
|
|
|
|
stop_words,
|
|
|
|
"stopWords"
|
|
|
|
);
|
|
|
|
|
|
|
|
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>>,
|
|
|
|
synonyms,
|
|
|
|
"synonyms"
|
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/distinct-attribute",
|
2022-06-02 11:53:16 +02:00
|
|
|
put,
|
2021-09-24 14:55:57 +02:00
|
|
|
String,
|
|
|
|
distinct_attribute,
|
|
|
|
"distinctAttribute"
|
|
|
|
);
|
|
|
|
|
2021-10-13 14:56:54 +02:00
|
|
|
make_setting_route!(
|
|
|
|
"/ranking-rules",
|
2022-06-02 11:53:16 +02:00
|
|
|
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!({
|
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
|
|
|
|
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,
|
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,
|
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>,
|
|
|
|
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> {
|
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": {
|
2022-09-22 12:14:51 +02:00
|
|
|
"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
|
|
|
},
|
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()),
|
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-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
|
|
|
},
|
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-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::Settings {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
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-09-27 16:33:37 +02:00
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
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-09-22 12:14:51 +02:00
|
|
|
let task = KindWithContent::Settings {
|
|
|
|
index_uid: index_uid.into_inner(),
|
|
|
|
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-09-27 16:33:37 +02:00
|
|
|
let task = tokio::task::spawn_blocking(move || index_scheduler.register(task)).await??;
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
debug!("returns: {:?}", task);
|
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2021-09-24 14:55:57 +02:00
|
|
|
}
|
2022-10-11 17:42:43 +02:00
|
|
|
|
|
|
|
pub fn settings(index: &Index, rtxn: &RoTxn) -> Result<Settings<Checked>, milli::Error> {
|
|
|
|
let displayed_attributes = index
|
|
|
|
.displayed_fields(rtxn)?
|
|
|
|
.map(|fields| fields.into_iter().map(String::from).collect());
|
|
|
|
|
|
|
|
let searchable_attributes = index
|
|
|
|
.user_defined_searchable_fields(rtxn)?
|
|
|
|
.map(|fields| fields.into_iter().map(String::from).collect());
|
|
|
|
|
|
|
|
let filterable_attributes = index.filterable_fields(rtxn)?.into_iter().collect();
|
|
|
|
|
|
|
|
let sortable_attributes = index.sortable_fields(rtxn)?.into_iter().collect();
|
|
|
|
|
|
|
|
let criteria = index
|
|
|
|
.criteria(rtxn)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|c| c.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let stop_words = index
|
|
|
|
.stop_words(rtxn)?
|
|
|
|
.map(|stop_words| -> Result<BTreeSet<_>, milli::Error> {
|
|
|
|
Ok(stop_words.stream().into_strs()?.into_iter().collect())
|
|
|
|
})
|
|
|
|
.transpose()?
|
|
|
|
.unwrap_or_default();
|
|
|
|
let distinct_field = index.distinct_field(rtxn)?.map(String::from);
|
|
|
|
|
|
|
|
// in milli each word in the synonyms map were split on their separator. Since we lost
|
|
|
|
// this information we are going to put space between words.
|
|
|
|
let synonyms = index
|
|
|
|
.synonyms(rtxn)?
|
|
|
|
.iter()
|
|
|
|
.map(|(key, values)| {
|
|
|
|
(
|
|
|
|
key.join(" "),
|
|
|
|
values.iter().map(|value| value.join(" ")).collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let min_typo_word_len = MinWordSizeTyposSetting {
|
|
|
|
one_typo: Setting::Set(index.min_word_len_one_typo(rtxn)?),
|
|
|
|
two_typos: Setting::Set(index.min_word_len_two_typos(rtxn)?),
|
|
|
|
};
|
|
|
|
|
|
|
|
let disabled_words = match index.exact_words(rtxn)? {
|
|
|
|
Some(fst) => fst.into_stream().into_strs()?.into_iter().collect(),
|
|
|
|
None => BTreeSet::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let disabled_attributes = index
|
|
|
|
.exact_attributes(rtxn)?
|
|
|
|
.into_iter()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let typo_tolerance = TypoSettings {
|
|
|
|
enabled: Setting::Set(index.authorize_typos(rtxn)?),
|
|
|
|
min_word_size_for_typos: Setting::Set(min_typo_word_len),
|
|
|
|
disable_on_words: Setting::Set(disabled_words),
|
|
|
|
disable_on_attributes: Setting::Set(disabled_attributes),
|
|
|
|
};
|
|
|
|
|
|
|
|
let faceting = FacetingSettings {
|
|
|
|
max_values_per_facet: Setting::Set(
|
|
|
|
index
|
|
|
|
.max_values_per_facet(rtxn)?
|
|
|
|
.unwrap_or(DEFAULT_VALUES_PER_FACET),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
let pagination = PaginationSettings {
|
|
|
|
max_total_hits: Setting::Set(
|
|
|
|
index
|
|
|
|
.pagination_max_total_hits(rtxn)?
|
|
|
|
.unwrap_or(DEFAULT_PAGINATION_MAX_TOTAL_HITS),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Settings {
|
|
|
|
displayed_attributes: match displayed_attributes {
|
|
|
|
Some(attrs) => Setting::Set(attrs),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
searchable_attributes: match searchable_attributes {
|
|
|
|
Some(attrs) => Setting::Set(attrs),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
filterable_attributes: Setting::Set(filterable_attributes),
|
|
|
|
sortable_attributes: Setting::Set(sortable_attributes),
|
|
|
|
ranking_rules: Setting::Set(criteria),
|
|
|
|
stop_words: Setting::Set(stop_words),
|
|
|
|
distinct_attribute: match distinct_field {
|
|
|
|
Some(field) => Setting::Set(field),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
synonyms: Setting::Set(synonyms),
|
|
|
|
typo_tolerance: Setting::Set(typo_tolerance),
|
|
|
|
faceting: Setting::Set(faceting),
|
|
|
|
pagination: Setting::Set(pagination),
|
|
|
|
_kind: PhantomData,
|
|
|
|
})
|
|
|
|
}
|