mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-27 04:25:06 +08:00
introduce the reserved keywords in the filters
This commit is contained in:
parent
6d5762a6c8
commit
7ae2a7341c
@ -595,6 +595,18 @@ fn field_id(
|
|||||||
) -> StdResult<Option<FieldId>, PestError<Rule>> {
|
) -> StdResult<Option<FieldId>, PestError<Rule>> {
|
||||||
// lexing ensures that we at least have a key
|
// lexing ensures that we at least have a key
|
||||||
let key = items.next().unwrap();
|
let key = items.next().unwrap();
|
||||||
|
if key.as_rule() == Rule::reserved {
|
||||||
|
return Err(PestError::new_from_span(
|
||||||
|
ErrorVariant::CustomError {
|
||||||
|
message: format!(
|
||||||
|
"`{}` is a reserved keyword and thus can't be used as a filter expression. Available filterable attributes are: {}",
|
||||||
|
key.as_str(),
|
||||||
|
filterable_fields.iter().join(", "),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
key.as_span(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if !filterable_fields.contains(key.as_str()) {
|
if !filterable_fields.contains(key.as_str()) {
|
||||||
return Err(PestError::new_from_span(
|
return Err(PestError::new_from_span(
|
||||||
@ -671,6 +683,13 @@ mod tests {
|
|||||||
let condition = FilterCondition::from_str(&rtxn, &index, "NOT channel = ponce").unwrap();
|
let condition = FilterCondition::from_str(&rtxn, &index, "NOT channel = ponce").unwrap();
|
||||||
let expected = Operator(0, Operator::NotEqual(None, S("ponce")));
|
let expected = Operator(0, Operator::NotEqual(None, S("ponce")));
|
||||||
assert_eq!(condition, expected);
|
assert_eq!(condition, expected);
|
||||||
|
|
||||||
|
let result = FilterCondition::from_str(&rtxn, &index, "_geo = France");
|
||||||
|
assert!(result.is_err());
|
||||||
|
let error = result.unwrap_err();
|
||||||
|
assert!(error.to_string().contains(
|
||||||
|
"`_geo` is a reserved keyword and thus can't be used as a filter expression."
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
key = _{quoted | word}
|
key = _{reserved | quoted | word }
|
||||||
value = _{quoted | word}
|
value = _{quoted | word }
|
||||||
quoted = _{ (PUSH("'") | PUSH("\"")) ~ string ~ POP }
|
quoted = _{ (PUSH("'") | PUSH("\"")) ~ string ~ POP }
|
||||||
string = {char*}
|
string = {char*}
|
||||||
word = ${(LETTER | NUMBER | "_" | "-" | ".")+}
|
word = ${(LETTER | NUMBER | "_" | "-" | ".")+}
|
||||||
@ -8,8 +8,9 @@ char = _{ !(PEEK | "\\") ~ ANY
|
|||||||
| "\\" ~ (PEEK | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
|
| "\\" ~ (PEEK | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
|
||||||
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})}
|
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})}
|
||||||
|
|
||||||
|
reserved = { "_geo" | "_geoDistance" | "_geoPoint" | ("_geoPoint" ~ parameters) }
|
||||||
// we deliberately choose to allow empty parameters to generate more specific error message later
|
// we deliberately choose to allow empty parameters to generate more specific error message later
|
||||||
parameters ={("(" ~ (value ~ ",")* ~ value? ~ ")") | ""}
|
parameters = {("(" ~ (value ~ ",")* ~ value? ~ ")") | ""}
|
||||||
condition = _{between | eq | greater | less | geq | leq | neq}
|
condition = _{between | eq | greater | less | geq | leq | neq}
|
||||||
between = {key ~ value ~ "TO" ~ value}
|
between = {key ~ value ~ "TO" ~ value}
|
||||||
geq = {key ~ ">=" ~ value}
|
geq = {key ~ ">=" ~ value}
|
||||||
|
Loading…
Reference in New Issue
Block a user