2022-10-26 18:44:10 +02:00
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
2023-01-31 12:24:37 +01:00
|
|
|
use std::fmt;
|
2022-10-20 18:00:07 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::str::FromStr;
|
2022-10-09 23:47:56 +02:00
|
|
|
|
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn serialize_with_wildcard<S>(
|
2023-01-31 12:24:37 +01:00
|
|
|
field: &Setting<Vec<String>>,
|
2022-10-09 23:47:56 +02:00
|
|
|
s: S,
|
|
|
|
) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2023-01-31 12:24:37 +01:00
|
|
|
use serde::Serialize;
|
2022-10-09 23:47:56 +02:00
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
let wildcard = vec!["*".to_string()];
|
|
|
|
match field {
|
|
|
|
Setting::Set(value) => Some(value),
|
|
|
|
Setting::Reset => Some(&wildcard),
|
|
|
|
Setting::NotSet => None,
|
|
|
|
}
|
|
|
|
.serialize(s)
|
2022-10-09 23:47:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Default, Debug)]
|
|
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
|
|
pub struct Checked;
|
2023-01-31 12:24:37 +01:00
|
|
|
|
2022-10-09 23:47:56 +02:00
|
|
|
#[derive(Clone, Default, Debug, Deserialize)]
|
|
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
|
|
pub struct Unchecked;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, Deserialize)]
|
|
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-10-20 18:00:07 +02:00
|
|
|
#[serde(bound(serialize = "T: serde::Serialize", deserialize = "T: Deserialize<'static>"))]
|
2022-10-09 23:47:56 +02:00
|
|
|
pub struct Settings<T> {
|
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
serialize_with = "serialize_with_wildcard",
|
2023-01-31 12:24:37 +01:00
|
|
|
skip_serializing_if = "Setting::is_not_set"
|
2022-10-09 23:47:56 +02:00
|
|
|
)]
|
2023-01-31 12:24:37 +01:00
|
|
|
pub displayed_attributes: Setting<Vec<String>>,
|
2022-10-09 23:47:56 +02:00
|
|
|
|
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
serialize_with = "serialize_with_wildcard",
|
2023-01-31 12:24:37 +01:00
|
|
|
skip_serializing_if = "Setting::is_not_set"
|
2022-10-09 23:47:56 +02:00
|
|
|
)]
|
2023-01-31 12:24:37 +01:00
|
|
|
pub searchable_attributes: Setting<Vec<String>>,
|
2022-10-09 23:47:56 +02:00
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub filterable_attributes: Setting<BTreeSet<String>>,
|
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub sortable_attributes: Setting<BTreeSet<String>>,
|
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub ranking_rules: Setting<Vec<String>>,
|
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub stop_words: Setting<BTreeSet<String>>,
|
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub synonyms: Setting<BTreeMap<String, Vec<String>>>,
|
|
|
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
|
|
pub distinct_attribute: Setting<String>,
|
2022-10-09 23:47:56 +02:00
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
pub _kind: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings<Unchecked> {
|
2023-01-31 12:24:37 +01:00
|
|
|
pub fn check(self) -> Settings<Checked> {
|
|
|
|
let displayed_attributes = match self.displayed_attributes {
|
|
|
|
Setting::Set(fields) => {
|
2022-10-09 23:47:56 +02:00
|
|
|
if fields.iter().any(|f| f == "*") {
|
2023-01-31 12:24:37 +01:00
|
|
|
Setting::Reset
|
2022-10-09 23:47:56 +02:00
|
|
|
} else {
|
2023-01-31 12:24:37 +01:00
|
|
|
Setting::Set(fields)
|
2022-10-09 23:47:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
otherwise => otherwise,
|
|
|
|
};
|
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
let searchable_attributes = match self.searchable_attributes {
|
|
|
|
Setting::Set(fields) => {
|
2022-10-09 23:47:56 +02:00
|
|
|
if fields.iter().any(|f| f == "*") {
|
2023-01-31 12:24:37 +01:00
|
|
|
Setting::Reset
|
2022-10-09 23:47:56 +02:00
|
|
|
} else {
|
2023-01-31 12:24:37 +01:00
|
|
|
Setting::Set(fields)
|
2022-10-09 23:47:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
otherwise => otherwise,
|
|
|
|
};
|
|
|
|
|
|
|
|
Settings {
|
|
|
|
displayed_attributes,
|
|
|
|
searchable_attributes,
|
|
|
|
filterable_attributes: self.filterable_attributes,
|
2023-01-31 12:24:37 +01:00
|
|
|
sortable_attributes: self.sortable_attributes,
|
2022-10-09 23:47:56 +02:00
|
|
|
ranking_rules: self.ranking_rules,
|
|
|
|
stop_words: self.stop_words,
|
|
|
|
synonyms: self.synonyms,
|
|
|
|
distinct_attribute: self.distinct_attribute,
|
|
|
|
_kind: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 12:47:48 +02:00
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Setting<T> {
|
|
|
|
Set(T),
|
|
|
|
Reset,
|
|
|
|
NotSet,
|
|
|
|
}
|
2022-10-17 12:47:48 +02:00
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
impl<T> Default for Setting<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::NotSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Setting<T> {
|
|
|
|
pub const fn is_not_set(&self) -> bool {
|
|
|
|
matches!(self, Self::NotSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map<A>(self, f: fn(T) -> A) -> Setting<A> {
|
|
|
|
match self {
|
|
|
|
Setting::Set(a) => Setting::Set(f(a)),
|
|
|
|
Setting::Reset => Setting::Reset,
|
|
|
|
Setting::NotSet => Setting::NotSet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
impl<T: serde::Serialize> serde::Serialize for Setting<T> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
Self::Set(value) => Some(value),
|
|
|
|
// Usually not_set isn't serialized by setting skip_serializing_if field attribute
|
|
|
|
Self::NotSet | Self::Reset => None,
|
|
|
|
}
|
|
|
|
.serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Setting<T> {
|
|
|
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
Deserialize::deserialize(deserializer).map(|x| match x {
|
|
|
|
Some(x) => Self::Set(x),
|
|
|
|
None => Self::Reset, // Reset is forced by sending null value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-10-17 12:47:48 +02:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
|
|
|
/// Query words at the front of an attribute is considered better than if it was at the back.
|
|
|
|
Words,
|
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
2023-01-31 12:24:37 +01:00
|
|
|
/// attributes are considered better.
|
2022-10-17 12:47:48 +02:00
|
|
|
Attribute,
|
2023-01-31 12:24:37 +01:00
|
|
|
/// Dynamically sort at query time the documents. None, one or multiple Asc/Desc sortable
|
|
|
|
/// attributes can be used in place of this criterion at query time.
|
|
|
|
Sort,
|
2022-10-17 12:47:48 +02:00
|
|
|
/// Sorted by the similarity of the matched words with the query words.
|
|
|
|
Exactness,
|
|
|
|
/// Sorted by the increasing value of the field specified.
|
|
|
|
Asc(String),
|
|
|
|
/// Sorted by the decreasing value of the field specified.
|
|
|
|
Desc(String),
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
impl Criterion {
|
|
|
|
/// Returns the field name parameter of this criterion.
|
|
|
|
pub fn field_name(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
Criterion::Asc(name) | Criterion::Desc(name) => Some(name),
|
|
|
|
_otherwise => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:47:48 +02:00
|
|
|
impl FromStr for Criterion {
|
2023-01-31 12:24:37 +01:00
|
|
|
// since we're not going to show the custom error message we can override the
|
|
|
|
// error type.
|
2022-10-17 12:47:48 +02:00
|
|
|
type Err = ();
|
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
|
|
|
match text {
|
2022-10-17 12:47:48 +02:00
|
|
|
"words" => Ok(Criterion::Words),
|
|
|
|
"typo" => Ok(Criterion::Typo),
|
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
2023-01-31 12:24:37 +01:00
|
|
|
"sort" => Ok(Criterion::Sort),
|
2022-10-17 12:47:48 +02:00
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
2023-01-31 12:24:37 +01:00
|
|
|
text => match AscDesc::from_str(text) {
|
|
|
|
Ok(AscDesc::Asc(field)) => Ok(Criterion::Asc(field)),
|
|
|
|
Ok(AscDesc::Desc(field)) => Ok(Criterion::Desc(field)),
|
|
|
|
Err(_) => Err(()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
|
|
|
|
pub enum AscDesc {
|
|
|
|
Asc(String),
|
|
|
|
Desc(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for AscDesc {
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
// since we don't know if this comes from the old or new syntax we need to check
|
|
|
|
// for both syntax.
|
|
|
|
// WARN: this code doesn't come from the original meilisearch v0.22.0 but was
|
|
|
|
// written specifically to be able to import the dump of meilisearch v0.21.0 AND
|
|
|
|
// meilisearch v0.22.0.
|
|
|
|
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
|
|
|
|
if let Some((field_name, asc_desc)) = text.rsplit_once(':') {
|
|
|
|
match asc_desc {
|
|
|
|
"asc" => Ok(AscDesc::Asc(field_name.to_string())),
|
|
|
|
"desc" => Ok(AscDesc::Desc(field_name.to_string())),
|
|
|
|
_ => Err(()),
|
2022-10-17 12:47:48 +02:00
|
|
|
}
|
2023-01-31 12:49:35 +01:00
|
|
|
} else if text.starts_with("asc(") && text.ends_with(')') {
|
2023-01-31 12:24:37 +01:00
|
|
|
Ok(AscDesc::Asc(
|
2023-01-31 12:49:35 +01:00
|
|
|
text.strip_prefix("asc(").unwrap().strip_suffix(')').unwrap().to_string(),
|
2023-01-31 12:24:37 +01:00
|
|
|
))
|
2023-01-31 12:49:35 +01:00
|
|
|
} else if text.starts_with("desc(") && text.ends_with(')') {
|
2023-01-31 12:24:37 +01:00
|
|
|
Ok(AscDesc::Desc(
|
2023-01-31 12:49:35 +01:00
|
|
|
text.strip_prefix("desc(").unwrap().strip_suffix(')').unwrap().to_string(),
|
2023-01-31 12:24:37 +01:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Err(())
|
2022-10-17 12:47:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 14:52:20 +01:00
|
|
|
|
2023-01-31 12:24:37 +01:00
|
|
|
impl fmt::Display for Criterion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Criterion::*;
|
|
|
|
|
2022-11-30 14:52:20 +01:00
|
|
|
match self {
|
2023-01-31 12:24:37 +01:00
|
|
|
Words => f.write_str("words"),
|
|
|
|
Typo => f.write_str("typo"),
|
|
|
|
Proximity => f.write_str("proximity"),
|
|
|
|
Attribute => f.write_str("attribute"),
|
|
|
|
Sort => f.write_str("sort"),
|
|
|
|
Exactness => f.write_str("exactness"),
|
|
|
|
Asc(attr) => write!(f, "{}:asc", attr),
|
|
|
|
Desc(attr) => write!(f, "{}:desc", attr),
|
2022-11-30 14:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|