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};
|
2020-11-27 19:14:56 +08:00
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
use crate::error::{Error, UserError};
|
|
|
|
|
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-06-14 22:46:19 +08:00
|
|
|
type Err = Error;
|
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-08-23 17:37:18 +08:00
|
|
|
text => match AscDesc::from_str(text) {
|
|
|
|
Ok(AscDesc::Asc(field)) => Ok(Criterion::Asc(field)),
|
|
|
|
Ok(AscDesc::Desc(field)) => Ok(Criterion::Desc(field)),
|
2021-09-07 17:41:25 +08:00
|
|
|
Err(UserError::InvalidAscDescSyntax { name }) => {
|
|
|
|
Err(UserError::InvalidCriterionName { name }.into())
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
Err(UserError::InvalidCriterionName { name: error.to_string() }.into())
|
|
|
|
}
|
2021-08-31 00:22:52 +08:00
|
|
|
Ok(AscDesc::Asc(Member::Geo(_))) | Ok(AscDesc::Desc(Member::Geo(_))) => {
|
|
|
|
Err(UserError::AttributeLimitReached)? // TODO: TAMO: use a real error
|
|
|
|
}
|
|
|
|
Err(error) => Err(error.into()),
|
2021-08-23 17:37:18 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 00:22:52 +08: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> {
|
|
|
|
if text.starts_with("_geoPoint(") {
|
|
|
|
let point =
|
|
|
|
text.strip_prefix("_geoPoint(")
|
|
|
|
.and_then(|point| point.strip_suffix(")"))
|
|
|
|
.ok_or_else(|| UserError::InvalidCriterionName { name: text.to_string() })?;
|
|
|
|
let point = point
|
|
|
|
.split(',')
|
2021-09-01 00:48:40 +08:00
|
|
|
.map(|el| el.trim()).parse()
|
2021-08-31 00:22:52 +08:00
|
|
|
.collect::<Result<Vec<f64>, _>>()
|
|
|
|
.map_err(|_| UserError::InvalidCriterionName { name: text.to_string() })?;
|
|
|
|
Ok(Member::Geo([point[0], point[1]]))
|
|
|
|
} else {
|
|
|
|
Ok(Member::Field(text.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Member {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Member::Field(name) => write!(f, "{}", name),
|
|
|
|
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 17:37:18 +08:00
|
|
|
pub enum AscDesc {
|
2021-08-31 00:22:52 +08:00
|
|
|
Asc(Member),
|
|
|
|
Desc(Member),
|
2021-08-23 17:37:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AscDesc {
|
2021-08-31 00:22:52 +08:00
|
|
|
pub fn member(&self) -> &Member {
|
2021-08-23 17:37:18 +08:00
|
|
|
match self {
|
2021-08-31 00:22:52 +08:00
|
|
|
AscDesc::Asc(member) => member,
|
|
|
|
AscDesc::Desc(member) => member,
|
2021-08-23 17:37:18 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 00:22:52 +08:00
|
|
|
|
|
|
|
pub fn field(&self) -> Option<&str> {
|
|
|
|
self.member().field()
|
|
|
|
}
|
2021-08-23 17:37:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for AscDesc {
|
|
|
|
type Err = UserError;
|
|
|
|
|
2021-09-07 17:41:25 +08: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 17:37:18 +08:00
|
|
|
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
|
|
|
|
match text.rsplit_once(':') {
|
2021-08-31 00:22:52 +08:00
|
|
|
Some((left, "asc")) => Ok(AscDesc::Asc(left.parse()?)),
|
|
|
|
Some((left, "desc")) => Ok(AscDesc::Desc(left.parse()?)),
|
|
|
|
_ => Err(UserError::InvalidCriterionName { name: text.to_string() }),
|
2021-08-17 21:05:00 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|