meilisearch/meilisearch-core/src/settings.rs

181 lines
6.2 KiB
Rust
Raw Normal View History

2020-01-17 02:19:44 +08:00
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr;
2020-02-11 22:16:02 +08:00
use std::iter::IntoIterator;
2020-01-08 21:17:38 +08:00
2020-01-29 00:45:29 +08:00
use serde::{Deserialize, Deserializer, Serialize};
use once_cell::sync::Lazy;
2020-01-30 01:30:21 +08:00
static RANKING_RULE_REGEX: Lazy<regex::Regex> = Lazy::new(|| {
let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap();
2020-01-30 01:30:21 +08:00
regex
});
2020-01-08 21:17:38 +08:00
#[derive(Default, Clone, Serialize, Deserialize)]
2020-01-21 19:03:48 +08:00
#[serde(rename_all = "camelCase", deny_unknown_fields)]
2020-01-08 21:17:38 +08:00
pub struct Settings {
2020-01-29 00:45:29 +08:00
#[serde(default, deserialize_with = "deserialize_some")]
pub ranking_rules: Option<Option<Vec<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub ranking_distinct: Option<Option<String>>,
#[serde(default, deserialize_with = "deserialize_some")]
2020-01-30 01:30:21 +08:00
pub searchable_attributes: Option<Option<Vec<String>>>,
2020-01-29 00:45:29 +08:00
#[serde(default, deserialize_with = "deserialize_some")]
2020-01-30 01:30:21 +08:00
pub displayed_attributes: Option<Option<HashSet<String>>>,
2020-01-29 00:45:29 +08:00
#[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>>,
2020-01-08 21:17:38 +08:00
}
2020-01-29 00:45:29 +08:00
// Any value that is present is considered Some value, including null.
fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where T: Deserialize<'de>,
D: Deserializer<'de>
{
Deserialize::deserialize(deserializer).map(Some)
2020-01-08 21:17:38 +08:00
}
2020-01-30 01:30:21 +08:00
impl Settings {
pub fn into_update(&self) -> Result<SettingsUpdate, RankingRuleConversionError> {
2020-01-20 16:52:24 +08:00
let settings = self.clone();
let ranking_rules = match settings.ranking_rules {
2020-02-11 22:16:02 +08:00
Some(Some(rules)) => UpdateState::Update(RankingRule::from_iter(rules.iter())?),
2020-01-29 00:45:29 +08:00
Some(None) => UpdateState::Clear,
None => UpdateState::Nothing,
2020-01-20 16:52:24 +08:00
};
2020-01-30 01:30:21 +08:00
Ok(SettingsUpdate {
2020-02-03 05:59:19 +08:00
ranking_rules,
2020-01-20 16:52:24 +08:00
ranking_distinct: settings.ranking_distinct.into(),
2020-02-11 22:16:02 +08:00
identifier: UpdateState::Nothing,
2020-01-30 01:30:21 +08:00
searchable_attributes: settings.searchable_attributes.into(),
displayed_attributes: settings.displayed_attributes.into(),
2020-01-20 16:52:24 +08:00
stop_words: settings.stop_words.into(),
synonyms: settings.synonyms.into(),
2020-01-27 15:52:36 +08:00
index_new_fields: settings.index_new_fields.into(),
2020-01-30 01:30:21 +08:00
})
2020-01-20 16:52:24 +08:00
}
}
2020-01-08 21:17:38 +08:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateState<T> {
Update(T),
Clear,
Nothing,
}
2020-01-29 00:45:29 +08:00
impl <T> From<Option<Option<T>>> for UpdateState<T> {
fn from(opt: Option<Option<T>>) -> UpdateState<T> {
2020-01-08 21:17:38 +08:00
match opt {
2020-01-29 00:45:29 +08:00
Some(Some(t)) => UpdateState::Update(t),
Some(None) => UpdateState::Clear,
2020-01-08 21:17:38 +08:00
None => UpdateState::Nothing,
}
}
}
2020-01-17 02:19:44 +08:00
#[derive(Debug, Clone)]
pub struct RankingRuleConversionError;
impl std::fmt::Display for RankingRuleConversionError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "impossible to convert into RankingRule")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RankingRule {
Typo,
Words,
Proximity,
Attribute,
WordsPosition,
Exactness,
Asc(String),
Dsc(String),
}
2020-01-17 02:19:44 +08:00
impl ToString for RankingRule {
fn to_string(&self) -> String {
match self {
RankingRule::Typo => "_typo".to_string(),
RankingRule::Words => "_words".to_string(),
RankingRule::Proximity => "_proximity".to_string(),
RankingRule::Attribute => "_attribute".to_string(),
2020-01-18 23:12:02 +08:00
RankingRule::WordsPosition => "_words_position".to_string(),
RankingRule::Exactness => "_exactness".to_string(),
2020-01-17 02:19:44 +08:00
RankingRule::Asc(field) => format!("asc({})", field),
RankingRule::Dsc(field) => format!("dsc({})", field),
}
}
}
impl FromStr for RankingRule {
type Err = RankingRuleConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let rule = match s {
"_typo" => RankingRule::Typo,
"_words" => RankingRule::Words,
"_proximity" => RankingRule::Proximity,
"_attribute" => RankingRule::Attribute,
"_words_position" => RankingRule::WordsPosition,
"_exactness" => RankingRule::Exactness,
2020-01-17 02:19:44 +08:00
_ => {
2020-01-30 01:30:21 +08:00
let captures = RANKING_RULE_REGEX.captures(s).ok_or(RankingRuleConversionError)?;
match (captures.get(1).map(|m| m.as_str()), captures.get(2)) {
(Some("asc"), Some(field)) => RankingRule::Asc(field.as_str().to_string()),
(Some("dsc"), Some(field)) => RankingRule::Dsc(field.as_str().to_string()),
2020-01-17 02:19:44 +08:00
_ => return Err(RankingRuleConversionError)
}
}
};
Ok(rule)
}
}
2020-01-20 16:52:24 +08:00
impl RankingRule {
2020-02-11 22:16:02 +08:00
pub fn field(&self) -> Option<&str> {
2020-01-20 16:52:24 +08:00
match self {
2020-01-30 01:30:21 +08:00
RankingRule::Asc(field) | RankingRule::Dsc(field) => Some(field),
2020-01-20 16:52:24 +08:00
_ => None,
}
}
2020-02-11 22:16:02 +08:00
pub fn from_iter(rules: impl IntoIterator<Item = impl AsRef<str>>) -> Result<Vec<RankingRule>, RankingRuleConversionError> {
rules.into_iter()
.map(|s| RankingRule::from_str(s.as_ref()))
2020-01-20 16:52:24 +08:00
.collect()
}
}
2020-01-08 21:17:38 +08:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SettingsUpdate {
pub ranking_rules: UpdateState<Vec<RankingRule>>,
2020-01-08 21:17:38 +08:00
pub ranking_distinct: UpdateState<String>,
2020-01-30 01:30:21 +08:00
pub identifier: UpdateState<String>,
pub searchable_attributes: UpdateState<Vec<String>>,
pub displayed_attributes: UpdateState<HashSet<String>>,
2020-01-08 21:17:38 +08:00
pub stop_words: UpdateState<BTreeSet<String>>,
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
2020-01-27 15:52:36 +08:00
pub index_new_fields: UpdateState<bool>,
2020-01-08 21:17:38 +08:00
}
impl Default for SettingsUpdate {
fn default() -> Self {
Self {
ranking_rules: UpdateState::Nothing,
ranking_distinct: UpdateState::Nothing,
2020-01-30 01:30:21 +08:00
identifier: UpdateState::Nothing,
searchable_attributes: UpdateState::Nothing,
displayed_attributes: UpdateState::Nothing,
2020-01-08 21:17:38 +08:00
stop_words: UpdateState::Nothing,
synonyms: UpdateState::Nothing,
2020-01-27 15:52:36 +08:00
index_new_fields: UpdateState::Nothing,
2020-01-08 21:17:38 +08:00
}
}
}