fix some uses cases on settings

This commit is contained in:
qdequele 2020-01-28 17:45:29 +01:00
parent 5c49f08bb2
commit 50a9825a0f
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
5 changed files with 92 additions and 72 deletions

View File

@ -2,7 +2,7 @@ use std::sync::Mutex;
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr; use std::str::FromStr;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| { static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| {
@ -14,36 +14,30 @@ static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| {
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Settings { pub struct Settings {
pub ranking_rules: Option<Vec<String>>, #[serde(default, deserialize_with = "deserialize_some")]
pub ranking_distinct: Option<String>, pub ranking_rules: Option<Option<Vec<String>>>,
pub attribute_identifier: Option<String>, #[serde(default, deserialize_with = "deserialize_some")]
pub attributes_searchable: Option<Vec<String>>, pub ranking_distinct: Option<Option<String>>,
pub attributes_displayed: Option<HashSet<String>>, #[serde(default, deserialize_with = "deserialize_some")]
pub stop_words: Option<BTreeSet<String>>, pub attribute_identifier: Option<Option<String>>,
pub synonyms: Option<BTreeMap<String, Vec<String>>>, #[serde(default, deserialize_with = "deserialize_some")]
pub index_new_fields: Option<bool>, pub attributes_searchable: Option<Option<Vec<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub attributes_displayed: Option<Option<HashSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub stop_words: Option<Option<BTreeSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub index_new_fields: Option<Option<bool>>,
} }
impl Settings { // Any value that is present is considered Some value, including null.
pub fn into_cleared(self) -> SettingsUpdate { fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
let settings = self.clone(); where T: Deserialize<'de>,
D: Deserializer<'de>
let ranking_rules = match settings.ranking_rules { {
Some(rules) => Some(RankingRule::from_vec(rules)), Deserialize::deserialize(deserializer).map(Some)
None => None
};
SettingsUpdate {
ranking_rules: UpdateState::convert_with_default(ranking_rules, UpdateState::Clear),
ranking_distinct: UpdateState::convert_with_default(settings.ranking_distinct, UpdateState::Clear),
attribute_identifier: UpdateState::convert_with_default(settings.attribute_identifier, UpdateState::Clear),
attributes_searchable: UpdateState::convert_with_default(settings.attributes_searchable, UpdateState::Clear),
attributes_displayed: UpdateState::convert_with_default(settings.attributes_displayed, UpdateState::Clear),
stop_words: UpdateState::convert_with_default(settings.stop_words, UpdateState::Clear),
synonyms: UpdateState::convert_with_default(settings.synonyms, UpdateState::Clear),
index_new_fields: UpdateState::convert_with_default(settings.index_new_fields, UpdateState::Clear),
}
}
} }
impl Into<SettingsUpdate> for Settings { impl Into<SettingsUpdate> for Settings {
@ -51,12 +45,13 @@ impl Into<SettingsUpdate> for Settings {
let settings = self.clone(); let settings = self.clone();
let ranking_rules = match settings.ranking_rules { let ranking_rules = match settings.ranking_rules {
Some(rules) => Some(RankingRule::from_vec(rules)), Some(Some(rules)) => UpdateState::Update(RankingRule::from_vec(rules)),
None => None, Some(None) => UpdateState::Clear,
None => UpdateState::Nothing,
}; };
SettingsUpdate { SettingsUpdate {
ranking_rules: ranking_rules.into(), ranking_rules: ranking_rules,
ranking_distinct: settings.ranking_distinct.into(), ranking_distinct: settings.ranking_distinct.into(),
attribute_identifier: settings.attribute_identifier.into(), attribute_identifier: settings.attribute_identifier.into(),
attributes_searchable: settings.attributes_searchable.into(), attributes_searchable: settings.attributes_searchable.into(),
@ -77,10 +72,11 @@ pub enum UpdateState<T> {
Nothing, Nothing,
} }
impl <T> From<Option<T>> for UpdateState<T> { impl <T> From<Option<Option<T>>> for UpdateState<T> {
fn from(opt: Option<T>) -> UpdateState<T> { fn from(opt: Option<Option<T>>) -> UpdateState<T> {
match opt { match opt {
Some(t) => UpdateState::Update(t), Some(Some(t)) => UpdateState::Update(t),
Some(None) => UpdateState::Clear,
None => UpdateState::Nothing, None => UpdateState::Nothing,
} }
} }
@ -93,13 +89,6 @@ impl<T> UpdateState<T> {
_ => true, _ => true,
} }
} }
fn convert_with_default(opt: Option<T>, default: UpdateState<T>) -> UpdateState<T> {
match opt {
Some(t) => UpdateState::Update(t),
None => default,
}
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -249,8 +249,8 @@ mod test {
const ENDPOINT: &str = "/cors"; const ENDPOINT: &str = "/cors";
fn app() -> crate::Server<()> { fn app() -> tide::Server<()> {
let mut app = crate::Server::new(); let mut app = tide::Server::new();
app.at(ENDPOINT).get(|_| async move { "Hello World" }); app.at(ENDPOINT).get(|_| async move { "Hello World" });
app app

View File

@ -146,7 +146,7 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
}, },
}; };
let settings = Settings { let settings = Settings {
attribute_identifier: Some(id), attribute_identifier: Some(Some(id)),
..Settings::default() ..Settings::default()
}; };
index.settings_update(&mut update_writer, settings.into())?; index.settings_update(&mut update_writer, settings.into())?;

View File

@ -1,6 +1,7 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::error; use log::error;
use meilisearch_core::ProcessedUpdateResult; use meilisearch_core::ProcessedUpdateResult;
use meilisearch_schema::Schema;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
@ -94,6 +95,7 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
struct IndexCreateRequest { struct IndexCreateRequest {
name: Option<String>, name: Option<String>,
uid: Option<String>, uid: Option<String>,
attribute_identifier: Option<String>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -148,6 +150,10 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
.updated_at(&writer)? .updated_at(&writer)?
.into_internal_error()?; .into_internal_error()?;
if let Some(id) = body.attribute_identifier {
created_index.main.put_schema(&mut writer, &Schema::with_identifier(id))?;
}
writer.commit()?; writer.commit()?;
let response_body = IndexCreateResponse { let response_body = IndexCreateResponse {

View File

@ -60,27 +60,52 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let index_new_fields = schema.map(|s| s.must_index_new_fields()); let index_new_fields = schema.map(|s| s.must_index_new_fields());
let settings = Settings { let settings = Settings {
ranking_rules, ranking_rules: Some(ranking_rules),
ranking_distinct, ranking_distinct: Some(ranking_distinct),
attribute_identifier, attribute_identifier: Some(attribute_identifier),
attributes_searchable, attributes_searchable: Some(attributes_searchable),
attributes_displayed, attributes_displayed: Some(attributes_displayed),
stop_words, stop_words: Some(stop_words),
synonyms, synonyms: Some(synonyms),
index_new_fields, index_new_fields: Some(index_new_fields),
}; };
Ok(tide::Response::new(200).body_json(&settings).unwrap()) Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
#[derive(Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdateSettings {
pub ranking_rules: Option<Vec<String>>,
pub ranking_distinct: Option<String>,
pub attribute_identifier: Option<String>,
pub attributes_searchable: Option<Vec<String>>,
pub attributes_displayed: Option<HashSet<String>>,
pub stop_words: Option<BTreeSet<String>>,
pub synonyms: Option<BTreeMap<String, Vec<String>>>,
pub index_new_fields: Option<bool>,
}
pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: Settings = ctx.body_json().await.map_err(ResponseError::bad_request)?; let settings_update: UpdateSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings {
ranking_rules: Some(settings_update.ranking_rules),
ranking_distinct: Some(settings_update.ranking_distinct),
attribute_identifier: Some(settings_update.attribute_identifier),
attributes_searchable: Some(settings_update.attributes_searchable),
attributes_displayed: Some(settings_update.attributes_displayed),
stop_words: Some(settings_update.stop_words),
synonyms: Some(settings_update.synonyms),
index_new_fields: Some(settings_update.index_new_fields),
};
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -153,13 +178,13 @@ pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
ranking_rules: settings.ranking_rules, ranking_rules: Some(settings.ranking_rules),
ranking_distinct: settings.ranking_distinct, ranking_distinct: Some(settings.ranking_distinct),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -208,12 +233,12 @@ pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
ranking_rules, ranking_rules: Some(ranking_rules),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -272,12 +297,12 @@ pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
ranking_distinct, ranking_distinct: Some(ranking_distinct),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -348,14 +373,14 @@ pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attribute_identifier: settings.attribute_identifier, attribute_identifier: Some(settings.attribute_identifier),
attributes_searchable: settings.attributes_searchable, attributes_searchable: Some(settings.attributes_searchable),
attributes_displayed: settings.attributes_displayed, attributes_displayed: Some(settings.attributes_displayed),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -425,12 +450,12 @@ pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_searchable, attributes_searchable: Some(attributes_searchable),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -478,12 +503,12 @@ pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_displayed, attributes_displayed: Some(attributes_displayed),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
@ -531,12 +556,12 @@ pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
index_new_fields, index_new_fields: Some(index_new_fields),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?; let update_id = index.settings_update(&mut writer, settings.into())?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };