2021-03-05 19:54:46 +01:00
|
|
|
use std::fmt;
|
2021-06-01 15:48:38 +02:00
|
|
|
use std::str::FromStr;
|
2020-11-27 12:14:56 +01:00
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-11-27 12:14:56 +01:00
|
|
|
|
2021-09-02 15:57:40 +02:00
|
|
|
use crate::error::{is_reserved_keyword, Error, UserError};
|
2021-06-14 16:46:19 +02:00
|
|
|
|
2021-01-20 17:27:43 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
2020-08-12 10:43:02 +02:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
2021-04-01 19:13:18 +02:00
|
|
|
/// Query words at the front of an attribute is considered better than if it was at the back.
|
2020-08-12 10:43:02 +02:00
|
|
|
Words,
|
2021-04-01 19:13:18 +02:00
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
2020-08-12 10:43:02 +02:00
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
2021-08-23 11:37:18 +02:00
|
|
|
/// attributes are considered better.
|
2020-08-12 10:43:02 +02:00
|
|
|
Attribute,
|
2021-09-07 16:11:44 +02: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 10:43:02 +02: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-20 17:27:43 +01:00
|
|
|
Asc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
/// Sorted by the decreasing value of the field specified.
|
2021-01-20 17:27:43 +01:00
|
|
|
Desc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-08 17:03:27 +02: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 15:48:38 +02:00
|
|
|
impl FromStr for Criterion {
|
2021-06-14 16:46:19 +02:00
|
|
|
type Err = Error;
|
2021-06-01 15:48:38 +02:00
|
|
|
|
2021-08-23 11:37:18 +02:00
|
|
|
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
|
|
|
match text {
|
2020-12-04 12:02:22 +01:00
|
|
|
"words" => Ok(Criterion::Words),
|
2021-04-01 19:13:18 +02:00
|
|
|
"typo" => Ok(Criterion::Typo),
|
2020-12-04 12:02:22 +01:00
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
2021-09-07 16:11:44 +02:00
|
|
|
"sort" => Ok(Criterion::Sort),
|
2020-12-04 12:02:22 +01:00
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
2021-08-23 11:37:18 +02:00
|
|
|
text => match AscDesc::from_str(text) {
|
2021-09-02 15:57:40 +02:00
|
|
|
Ok(AscDesc::Asc(Member::Field(field))) => Ok(Criterion::Asc(field)),
|
|
|
|
Ok(AscDesc::Desc(Member::Field(field))) => Ok(Criterion::Desc(field)),
|
|
|
|
Ok(AscDesc::Asc(Member::Geo(_))) | Ok(AscDesc::Desc(Member::Geo(_))) => {
|
2021-09-22 15:09:07 +02:00
|
|
|
Err(UserError::InvalidReservedRankingRuleNameSort {
|
|
|
|
name: "_geoPoint".to_string(),
|
|
|
|
})?
|
2021-09-02 15:57:40 +02:00
|
|
|
}
|
2021-09-07 11:41:25 +02:00
|
|
|
Err(UserError::InvalidAscDescSyntax { name }) => {
|
2021-09-22 15:09:07 +02:00
|
|
|
Err(UserError::InvalidRankingRuleName { name })?
|
|
|
|
}
|
|
|
|
Err(UserError::InvalidReservedAscDescSyntax { name })
|
|
|
|
if name.starts_with("_geoPoint") =>
|
|
|
|
{
|
|
|
|
Err(UserError::InvalidReservedRankingRuleNameSort {
|
|
|
|
name: "_geoPoint".to_string(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
Err(UserError::InvalidReservedAscDescSyntax { name })
|
|
|
|
if name.starts_with("_geoRadius") =>
|
|
|
|
{
|
|
|
|
Err(UserError::InvalidReservedRankingRuleNameFilter {
|
|
|
|
name: "_geoRadius".to_string(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
Err(UserError::InvalidReservedAscDescSyntax { name }) => {
|
|
|
|
Err(UserError::InvalidReservedRankingRuleName { name }.into())
|
2021-09-07 11:41:25 +02:00
|
|
|
}
|
|
|
|
Err(error) => {
|
2021-09-08 19:17:00 +02:00
|
|
|
Err(UserError::InvalidRankingRuleName { name: error.to_string() }.into())
|
2021-09-07 11:41:25 +02:00
|
|
|
}
|
2021-08-23 11:37:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:22:52 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
|
|
|
pub enum Member {
|
|
|
|
Field(String),
|
|
|
|
Geo([f64; 2]),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Member {
|
|
|
|
type Err = UserError;
|
|
|
|
|
|
|
|
fn from_str(text: &str) -> Result<Member, Self::Err> {
|
2021-09-20 18:08:22 +02:00
|
|
|
match text.strip_prefix("_geoPoint(").and_then(|text| text.strip_suffix(")")) {
|
|
|
|
Some(point) => {
|
|
|
|
let (lat, long) = point
|
|
|
|
.split_once(',')
|
2021-09-22 15:09:07 +02:00
|
|
|
.ok_or_else(|| UserError::InvalidReservedAscDescSyntax {
|
|
|
|
name: text.to_string(),
|
|
|
|
})
|
2021-09-20 18:08:22 +02:00
|
|
|
.and_then(|(lat, long)| {
|
|
|
|
lat.trim()
|
|
|
|
.parse()
|
|
|
|
.and_then(|lat| long.trim().parse().map(|long| (lat, long)))
|
2021-09-22 15:09:07 +02:00
|
|
|
.map_err(|_| UserError::InvalidReservedAscDescSyntax {
|
2021-09-20 18:08:22 +02:00
|
|
|
name: text.to_string(),
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok(Member::Geo([lat, long]))
|
|
|
|
}
|
|
|
|
None => {
|
2021-09-22 15:09:07 +02:00
|
|
|
if is_reserved_keyword(text) || text.starts_with("_geoRadius(") {
|
|
|
|
return Err(UserError::InvalidReservedAscDescSyntax {
|
2021-09-20 18:08:22 +02:00
|
|
|
name: text.to_string(),
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
Ok(Member::Field(text.to_string()))
|
2021-09-20 17:21:02 +02:00
|
|
|
}
|
2021-08-30 18:22:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Member {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2021-09-09 12:20:08 +02:00
|
|
|
Member::Field(name) => f.write_str(name),
|
2021-08-30 18:22:52 +02:00
|
|
|
Member::Geo([lat, lng]) => write!(f, "_geoPoint({}, {})", lat, lng),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Member {
|
|
|
|
pub fn field(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
Member::Field(field) => Some(field),
|
|
|
|
Member::Geo(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn geo_point(&self) -> Option<&[f64; 2]> {
|
|
|
|
match self {
|
|
|
|
Member::Geo(point) => Some(point),
|
|
|
|
Member::Field(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
2021-08-23 11:37:18 +02:00
|
|
|
pub enum AscDesc {
|
2021-08-30 18:22:52 +02:00
|
|
|
Asc(Member),
|
|
|
|
Desc(Member),
|
2021-08-23 11:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AscDesc {
|
2021-08-30 18:22:52 +02:00
|
|
|
pub fn member(&self) -> &Member {
|
2021-08-23 11:37:18 +02:00
|
|
|
match self {
|
2021-08-30 18:22:52 +02:00
|
|
|
AscDesc::Asc(member) => member,
|
|
|
|
AscDesc::Desc(member) => member,
|
2021-08-23 11:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 18:22:52 +02:00
|
|
|
|
|
|
|
pub fn field(&self) -> Option<&str> {
|
|
|
|
self.member().field()
|
|
|
|
}
|
2021-08-23 11:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for AscDesc {
|
|
|
|
type Err = UserError;
|
|
|
|
|
2021-09-07 11:41:25 +02:00
|
|
|
/// Since we don't know if this was deserialized for a criterion or a sort we just return a
|
|
|
|
/// string and let the caller create his own error
|
2021-08-23 11:37:18 +02:00
|
|
|
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
|
|
|
|
match text.rsplit_once(':') {
|
2021-08-30 18:22:52 +02:00
|
|
|
Some((left, "asc")) => Ok(AscDesc::Asc(left.parse()?)),
|
|
|
|
Some((left, "desc")) => Ok(AscDesc::Desc(left.parse()?)),
|
2021-09-22 11:37:05 +02:00
|
|
|
_ => Err(UserError::InvalidAscDescSyntax { name: text.to_string() }),
|
2021-08-17 15:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:43:02 +02:00
|
|
|
pub fn default_criteria() -> Vec<Criterion> {
|
|
|
|
vec![
|
|
|
|
Criterion::Words,
|
2021-04-01 19:13:18 +02:00
|
|
|
Criterion::Typo,
|
2020-08-12 10:43:02 +02:00
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Attribute,
|
2021-09-07 16:11:44 +02:00
|
|
|
Criterion::Sort,
|
2020-08-12 10:43:02 +02:00
|
|
|
Criterion::Exactness,
|
|
|
|
]
|
|
|
|
}
|
2021-03-05 19:54:46 +01:00
|
|
|
|
|
|
|
impl fmt::Display for Criterion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Criterion::*;
|
|
|
|
|
|
|
|
match self {
|
2021-06-16 18:33:33 +02:00
|
|
|
Words => f.write_str("words"),
|
|
|
|
Typo => f.write_str("typo"),
|
|
|
|
Proximity => f.write_str("proximity"),
|
|
|
|
Attribute => f.write_str("attribute"),
|
2021-09-07 16:11:44 +02:00
|
|
|
Sort => f.write_str("sort"),
|
2021-06-16 18:33:33 +02:00
|
|
|
Exactness => f.write_str("exactness"),
|
2021-08-23 11:37:18 +02:00
|
|
|
Asc(attr) => write!(f, "{}:asc", attr),
|
|
|
|
Desc(attr) => write!(f, "{}:desc", attr),
|
2021-03-05 19:54:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 17:21:02 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-09-22 15:09:07 +02:00
|
|
|
use big_s::S;
|
|
|
|
use AscDesc::*;
|
|
|
|
use Member::*;
|
|
|
|
use UserError::*;
|
|
|
|
|
2021-09-20 17:21:02 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_asc_desc() {
|
|
|
|
let valid_req = [
|
|
|
|
("truc:asc", Asc(Field(S("truc")))),
|
|
|
|
("bidule:desc", Desc(Field(S("bidule")))),
|
|
|
|
("a-b:desc", Desc(Field(S("a-b")))),
|
|
|
|
("a:b:desc", Desc(Field(S("a:b")))),
|
|
|
|
("a12:asc", Asc(Field(S("a12")))),
|
|
|
|
("42:asc", Asc(Field(S("42")))),
|
|
|
|
("_geoPoint(42, 59):asc", Asc(Geo([42., 59.]))),
|
|
|
|
("_geoPoint(42.459, 59):desc", Desc(Geo([42.459, 59.]))),
|
|
|
|
("_geoPoint(42, 59.895):desc", Desc(Geo([42., 59.895]))),
|
|
|
|
("_geoPoint(42, 59.895):desc", Desc(Geo([42., 59.895]))),
|
|
|
|
("_geoPoint(42.0002, 59.895):desc", Desc(Geo([42.0002, 59.895]))),
|
|
|
|
("_geoPoint(42., 59.):desc", Desc(Geo([42., 59.]))),
|
|
|
|
("truc(12, 13):desc", Desc(Field(S("truc(12, 13)")))),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (req, expected) in valid_req {
|
2021-09-22 15:09:07 +02:00
|
|
|
let res = req.parse::<AscDesc>();
|
|
|
|
assert!(
|
|
|
|
res.is_ok(),
|
|
|
|
"Failed to parse `{}`, was expecting `{:?}` but instead got `{:?}`",
|
|
|
|
req,
|
|
|
|
expected,
|
|
|
|
res
|
|
|
|
);
|
|
|
|
assert_eq!(res.unwrap(), expected);
|
2021-09-20 17:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let invalid_req = [
|
2021-09-22 15:09:07 +02:00
|
|
|
("truc:machin", InvalidAscDescSyntax { name: S("truc:machin") }),
|
|
|
|
("truc:deesc", InvalidAscDescSyntax { name: S("truc:deesc") }),
|
|
|
|
("truc:asc:deesc", InvalidAscDescSyntax { name: S("truc:asc:deesc") }),
|
|
|
|
("42desc", InvalidAscDescSyntax { name: S("42desc") }),
|
|
|
|
("_geoPoint:asc", InvalidReservedAscDescSyntax { name: S("_geoPoint") }),
|
|
|
|
("_geoDistance:asc", InvalidReservedAscDescSyntax { name: S("_geoDistance") }),
|
|
|
|
(
|
|
|
|
"_geoPoint(42.12 , 59.598)",
|
|
|
|
InvalidAscDescSyntax { name: S("_geoPoint(42.12 , 59.598)") },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"_geoPoint(42.12 , 59.598):deesc",
|
|
|
|
InvalidAscDescSyntax { name: S("_geoPoint(42.12 , 59.598):deesc") },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"_geoPoint(42.12 , 59.598):machin",
|
|
|
|
InvalidAscDescSyntax { name: S("_geoPoint(42.12 , 59.598):machin") },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"_geoPoint(42.12 , 59.598):asc:aasc",
|
|
|
|
InvalidAscDescSyntax { name: S("_geoPoint(42.12 , 59.598):asc:aasc") },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"_geoPoint(42,12 , 59,598):desc",
|
|
|
|
InvalidReservedAscDescSyntax { name: S("_geoPoint(42,12 , 59,598)") },
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"_geoPoint(35, 85, 75):asc",
|
|
|
|
InvalidReservedAscDescSyntax { name: S("_geoPoint(35, 85, 75)") },
|
|
|
|
),
|
|
|
|
("_geoPoint(18):asc", InvalidReservedAscDescSyntax { name: S("_geoPoint(18)") }),
|
2021-09-20 17:21:02 +02:00
|
|
|
];
|
|
|
|
|
2021-09-22 15:09:07 +02:00
|
|
|
for (req, expected_error) in invalid_req {
|
2021-09-20 17:21:02 +02:00
|
|
|
let res = req.parse::<AscDesc>();
|
|
|
|
assert!(
|
|
|
|
res.is_err(),
|
|
|
|
"Should no be able to parse `{}`, was expecting an error but instead got: `{:?}`",
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
);
|
2021-09-22 15:09:07 +02:00
|
|
|
let res = res.unwrap_err();
|
|
|
|
assert_eq!(
|
|
|
|
res.to_string(),
|
|
|
|
expected_error.to_string(),
|
|
|
|
"Bad error for input {}: got `{:?}` instead of `{:?}`",
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
expected_error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 = [
|
|
|
|
("words suffix", InvalidRankingRuleName { name: S("words suffix") }),
|
|
|
|
("prefix typo", InvalidRankingRuleName { name: S("prefix typo") }),
|
|
|
|
("proximity attribute", InvalidRankingRuleName { name: S("proximity attribute") }),
|
|
|
|
("price", InvalidRankingRuleName { name: S("price") }),
|
|
|
|
("asc:price", InvalidRankingRuleName { name: S("asc:price") }),
|
|
|
|
("price:deesc", InvalidRankingRuleName { name: S("price:deesc") }),
|
|
|
|
("price:aasc", InvalidRankingRuleName { name: S("price:aasc") }),
|
|
|
|
("price:asc and desc", InvalidRankingRuleName { name: S("price:asc and desc") }),
|
|
|
|
("price:asc:truc", InvalidRankingRuleName { name: S("price:asc:truc") }),
|
|
|
|
("_geo:asc", InvalidReservedRankingRuleName { name: S("_geo") }),
|
|
|
|
("_geoDistance:asc", InvalidReservedRankingRuleName { name: S("_geoDistance") }),
|
|
|
|
("_geoPoint:asc", InvalidReservedRankingRuleNameSort { name: S("_geoPoint") }),
|
|
|
|
("_geoPoint(42, 75):asc", InvalidReservedRankingRuleNameSort { name: S("_geoPoint") }),
|
|
|
|
("_geoRadius:asc", InvalidReservedRankingRuleNameFilter { name: S("_geoRadius") }),
|
|
|
|
(
|
|
|
|
"_geoRadius(42, 75, 59):asc",
|
|
|
|
InvalidReservedRankingRuleNameFilter { name: S("_geoRadius") },
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
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 17:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|