2021-03-06 02:54:46 +08:00
|
|
|
use std::fmt;
|
2021-06-01 21:48:38 +08:00
|
|
|
use std::str::FromStr;
|
2020-11-27 19:14:56 +08:00
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-05-03 02:10:25 +08:00
|
|
|
use thiserror::Error;
|
2020-11-27 19:14:56 +08:00
|
|
|
|
2022-05-03 02:10:25 +08:00
|
|
|
use crate::{AscDesc, Member};
|
2021-09-22 22:02:07 +08:00
|
|
|
|
2022-05-03 02:10:25 +08:00
|
|
|
#[derive(Error, Debug)]
|
2021-09-22 22:02:07 +08:00
|
|
|
pub enum CriterionError {
|
2022-05-03 02:10:25 +08:00
|
|
|
#[error("`{name}` ranking rule is invalid. Valid ranking rules are Words, Typo, Sort, Proximity, Attribute, Exactness and custom ranking rules.")]
|
2021-09-22 22:02:07 +08:00
|
|
|
InvalidName { name: String },
|
2022-05-03 02:10:25 +08:00
|
|
|
#[error("`{name}` is a reserved keyword and thus can't be used as a ranking rule")]
|
2021-09-22 22:02:07 +08:00
|
|
|
ReservedName { name: String },
|
2022-05-03 02:10:25 +08:00
|
|
|
#[error(
|
|
|
|
"`{name}` is a reserved keyword and thus can't be used as a ranking rule. \
|
|
|
|
`{name}` can only be used for sorting at search time"
|
|
|
|
)]
|
2021-09-22 22:02:07 +08:00
|
|
|
ReservedNameForSort { name: String },
|
2022-05-03 02:10:25 +08:00
|
|
|
#[error(
|
|
|
|
"`{name}` is a reserved keyword and thus can't be used as a ranking rule. \
|
|
|
|
`{name}` can only be used for filtering at search time"
|
|
|
|
)]
|
2021-09-22 22:02:07 +08:00
|
|
|
ReservedNameForFilter { name: String },
|
|
|
|
}
|
|
|
|
|
2021-01-21 00:27:43 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
2020-08-12 16:43:02 +08:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
2021-04-02 01:13:18 +08:00
|
|
|
/// Query words at the front of an attribute is considered better than if it was at the back.
|
2020-08-12 16:43:02 +08:00
|
|
|
Words,
|
2021-04-02 01:13:18 +08:00
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
2020-08-12 16:43:02 +08:00
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
2021-08-23 17:37:18 +08:00
|
|
|
/// attributes are considered better.
|
2020-08-12 16:43:02 +08:00
|
|
|
Attribute,
|
2021-09-07 22:11:44 +08: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,
|
2020-08-12 16:43:02 +08:00
|
|
|
/// Sorted by the similarity of the matched words with the query words.
|
|
|
|
Exactness,
|
|
|
|
/// Sorted by the increasing value of the field specified.
|
2021-01-21 00:27:43 +08:00
|
|
|
Asc(String),
|
2020-08-12 16:43:02 +08:00
|
|
|
/// Sorted by the decreasing value of the field specified.
|
2021-01-21 00:27:43 +08:00
|
|
|
Desc(String),
|
2020-08-12 16:43:02 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 23:03:27 +08: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 21:48:38 +08:00
|
|
|
impl FromStr for Criterion {
|
2021-09-22 22:02:07 +08:00
|
|
|
type Err = CriterionError;
|
2021-06-01 21:48:38 +08:00
|
|
|
|
2021-08-23 17:37:18 +08:00
|
|
|
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
|
|
|
match text {
|
2020-12-04 19:02:22 +08:00
|
|
|
"words" => Ok(Criterion::Words),
|
2021-04-02 01:13:18 +08:00
|
|
|
"typo" => Ok(Criterion::Typo),
|
2020-12-04 19:02:22 +08:00
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
2021-09-07 22:11:44 +08:00
|
|
|
"sort" => Ok(Criterion::Sort),
|
2020-12-04 19:02:22 +08:00
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
2021-09-22 22:09:08 +08:00
|
|
|
text => match AscDesc::from_str(text)? {
|
|
|
|
AscDesc::Asc(Member::Field(field)) => Ok(Criterion::Asc(field)),
|
|
|
|
AscDesc::Desc(Member::Field(field)) => Ok(Criterion::Desc(field)),
|
|
|
|
AscDesc::Asc(Member::Geo(_)) | AscDesc::Desc(Member::Geo(_)) => {
|
2021-09-22 22:02:07 +08:00
|
|
|
Err(CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() })?
|
2021-09-22 21:09:07 +08:00
|
|
|
}
|
2021-08-23 17:37:18 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:43:02 +08:00
|
|
|
pub fn default_criteria() -> Vec<Criterion> {
|
|
|
|
vec![
|
|
|
|
Criterion::Words,
|
2021-04-02 01:13:18 +08:00
|
|
|
Criterion::Typo,
|
2020-08-12 16:43:02 +08:00
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Attribute,
|
2021-09-07 22:11:44 +08:00
|
|
|
Criterion::Sort,
|
2020-08-12 16:43:02 +08:00
|
|
|
Criterion::Exactness,
|
|
|
|
]
|
|
|
|
}
|
2021-03-06 02:54:46 +08:00
|
|
|
|
|
|
|
impl fmt::Display for Criterion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Criterion::*;
|
|
|
|
|
|
|
|
match self {
|
2021-06-17 00:33:33 +08:00
|
|
|
Words => f.write_str("words"),
|
|
|
|
Typo => f.write_str("typo"),
|
|
|
|
Proximity => f.write_str("proximity"),
|
|
|
|
Attribute => f.write_str("attribute"),
|
2021-09-07 22:11:44 +08:00
|
|
|
Sort => f.write_str("sort"),
|
2021-06-17 00:33:33 +08:00
|
|
|
Exactness => f.write_str("exactness"),
|
2021-08-23 17:37:18 +08:00
|
|
|
Asc(attr) => write!(f, "{}:asc", attr),
|
|
|
|
Desc(attr) => write!(f, "{}:desc", attr),
|
2021-03-06 02:54:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 23:21:02 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-09-22 21:09:07 +08:00
|
|
|
use big_s::S;
|
2021-09-22 22:02:07 +08:00
|
|
|
use CriterionError::*;
|
2021-09-22 21:09:07 +08:00
|
|
|
|
2021-09-20 23:21:02 +08:00
|
|
|
use super::*;
|
|
|
|
|
2021-09-22 21:09:07 +08:00
|
|
|
#[test]
|
|
|
|
fn parse_criterion() {
|
|
|
|
let valid_criteria = [
|
|
|
|
("words", Criterion::Words),
|
|
|
|
("typo", Criterion::Typo),
|
|
|
|
("proximity", Criterion::Proximity),
|
|
|
|
("attribute", Criterion::Attribute),
|
|
|
|
("sort", Criterion::Sort),
|
|
|
|
("exactness", Criterion::Exactness),
|
|
|
|
("price:asc", Criterion::Asc(S("price"))),
|
|
|
|
("price:desc", Criterion::Desc(S("price"))),
|
|
|
|
("price:asc:desc", Criterion::Desc(S("price:asc"))),
|
|
|
|
("truc:machin:desc", Criterion::Desc(S("truc:machin"))),
|
|
|
|
("hello-world!:desc", Criterion::Desc(S("hello-world!"))),
|
|
|
|
("it's spacy over there:asc", Criterion::Asc(S("it's spacy over there"))),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in valid_criteria {
|
|
|
|
let res = input.parse::<Criterion>();
|
|
|
|
assert!(
|
|
|
|
res.is_ok(),
|
|
|
|
"Failed to parse `{}`, was expecting `{:?}` but instead got `{:?}`",
|
|
|
|
input,
|
|
|
|
expected,
|
|
|
|
res
|
|
|
|
);
|
|
|
|
assert_eq!(res.unwrap(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
let invalid_criteria = [
|
2021-09-22 22:02:07 +08:00
|
|
|
("words suffix", InvalidName { name: S("words suffix") }),
|
|
|
|
("prefix typo", InvalidName { name: S("prefix typo") }),
|
|
|
|
("proximity attribute", InvalidName { name: S("proximity attribute") }),
|
|
|
|
("price", InvalidName { name: S("price") }),
|
|
|
|
("asc:price", InvalidName { name: S("asc:price") }),
|
|
|
|
("price:deesc", InvalidName { name: S("price:deesc") }),
|
|
|
|
("price:aasc", InvalidName { name: S("price:aasc") }),
|
|
|
|
("price:asc and desc", InvalidName { name: S("price:asc and desc") }),
|
|
|
|
("price:asc:truc", InvalidName { name: S("price:asc:truc") }),
|
|
|
|
("_geo:asc", ReservedName { name: S("_geo") }),
|
|
|
|
("_geoDistance:asc", ReservedName { name: S("_geoDistance") }),
|
|
|
|
("_geoPoint:asc", ReservedNameForSort { name: S("_geoPoint") }),
|
|
|
|
("_geoPoint(42, 75):asc", ReservedNameForSort { name: S("_geoPoint") }),
|
|
|
|
("_geoRadius:asc", ReservedNameForFilter { name: S("_geoRadius") }),
|
|
|
|
("_geoRadius(42, 75, 59):asc", ReservedNameForFilter { name: S("_geoRadius") }),
|
2021-09-22 21:09:07 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in invalid_criteria {
|
|
|
|
let res = input.parse::<Criterion>();
|
|
|
|
assert!(
|
|
|
|
res.is_err(),
|
|
|
|
"Should no be able to parse `{}`, was expecting an error but instead got: `{:?}`",
|
|
|
|
input,
|
|
|
|
res
|
|
|
|
);
|
|
|
|
let res = res.unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
res.to_string(),
|
|
|
|
expected.to_string(),
|
|
|
|
"Bad error for input {}: got `{:?}` instead of `{:?}`",
|
|
|
|
input,
|
|
|
|
res,
|
|
|
|
expected
|
|
|
|
);
|
2021-09-20 23:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|