mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-27 07:14:26 +01:00
Merge #366
366: Geosearch error handling r=Kerollmops a=irevoire Rewrite most of geosearch error handling and another batch of tests on the criterion parsing. Co-authored-by: Tamo <tamo@meilisearch.com> Co-authored-by: Irevoire <tamo@meilisearch.com>
This commit is contained in:
commit
b806097141
228
milli/src/asc_desc.rs
Normal file
228
milli/src/asc_desc.rs
Normal file
@ -0,0 +1,228 @@
|
|||||||
|
//! This module provides the `AscDesc` type and defines all the errors related to this type.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::error::is_reserved_keyword;
|
||||||
|
use crate::CriterionError;
|
||||||
|
|
||||||
|
/// This error type is never supposed to be shown to the end user.
|
||||||
|
/// You must always cast it to a sort error or a criterion error.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AscDescError {
|
||||||
|
InvalidSyntax { name: String },
|
||||||
|
ReservedKeyword { name: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for AscDescError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::InvalidSyntax { name } => {
|
||||||
|
write!(f, "invalid asc/desc syntax for {}", name)
|
||||||
|
}
|
||||||
|
Self::ReservedKeyword { name } => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{} is a reserved keyword and thus can't be used as a asc/desc rule",
|
||||||
|
name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<AscDescError> for CriterionError {
|
||||||
|
fn from(error: AscDescError) -> Self {
|
||||||
|
match error {
|
||||||
|
AscDescError::InvalidSyntax { name } => CriterionError::InvalidName { name },
|
||||||
|
AscDescError::ReservedKeyword { name } if name.starts_with("_geoPoint") => {
|
||||||
|
CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() }
|
||||||
|
}
|
||||||
|
AscDescError::ReservedKeyword { name } if name.starts_with("_geoRadius") => {
|
||||||
|
CriterionError::ReservedNameForFilter { name: "_geoRadius".to_string() }
|
||||||
|
}
|
||||||
|
AscDescError::ReservedKeyword { name } => CriterionError::ReservedName { name },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||||
|
pub enum Member {
|
||||||
|
Field(String),
|
||||||
|
Geo([f64; 2]),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Member {
|
||||||
|
type Err = AscDescError;
|
||||||
|
|
||||||
|
fn from_str(text: &str) -> Result<Member, Self::Err> {
|
||||||
|
match text.strip_prefix("_geoPoint(").and_then(|text| text.strip_suffix(")")) {
|
||||||
|
Some(point) => {
|
||||||
|
let (lat, long) = point
|
||||||
|
.split_once(',')
|
||||||
|
.ok_or_else(|| AscDescError::ReservedKeyword { name: text.to_string() })
|
||||||
|
.and_then(|(lat, long)| {
|
||||||
|
lat.trim()
|
||||||
|
.parse()
|
||||||
|
.and_then(|lat| long.trim().parse().map(|long| (lat, long)))
|
||||||
|
.map_err(|_| AscDescError::ReservedKeyword { name: text.to_string() })
|
||||||
|
})?;
|
||||||
|
Ok(Member::Geo([lat, long]))
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if is_reserved_keyword(text) || text.starts_with("_geoRadius(") {
|
||||||
|
return Err(AscDescError::ReservedKeyword { name: text.to_string() })?;
|
||||||
|
}
|
||||||
|
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) => f.write_str(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)]
|
||||||
|
pub enum AscDesc {
|
||||||
|
Asc(Member),
|
||||||
|
Desc(Member),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AscDesc {
|
||||||
|
pub fn member(&self) -> &Member {
|
||||||
|
match self {
|
||||||
|
AscDesc::Asc(member) => member,
|
||||||
|
AscDesc::Desc(member) => member,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn field(&self) -> Option<&str> {
|
||||||
|
self.member().field()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for AscDesc {
|
||||||
|
type Err = AscDescError;
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
|
||||||
|
match text.rsplit_once(':') {
|
||||||
|
Some((left, "asc")) => Ok(AscDesc::Asc(left.parse()?)),
|
||||||
|
Some((left, "desc")) => Ok(AscDesc::Desc(left.parse()?)),
|
||||||
|
_ => Err(AscDescError::InvalidSyntax { name: text.to_string() }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use big_s::S;
|
||||||
|
use AscDesc::*;
|
||||||
|
use AscDescError::*;
|
||||||
|
use Member::*;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
let invalid_req = [
|
||||||
|
("truc:machin", InvalidSyntax { name: S("truc:machin") }),
|
||||||
|
("truc:deesc", InvalidSyntax { name: S("truc:deesc") }),
|
||||||
|
("truc:asc:deesc", InvalidSyntax { name: S("truc:asc:deesc") }),
|
||||||
|
("42desc", InvalidSyntax { name: S("42desc") }),
|
||||||
|
("_geoPoint:asc", ReservedKeyword { name: S("_geoPoint") }),
|
||||||
|
("_geoDistance:asc", ReservedKeyword { name: S("_geoDistance") }),
|
||||||
|
("_geoPoint(42.12 , 59.598)", InvalidSyntax { name: S("_geoPoint(42.12 , 59.598)") }),
|
||||||
|
(
|
||||||
|
"_geoPoint(42.12 , 59.598):deesc",
|
||||||
|
InvalidSyntax { name: S("_geoPoint(42.12 , 59.598):deesc") },
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"_geoPoint(42.12 , 59.598):machin",
|
||||||
|
InvalidSyntax { name: S("_geoPoint(42.12 , 59.598):machin") },
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"_geoPoint(42.12 , 59.598):asc:aasc",
|
||||||
|
InvalidSyntax { name: S("_geoPoint(42.12 , 59.598):asc:aasc") },
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"_geoPoint(42,12 , 59,598):desc",
|
||||||
|
ReservedKeyword { name: S("_geoPoint(42,12 , 59,598)") },
|
||||||
|
),
|
||||||
|
("_geoPoint(35, 85, 75):asc", ReservedKeyword { name: S("_geoPoint(35, 85, 75)") }),
|
||||||
|
("_geoPoint(18):asc", ReservedKeyword { name: S("_geoPoint(18)") }),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (req, expected_error) in invalid_req {
|
||||||
|
let res = req.parse::<AscDesc>();
|
||||||
|
assert!(
|
||||||
|
res.is_err(),
|
||||||
|
"Should no be able to parse `{}`, was expecting an error but instead got: `{:?}`",
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
);
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,49 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::error::{is_reserved_keyword, Error, UserError};
|
use crate::error::Error;
|
||||||
|
use crate::{AscDesc, Member, UserError};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum CriterionError {
|
||||||
|
InvalidName { name: String },
|
||||||
|
ReservedName { name: String },
|
||||||
|
ReservedNameForSort { name: String },
|
||||||
|
ReservedNameForFilter { name: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for CriterionError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::InvalidName { name } => write!(f, "invalid ranking rule {}", name),
|
||||||
|
Self::ReservedName { name } => {
|
||||||
|
write!(f, "{} is a reserved keyword and thus can't be used as a ranking rule", name)
|
||||||
|
}
|
||||||
|
Self::ReservedNameForSort { name } => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{} is a reserved keyword and thus can't be used as a ranking rule. \
|
||||||
|
{} can only be used for sorting at search time",
|
||||||
|
name, name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Self::ReservedNameForFilter { name } => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{} is a reserved keyword and thus can't be used as a ranking rule. \
|
||||||
|
{} can only be used for filtering at search time",
|
||||||
|
name, name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CriterionError> for Error {
|
||||||
|
fn from(error: CriterionError) -> Self {
|
||||||
|
Self::UserError(UserError::CriterionError(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
pub enum Criterion {
|
pub enum Criterion {
|
||||||
@ -39,7 +81,7 @@ impl Criterion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Criterion {
|
impl FromStr for Criterion {
|
||||||
type Err = Error;
|
type Err = CriterionError;
|
||||||
|
|
||||||
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
fn from_str(text: &str) -> Result<Criterion, Self::Err> {
|
||||||
match text {
|
match text {
|
||||||
@ -49,118 +91,17 @@ impl FromStr for Criterion {
|
|||||||
"attribute" => Ok(Criterion::Attribute),
|
"attribute" => Ok(Criterion::Attribute),
|
||||||
"sort" => Ok(Criterion::Sort),
|
"sort" => Ok(Criterion::Sort),
|
||||||
"exactness" => Ok(Criterion::Exactness),
|
"exactness" => Ok(Criterion::Exactness),
|
||||||
text => match AscDesc::from_str(text) {
|
text => match AscDesc::from_str(text)? {
|
||||||
Ok(AscDesc::Asc(Member::Field(field))) => Ok(Criterion::Asc(field)),
|
AscDesc::Asc(Member::Field(field)) => Ok(Criterion::Asc(field)),
|
||||||
Ok(AscDesc::Desc(Member::Field(field))) => Ok(Criterion::Desc(field)),
|
AscDesc::Desc(Member::Field(field)) => Ok(Criterion::Desc(field)),
|
||||||
Ok(AscDesc::Asc(Member::Geo(_))) | Ok(AscDesc::Desc(Member::Geo(_))) => {
|
AscDesc::Asc(Member::Geo(_)) | AscDesc::Desc(Member::Geo(_)) => {
|
||||||
Err(UserError::InvalidRankingRuleName { name: text.to_string() })?
|
Err(CriterionError::ReservedNameForSort { name: "_geoPoint".to_string() })?
|
||||||
}
|
|
||||||
Err(UserError::InvalidAscDescSyntax { name }) => {
|
|
||||||
Err(UserError::InvalidRankingRuleName { name }.into())
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
Err(UserError::InvalidRankingRuleName { name: error.to_string() }.into())
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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> {
|
|
||||||
match text.strip_prefix("_geoPoint(").and_then(|text| text.strip_suffix(")")) {
|
|
||||||
Some(point) => {
|
|
||||||
let (lat, long) = point
|
|
||||||
.split_once(',')
|
|
||||||
.ok_or_else(|| UserError::InvalidRankingRuleName { name: text.to_string() })
|
|
||||||
.and_then(|(lat, long)| {
|
|
||||||
lat.trim()
|
|
||||||
.parse()
|
|
||||||
.and_then(|lat| long.trim().parse().map(|long| (lat, long)))
|
|
||||||
.map_err(|_| UserError::InvalidRankingRuleName {
|
|
||||||
name: text.to_string(),
|
|
||||||
})
|
|
||||||
})?;
|
|
||||||
Ok(Member::Geo([lat, long]))
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
if is_reserved_keyword(text) {
|
|
||||||
return Err(UserError::InvalidReservedRankingRuleName {
|
|
||||||
name: text.to_string(),
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
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) => f.write_str(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)]
|
|
||||||
pub enum AscDesc {
|
|
||||||
Asc(Member),
|
|
||||||
Desc(Member),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AscDesc {
|
|
||||||
pub fn member(&self) -> &Member {
|
|
||||||
match self {
|
|
||||||
AscDesc::Asc(member) => member,
|
|
||||||
AscDesc::Desc(member) => member,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn field(&self) -> Option<&str> {
|
|
||||||
self.member().field()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for AscDesc {
|
|
||||||
type Err = UserError;
|
|
||||||
|
|
||||||
/// 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
|
|
||||||
fn from_str(text: &str) -> Result<AscDesc, Self::Err> {
|
|
||||||
match text.rsplit_once(':') {
|
|
||||||
Some((left, "asc")) => Ok(AscDesc::Asc(left.parse()?)),
|
|
||||||
Some((left, "desc")) => Ok(AscDesc::Desc(left.parse()?)),
|
|
||||||
_ => Err(UserError::InvalidAscDescSyntax { name: text.to_string() }),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn default_criteria() -> Vec<Criterion> {
|
pub fn default_criteria() -> Vec<Criterion> {
|
||||||
vec![
|
vec![
|
||||||
Criterion::Words,
|
Criterion::Words,
|
||||||
@ -191,59 +132,74 @@ impl fmt::Display for Criterion {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use big_s::S;
|
||||||
|
use CriterionError::*;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_asc_desc() {
|
fn parse_criterion() {
|
||||||
use big_s::S;
|
let valid_criteria = [
|
||||||
use AscDesc::*;
|
("words", Criterion::Words),
|
||||||
use Member::*;
|
("typo", Criterion::Typo),
|
||||||
|
("proximity", Criterion::Proximity),
|
||||||
let valid_req = [
|
("attribute", Criterion::Attribute),
|
||||||
("truc:asc", Asc(Field(S("truc")))),
|
("sort", Criterion::Sort),
|
||||||
("bidule:desc", Desc(Field(S("bidule")))),
|
("exactness", Criterion::Exactness),
|
||||||
("a-b:desc", Desc(Field(S("a-b")))),
|
("price:asc", Criterion::Asc(S("price"))),
|
||||||
("a:b:desc", Desc(Field(S("a:b")))),
|
("price:desc", Criterion::Desc(S("price"))),
|
||||||
("a12:asc", Asc(Field(S("a12")))),
|
("price:asc:desc", Criterion::Desc(S("price:asc"))),
|
||||||
("42:asc", Asc(Field(S("42")))),
|
("truc:machin:desc", Criterion::Desc(S("truc:machin"))),
|
||||||
("_geoPoint(42, 59):asc", Asc(Geo([42., 59.]))),
|
("hello-world!:desc", Criterion::Desc(S("hello-world!"))),
|
||||||
("_geoPoint(42.459, 59):desc", Desc(Geo([42.459, 59.]))),
|
("it's spacy over there:asc", Criterion::Asc(S("it's spacy over there"))),
|
||||||
("_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 {
|
for (input, expected) in valid_criteria {
|
||||||
let res = req.parse();
|
let res = input.parse::<Criterion>();
|
||||||
assert!(res.is_ok(), "Failed to parse `{}`, was expecting `{:?}`", req, expected);
|
assert!(
|
||||||
assert_eq!(expected, res.unwrap());
|
res.is_ok(),
|
||||||
|
"Failed to parse `{}`, was expecting `{:?}` but instead got `{:?}`",
|
||||||
|
input,
|
||||||
|
expected,
|
||||||
|
res
|
||||||
|
);
|
||||||
|
assert_eq!(res.unwrap(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
let invalid_req = [
|
let invalid_criteria = [
|
||||||
"truc:machin",
|
("words suffix", InvalidName { name: S("words suffix") }),
|
||||||
"truc:deesc",
|
("prefix typo", InvalidName { name: S("prefix typo") }),
|
||||||
"truc:asc:deesc",
|
("proximity attribute", InvalidName { name: S("proximity attribute") }),
|
||||||
"42desc",
|
("price", InvalidName { name: S("price") }),
|
||||||
"_geoPoint:asc",
|
("asc:price", InvalidName { name: S("asc:price") }),
|
||||||
"_geoDistance:asc",
|
("price:deesc", InvalidName { name: S("price:deesc") }),
|
||||||
"_geoPoint(42.12 , 59.598)",
|
("price:aasc", InvalidName { name: S("price:aasc") }),
|
||||||
"_geoPoint(42.12 , 59.598):deesc",
|
("price:asc and desc", InvalidName { name: S("price:asc and desc") }),
|
||||||
"_geoPoint(42.12 , 59.598):machin",
|
("price:asc:truc", InvalidName { name: S("price:asc:truc") }),
|
||||||
"_geoPoint(42.12 , 59.598):asc:aasc",
|
("_geo:asc", ReservedName { name: S("_geo") }),
|
||||||
"_geoPoint(42,12 , 59,598):desc",
|
("_geoDistance:asc", ReservedName { name: S("_geoDistance") }),
|
||||||
"_geoPoint(35, 85, 75):asc",
|
("_geoPoint:asc", ReservedNameForSort { name: S("_geoPoint") }),
|
||||||
"_geoPoint(18):asc",
|
("_geoPoint(42, 75):asc", ReservedNameForSort { name: S("_geoPoint") }),
|
||||||
|
("_geoRadius:asc", ReservedNameForFilter { name: S("_geoRadius") }),
|
||||||
|
("_geoRadius(42, 75, 59):asc", ReservedNameForFilter { name: S("_geoRadius") }),
|
||||||
];
|
];
|
||||||
|
|
||||||
for req in invalid_req {
|
for (input, expected) in invalid_criteria {
|
||||||
let res = req.parse::<AscDesc>();
|
let res = input.parse::<Criterion>();
|
||||||
assert!(
|
assert!(
|
||||||
res.is_err(),
|
res.is_err(),
|
||||||
"Should no be able to parse `{}`, was expecting an error but instead got: `{:?}`",
|
"Should no be able to parse `{}`, was expecting an error but instead got: `{:?}`",
|
||||||
req,
|
input,
|
||||||
|
res
|
||||||
|
);
|
||||||
|
let res = res.unwrap_err();
|
||||||
|
assert_eq!(
|
||||||
|
res.to_string(),
|
||||||
|
expected.to_string(),
|
||||||
|
"Bad error for input {}: got `{:?}` instead of `{:?}`",
|
||||||
|
input,
|
||||||
res,
|
res,
|
||||||
|
expected
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ use rayon::ThreadPoolBuildError;
|
|||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
|
|
||||||
use crate::search::ParserRule;
|
use crate::search::ParserRule;
|
||||||
use crate::{DocumentId, FieldId};
|
use crate::{CriterionError, DocumentId, FieldId};
|
||||||
|
|
||||||
pub type Object = Map<String, Value>;
|
pub type Object = Map<String, Value>;
|
||||||
|
|
||||||
@ -55,16 +55,15 @@ pub enum FieldIdMapMissingEntry {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum UserError {
|
pub enum UserError {
|
||||||
AttributeLimitReached,
|
AttributeLimitReached,
|
||||||
|
CriterionError(CriterionError),
|
||||||
DocumentLimitReached,
|
DocumentLimitReached,
|
||||||
InvalidAscDescSyntax { name: String },
|
|
||||||
InvalidDocumentId { document_id: Value },
|
InvalidDocumentId { document_id: Value },
|
||||||
InvalidFacetsDistribution { invalid_facets_name: HashSet<String> },
|
InvalidFacetsDistribution { invalid_facets_name: HashSet<String> },
|
||||||
InvalidFilter(pest::error::Error<ParserRule>),
|
InvalidFilter(pest::error::Error<ParserRule>),
|
||||||
InvalidFilterAttribute(pest::error::Error<ParserRule>),
|
InvalidFilterAttribute(pest::error::Error<ParserRule>),
|
||||||
InvalidSortName { name: String },
|
InvalidSortName { name: String },
|
||||||
|
InvalidReservedSortName { name: String },
|
||||||
InvalidGeoField { document_id: Value, object: Value },
|
InvalidGeoField { document_id: Value, object: Value },
|
||||||
InvalidRankingRuleName { name: String },
|
|
||||||
InvalidReservedRankingRuleName { name: String },
|
|
||||||
InvalidSortableAttribute { field: String, valid_fields: HashSet<String> },
|
InvalidSortableAttribute { field: String, valid_fields: HashSet<String> },
|
||||||
SortRankingRuleMissing,
|
SortRankingRuleMissing,
|
||||||
InvalidStoreFile,
|
InvalidStoreFile,
|
||||||
@ -211,6 +210,7 @@ impl fmt::Display for UserError {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::AttributeLimitReached => f.write_str("maximum number of attributes reached"),
|
Self::AttributeLimitReached => f.write_str("maximum number of attributes reached"),
|
||||||
|
Self::CriterionError(error) => write!(f, "{}", error),
|
||||||
Self::DocumentLimitReached => f.write_str("maximum number of documents reached"),
|
Self::DocumentLimitReached => f.write_str("maximum number of documents reached"),
|
||||||
Self::InvalidFacetsDistribution { invalid_facets_name } => {
|
Self::InvalidFacetsDistribution { invalid_facets_name } => {
|
||||||
let name_list =
|
let name_list =
|
||||||
@ -222,17 +222,17 @@ impl fmt::Display for UserError {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
Self::InvalidFilter(error) => error.fmt(f),
|
Self::InvalidFilter(error) => error.fmt(f),
|
||||||
Self::InvalidAscDescSyntax { name } => {
|
|
||||||
write!(f, "invalid asc/desc syntax for {}", name)
|
|
||||||
}
|
|
||||||
Self::InvalidGeoField { document_id, object } => write!(
|
Self::InvalidGeoField { document_id, object } => write!(
|
||||||
f,
|
f,
|
||||||
"the document with the id: {} contains an invalid _geo field: {}",
|
"the document with the id: {} contains an invalid _geo field: {}",
|
||||||
document_id, object
|
document_id, object
|
||||||
),
|
),
|
||||||
Self::InvalidRankingRuleName { name } => write!(f, "invalid criterion {}", name),
|
Self::InvalidReservedSortName { name } => {
|
||||||
Self::InvalidReservedRankingRuleName { name } => {
|
write!(
|
||||||
write!(f, "{} is a reserved keyword and thus can't be used as a ranking rule", name)
|
f,
|
||||||
|
"{} is a reserved keyword and thus can't be used as a sort expression",
|
||||||
|
name
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Self::InvalidDocumentId { document_id } => {
|
Self::InvalidDocumentId { document_id } => {
|
||||||
let json = serde_json::to_string(document_id).unwrap();
|
let json = serde_json::to_string(document_id).unwrap();
|
||||||
|
@ -4,6 +4,7 @@ extern crate pest_derive;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod documents;
|
pub mod documents;
|
||||||
|
|
||||||
|
mod asc_desc;
|
||||||
mod criterion;
|
mod criterion;
|
||||||
mod error;
|
mod error;
|
||||||
mod external_documents_ids;
|
mod external_documents_ids;
|
||||||
@ -24,7 +25,8 @@ use fxhash::{FxHasher32, FxHasher64};
|
|||||||
pub use grenad::CompressionType;
|
pub use grenad::CompressionType;
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
|
|
||||||
pub use self::criterion::{default_criteria, AscDesc, Criterion, Member};
|
pub use self::asc_desc::{AscDesc, AscDescError, Member};
|
||||||
|
pub use self::criterion::{default_criteria, Criterion, CriterionError};
|
||||||
pub use self::error::{
|
pub use self::error::{
|
||||||
Error, FieldIdMapMissingEntry, InternalError, SerializationError, UserError,
|
Error, FieldIdMapMissingEntry, InternalError, SerializationError, UserError,
|
||||||
};
|
};
|
||||||
|
@ -12,10 +12,9 @@ use self::r#final::Final;
|
|||||||
use self::typo::Typo;
|
use self::typo::Typo;
|
||||||
use self::words::Words;
|
use self::words::Words;
|
||||||
use super::query_tree::{Operation, PrimitiveQueryPart, Query, QueryKind};
|
use super::query_tree::{Operation, PrimitiveQueryPart, Query, QueryKind};
|
||||||
use crate::criterion::{AscDesc as AscDescName, Member};
|
|
||||||
use crate::search::criteria::geo::Geo;
|
use crate::search::criteria::geo::Geo;
|
||||||
use crate::search::{word_derivations, WordDerivationsCache};
|
use crate::search::{word_derivations, WordDerivationsCache};
|
||||||
use crate::{DocumentId, FieldId, Index, Result, TreeLevel};
|
use crate::{AscDesc as AscDescName, DocumentId, FieldId, Index, Member, Result, TreeLevel};
|
||||||
|
|
||||||
mod asc_desc;
|
mod asc_desc;
|
||||||
mod attribute;
|
mod attribute;
|
||||||
|
@ -18,10 +18,9 @@ pub(crate) use self::facet::ParserRule;
|
|||||||
pub use self::facet::{FacetDistribution, FacetNumberIter, FilterCondition, Operator};
|
pub use self::facet::{FacetDistribution, FacetNumberIter, FilterCondition, Operator};
|
||||||
pub use self::matching_words::MatchingWords;
|
pub use self::matching_words::MatchingWords;
|
||||||
use self::query_tree::QueryTreeBuilder;
|
use self::query_tree::QueryTreeBuilder;
|
||||||
use crate::criterion::{AscDesc, Criterion};
|
|
||||||
use crate::error::UserError;
|
use crate::error::UserError;
|
||||||
use crate::search::criteria::r#final::{Final, FinalResult};
|
use crate::search::criteria::r#final::{Final, FinalResult};
|
||||||
use crate::{DocumentId, Index, Result};
|
use crate::{AscDesc, Criterion, DocumentId, Index, Member, Result};
|
||||||
|
|
||||||
// Building these factories is not free.
|
// Building these factories is not free.
|
||||||
static LEVDIST0: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(0, true));
|
static LEVDIST0: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(0, true));
|
||||||
@ -148,15 +147,20 @@ impl<'a> Search<'a> {
|
|||||||
if let Some(sort_criteria) = &self.sort_criteria {
|
if let Some(sort_criteria) = &self.sort_criteria {
|
||||||
let sortable_fields = self.index.sortable_fields(self.rtxn)?;
|
let sortable_fields = self.index.sortable_fields(self.rtxn)?;
|
||||||
for asc_desc in sort_criteria {
|
for asc_desc in sort_criteria {
|
||||||
// we are not supposed to find any geoPoint in the criterion
|
match asc_desc.member() {
|
||||||
if let Some(field) = asc_desc.field() {
|
Member::Field(ref field) if !sortable_fields.contains(field) => {
|
||||||
if !sortable_fields.contains(field) {
|
|
||||||
return Err(UserError::InvalidSortableAttribute {
|
return Err(UserError::InvalidSortableAttribute {
|
||||||
field: field.to_string(),
|
field: field.to_string(),
|
||||||
valid_fields: sortable_fields,
|
valid_fields: sortable_fields,
|
||||||
}
|
})?
|
||||||
.into());
|
|
||||||
}
|
}
|
||||||
|
Member::Geo(_) if !sortable_fields.contains("_geo") => {
|
||||||
|
return Err(UserError::InvalidSortableAttribute {
|
||||||
|
field: "_geo".to_string(),
|
||||||
|
valid_fields: sortable_fields,
|
||||||
|
})?
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user