mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 10:37:41 +08:00
Implements the EXIST filter operator
This commit is contained in:
parent
a8641b42a7
commit
72452f0cb2
@ -8,7 +8,7 @@
|
|||||||
use nom::branch::alt;
|
use nom::branch::alt;
|
||||||
use nom::bytes::complete::tag;
|
use nom::bytes::complete::tag;
|
||||||
use nom::combinator::cut;
|
use nom::combinator::cut;
|
||||||
use nom::sequence::tuple;
|
use nom::sequence::{terminated, tuple};
|
||||||
use Condition::*;
|
use Condition::*;
|
||||||
|
|
||||||
use crate::{parse_value, FilterCondition, IResult, Span, Token};
|
use crate::{parse_value, FilterCondition, IResult, Span, Token};
|
||||||
@ -19,6 +19,8 @@ pub enum Condition<'a> {
|
|||||||
GreaterThanOrEqual(Token<'a>),
|
GreaterThanOrEqual(Token<'a>),
|
||||||
Equal(Token<'a>),
|
Equal(Token<'a>),
|
||||||
NotEqual(Token<'a>),
|
NotEqual(Token<'a>),
|
||||||
|
Exist,
|
||||||
|
NotExist,
|
||||||
LowerThan(Token<'a>),
|
LowerThan(Token<'a>),
|
||||||
LowerThanOrEqual(Token<'a>),
|
LowerThanOrEqual(Token<'a>),
|
||||||
Between { from: Token<'a>, to: Token<'a> },
|
Between { from: Token<'a>, to: Token<'a> },
|
||||||
@ -33,6 +35,8 @@ impl<'a> Condition<'a> {
|
|||||||
GreaterThanOrEqual(n) => (LowerThan(n), None),
|
GreaterThanOrEqual(n) => (LowerThan(n), None),
|
||||||
Equal(s) => (NotEqual(s), None),
|
Equal(s) => (NotEqual(s), None),
|
||||||
NotEqual(s) => (Equal(s), None),
|
NotEqual(s) => (Equal(s), None),
|
||||||
|
Exist => (NotExist, None),
|
||||||
|
NotExist => (Exist, None),
|
||||||
LowerThan(n) => (GreaterThanOrEqual(n), None),
|
LowerThan(n) => (GreaterThanOrEqual(n), None),
|
||||||
LowerThanOrEqual(n) => (GreaterThan(n), None),
|
LowerThanOrEqual(n) => (GreaterThan(n), None),
|
||||||
Between { from, to } => (LowerThan(from), Some(GreaterThan(to))),
|
Between { from, to } => (LowerThan(from), Some(GreaterThan(to))),
|
||||||
@ -58,6 +62,13 @@ pub fn parse_condition(input: Span) -> IResult<FilterCondition> {
|
|||||||
Ok((input, condition))
|
Ok((input, condition))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// exist = value EXIST
|
||||||
|
pub fn parse_exist(input: Span) -> IResult<FilterCondition> {
|
||||||
|
let (input, key) = terminated(parse_value, tag("EXIST"))(input)?;
|
||||||
|
|
||||||
|
Ok((input, FilterCondition::Condition { fid: key.into(), op: Exist }))
|
||||||
|
}
|
||||||
|
|
||||||
/// to = value value TO value
|
/// to = value value TO value
|
||||||
pub fn parse_to(input: Span) -> IResult<FilterCondition> {
|
pub fn parse_to(input: Span) -> IResult<FilterCondition> {
|
||||||
let (input, (key, from, _, to)) =
|
let (input, (key, from, _, to)) =
|
||||||
|
@ -128,10 +128,10 @@ impl<'a> Display for Error<'a> {
|
|||||||
writeln!(f, "Was expecting a value but instead got `{}`.", escaped_input)?
|
writeln!(f, "Was expecting a value but instead got `{}`.", escaped_input)?
|
||||||
}
|
}
|
||||||
ErrorKind::InvalidPrimary if input.trim().is_empty() => {
|
ErrorKind::InvalidPrimary if input.trim().is_empty() => {
|
||||||
writeln!(f, "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` but instead got nothing.")?
|
writeln!(f, "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` but instead got nothing.")?
|
||||||
}
|
}
|
||||||
ErrorKind::InvalidPrimary => {
|
ErrorKind::InvalidPrimary => {
|
||||||
writeln!(f, "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` at `{}`.", escaped_input)?
|
writeln!(f, "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` at `{}`.", escaped_input)?
|
||||||
}
|
}
|
||||||
ErrorKind::ExpectedEof => {
|
ErrorKind::ExpectedEof => {
|
||||||
writeln!(f, "Found unexpected characters at the end of the filter: `{}`. You probably forgot an `OR` or an `AND` rule.", escaped_input)?
|
writeln!(f, "Found unexpected characters at the end of the filter: `{}`. You probably forgot an `OR` or an `AND` rule.", escaped_input)?
|
||||||
|
@ -6,8 +6,9 @@
|
|||||||
//! or = and (~ "OR" ~ and)
|
//! or = and (~ "OR" ~ and)
|
||||||
//! and = not (~ "AND" not)*
|
//! and = not (~ "AND" not)*
|
||||||
//! not = ("NOT" ~ not) | primary
|
//! not = ("NOT" ~ not) | primary
|
||||||
//! primary = (WS* ~ "(" expression ")" ~ WS*) | geoRadius | condition | to
|
//! primary = (WS* ~ "(" expression ")" ~ WS*) | geoRadius | condition | exist | to
|
||||||
//! condition = value ("==" | ">" ...) value
|
//! condition = value ("==" | ">" ...) value
|
||||||
|
//! exist = value EXIST
|
||||||
//! to = value value TO value
|
//! to = value value TO value
|
||||||
//! value = WS* ~ ( word | singleQuoted | doubleQuoted) ~ WS*
|
//! value = WS* ~ ( word | singleQuoted | doubleQuoted) ~ WS*
|
||||||
//! singleQuoted = "'" .* all but quotes "'"
|
//! singleQuoted = "'" .* all but quotes "'"
|
||||||
@ -42,6 +43,7 @@ mod value;
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use condition::parse_exist;
|
||||||
pub use condition::{parse_condition, parse_to, Condition};
|
pub use condition::{parse_condition, parse_to, Condition};
|
||||||
use error::{cut_with_err, NomErrorExt};
|
use error::{cut_with_err, NomErrorExt};
|
||||||
pub use error::{Error, ErrorKind};
|
pub use error::{Error, ErrorKind};
|
||||||
@ -248,6 +250,7 @@ fn parse_primary(input: Span) -> IResult<FilterCondition> {
|
|||||||
),
|
),
|
||||||
parse_geo_radius,
|
parse_geo_radius,
|
||||||
parse_condition,
|
parse_condition,
|
||||||
|
parse_exist,
|
||||||
parse_to,
|
parse_to,
|
||||||
// the next lines are only for error handling and are written at the end to have the less possible performance impact
|
// the next lines are only for error handling and are written at the end to have the less possible performance impact
|
||||||
parse_geo_point,
|
parse_geo_point,
|
||||||
@ -420,6 +423,20 @@ pub mod tests {
|
|||||||
op: Condition::LowerThan(rtok("NOT subscribers >= ", "1000")),
|
op: Condition::LowerThan(rtok("NOT subscribers >= ", "1000")),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"subscribers EXIST",
|
||||||
|
Fc::Condition {
|
||||||
|
fid: rtok("", "subscribers"),
|
||||||
|
op: Condition::Exist,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"NOT subscribers EXIST",
|
||||||
|
Fc::Condition {
|
||||||
|
fid: rtok("NOT ", "subscribers"),
|
||||||
|
op: Condition::NotExist,
|
||||||
|
},
|
||||||
|
),
|
||||||
(
|
(
|
||||||
"subscribers 100 TO 1000",
|
"subscribers 100 TO 1000",
|
||||||
Fc::Condition {
|
Fc::Condition {
|
||||||
@ -577,10 +594,10 @@ pub mod tests {
|
|||||||
("channel = ", "Was expecting a value but instead got nothing."),
|
("channel = ", "Was expecting a value but instead got nothing."),
|
||||||
("channel = 🐻", "Was expecting a value but instead got `🐻`."),
|
("channel = 🐻", "Was expecting a value but instead got `🐻`."),
|
||||||
("channel = 🐻 AND followers < 100", "Was expecting a value but instead got `🐻`."),
|
("channel = 🐻 AND followers < 100", "Was expecting a value but instead got `🐻`."),
|
||||||
("OR", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` at `OR`."),
|
("OR", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` at `OR`."),
|
||||||
("AND", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` at `AND`."),
|
("AND", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` at `AND`."),
|
||||||
("channel Ponce", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` at `channel Ponce`."),
|
("channel Ponce", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` at `channel Ponce`."),
|
||||||
("channel = Ponce OR", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO` or `_geoRadius` but instead got nothing."),
|
("channel = Ponce OR", "Was expecting an operation `=`, `!=`, `>=`, `>`, `<=`, `<`, `TO`, `EXIST`, or `_geoRadius` but instead got nothing."),
|
||||||
("_geoRadius", "The `_geoRadius` filter expects three arguments: `_geoRadius(latitude, longitude, radius)`."),
|
("_geoRadius", "The `_geoRadius` filter expects three arguments: `_geoRadius(latitude, longitude, radius)`."),
|
||||||
("_geoRadius = 12", "The `_geoRadius` filter expects three arguments: `_geoRadius(latitude, longitude, radius)`."),
|
("_geoRadius = 12", "The `_geoRadius` filter expects three arguments: `_geoRadius(latitude, longitude, radius)`."),
|
||||||
("_geoPoint(12, 13, 14)", "`_geoPoint` is a reserved keyword and thus can't be used as a filter expression. Use the `_geoRadius(latitude, longitude, distance) built-in rule to filter on `_geo` coordinates."),
|
("_geoPoint(12, 13, 14)", "`_geoPoint` is a reserved keyword and thus can't be used as a filter expression. Use the `_geoRadius(latitude, longitude, distance) built-in rule to filter on `_geo` coordinates."),
|
||||||
|
@ -280,6 +280,25 @@ impl<'a> Filter<'a> {
|
|||||||
Condition::LowerThan(val) => (Included(f64::MIN), Excluded(val.parse()?)),
|
Condition::LowerThan(val) => (Included(f64::MIN), Excluded(val.parse()?)),
|
||||||
Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)),
|
Condition::LowerThanOrEqual(val) => (Included(f64::MIN), Included(val.parse()?)),
|
||||||
Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)),
|
Condition::Between { from, to } => (Included(from.parse()?), Included(to.parse()?)),
|
||||||
|
Condition::Exist => {
|
||||||
|
let exist = index.exists_faceted_documents_ids(rtxn, field_id)?;
|
||||||
|
return Ok(exist);
|
||||||
|
}
|
||||||
|
Condition::NotExist => {
|
||||||
|
let all_ids = index.documents_ids(rtxn)?;
|
||||||
|
|
||||||
|
let exist = Self::evaluate_operator(
|
||||||
|
rtxn,
|
||||||
|
index,
|
||||||
|
numbers_db,
|
||||||
|
strings_db,
|
||||||
|
field_id,
|
||||||
|
&Condition::Exist,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let notexist = all_ids - exist;
|
||||||
|
return Ok(notexist);
|
||||||
|
}
|
||||||
Condition::Equal(val) => {
|
Condition::Equal(val) => {
|
||||||
let (_original_value, string_docids) = strings_db
|
let (_original_value, string_docids) = strings_db
|
||||||
.get(rtxn, &(field_id, &val.value().to_lowercase()))?
|
.get(rtxn, &(field_id, &val.value().to_lowercase()))?
|
||||||
|
Loading…
Reference in New Issue
Block a user