2021-05-03 12:51:33 +02:00
|
|
|
use std::collections::HashSet;
|
2020-11-26 20:42:54 +01:00
|
|
|
use std::fmt::Debug;
|
2021-06-16 18:33:33 +02:00
|
|
|
use std::ops::Bound::{self, Excluded, Included};
|
2021-06-14 16:46:19 +02:00
|
|
|
use std::result::Result as StdResult;
|
2020-11-26 20:42:54 +01:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-01-07 10:17:27 +01:00
|
|
|
use either::Either;
|
2021-04-07 11:57:16 +02:00
|
|
|
use heed::types::DecodeIgnore;
|
2020-11-26 20:42:54 +01:00
|
|
|
use log::debug;
|
|
|
|
use pest::error::{Error as PestError, ErrorVariant};
|
|
|
|
use pest::iterators::{Pair, Pairs};
|
|
|
|
use pest::Parser;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
use self::FilterCondition::*;
|
2021-05-03 11:45:45 +02:00
|
|
|
use self::Operator::*;
|
2021-06-16 18:33:33 +02:00
|
|
|
use super::parser::{FilterParser, Rule, PREC_CLIMBER};
|
|
|
|
use super::FacetRange;
|
|
|
|
use crate::error::UserError;
|
|
|
|
use crate::heed_codec::facet::{FacetLevelValueF64Codec, FacetValueStringCodec};
|
|
|
|
use crate::{CboRoaringBitmapCodec, FieldId, FieldsIdsMap, Index, Result};
|
2020-11-26 20:42:54 +01:00
|
|
|
|
2021-05-03 11:45:45 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Operator {
|
2021-04-07 11:57:16 +02:00
|
|
|
GreaterThan(f64),
|
|
|
|
GreaterThanOrEqual(f64),
|
2021-05-03 11:45:45 +02:00
|
|
|
Equal(Option<f64>, String),
|
|
|
|
NotEqual(Option<f64>, String),
|
2021-04-07 11:57:16 +02:00
|
|
|
LowerThan(f64),
|
|
|
|
LowerThanOrEqual(f64),
|
|
|
|
Between(f64, f64),
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 11:45:45 +02:00
|
|
|
impl Operator {
|
2020-11-26 20:42:54 +01:00
|
|
|
/// This method can return two operations in case it must express
|
|
|
|
/// an OR operation for the between case (i.e. `TO`).
|
|
|
|
fn negate(self) -> (Self, Option<Self>) {
|
|
|
|
match self {
|
2021-06-16 18:33:33 +02:00
|
|
|
GreaterThan(n) => (LowerThanOrEqual(n), None),
|
2021-05-03 11:45:45 +02:00
|
|
|
GreaterThanOrEqual(n) => (LowerThan(n), None),
|
2021-06-16 18:33:33 +02:00
|
|
|
Equal(n, s) => (NotEqual(n, s), None),
|
|
|
|
NotEqual(n, s) => (Equal(n, s), None),
|
|
|
|
LowerThan(n) => (GreaterThanOrEqual(n), None),
|
|
|
|
LowerThanOrEqual(n) => (GreaterThan(n), None),
|
|
|
|
Between(n, m) => (LowerThan(n), Some(GreaterThan(m))),
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2021-06-01 15:25:17 +02:00
|
|
|
pub enum FilterCondition {
|
2021-05-03 11:45:45 +02:00
|
|
|
Operator(FieldId, Operator),
|
2020-11-26 20:42:54 +01:00
|
|
|
Or(Box<Self>, Box<Self>),
|
|
|
|
And(Box<Self>, Box<Self>),
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
impl FilterCondition {
|
2021-01-07 10:17:27 +01:00
|
|
|
pub fn from_array<I, J, A, B>(
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
index: &Index,
|
|
|
|
array: I,
|
2021-06-14 16:46:19 +02:00
|
|
|
) -> Result<Option<FilterCondition>>
|
2021-06-16 18:33:33 +02:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Either<J, B>>,
|
|
|
|
J: IntoIterator<Item = A>,
|
|
|
|
A: AsRef<str>,
|
|
|
|
B: AsRef<str>,
|
2021-01-07 10:17:27 +01:00
|
|
|
{
|
|
|
|
let mut ands = None;
|
|
|
|
|
|
|
|
for either in array {
|
|
|
|
match either {
|
|
|
|
Either::Left(array) => {
|
|
|
|
let mut ors = None;
|
|
|
|
for rule in array {
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(rtxn, index, rule.as_ref())?;
|
2021-01-07 10:17:27 +01:00
|
|
|
ors = match ors.take() {
|
|
|
|
Some(ors) => Some(Or(Box::new(ors), Box::new(condition))),
|
|
|
|
None => Some(condition),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(rule) = ors {
|
|
|
|
ands = match ands.take() {
|
|
|
|
Some(ands) => Some(And(Box::new(ands), Box::new(rule))),
|
|
|
|
None => Some(rule),
|
|
|
|
};
|
|
|
|
}
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-01-07 10:17:27 +01:00
|
|
|
Either::Right(rule) => {
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(rtxn, index, rule.as_ref())?;
|
2021-01-07 10:17:27 +01:00
|
|
|
ands = match ands.take() {
|
|
|
|
Some(ands) => Some(And(Box::new(ands), Box::new(condition))),
|
|
|
|
None => Some(condition),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ands)
|
|
|
|
}
|
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
pub fn from_str(
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
index: &Index,
|
|
|
|
expression: &str,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let fields_ids_map = index.fields_ids_map(rtxn)?;
|
2021-06-01 12:19:55 +02:00
|
|
|
let filterable_fields = index.filterable_fields_ids(rtxn)?;
|
2021-06-16 18:33:33 +02:00
|
|
|
let lexed =
|
|
|
|
FilterParser::parse(Rule::prgm, expression).map_err(UserError::InvalidFilter)?;
|
2021-06-01 15:25:17 +02:00
|
|
|
FilterCondition::from_pairs(&fields_ids_map, &filterable_fields, lexed)
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_pairs(
|
|
|
|
fim: &FieldsIdsMap,
|
2021-05-03 11:45:45 +02:00
|
|
|
ff: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
expression: Pairs<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<Self> {
|
2020-11-26 20:42:54 +01:00
|
|
|
PREC_CLIMBER.climb(
|
|
|
|
expression,
|
|
|
|
|pair: Pair<Rule>| match pair.as_rule() {
|
|
|
|
Rule::greater => Ok(Self::greater_than(fim, ff, pair)?),
|
|
|
|
Rule::geq => Ok(Self::greater_than_or_equal(fim, ff, pair)?),
|
|
|
|
Rule::eq => Ok(Self::equal(fim, ff, pair)?),
|
|
|
|
Rule::neq => Ok(Self::equal(fim, ff, pair)?.negate()),
|
|
|
|
Rule::leq => Ok(Self::lower_than_or_equal(fim, ff, pair)?),
|
|
|
|
Rule::less => Ok(Self::lower_than(fim, ff, pair)?),
|
|
|
|
Rule::between => Ok(Self::between(fim, ff, pair)?),
|
|
|
|
Rule::not => Ok(Self::from_pairs(fim, ff, pair.into_inner())?.negate()),
|
|
|
|
Rule::prgm => Self::from_pairs(fim, ff, pair.into_inner()),
|
|
|
|
Rule::term => Self::from_pairs(fim, ff, pair.into_inner()),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2021-06-16 18:33:33 +02:00
|
|
|
|lhs: Result<Self>, op: Pair<Rule>, rhs: Result<Self>| match op.as_rule() {
|
|
|
|
Rule::or => Ok(Or(Box::new(lhs?), Box::new(rhs?))),
|
|
|
|
Rule::and => Ok(And(Box::new(lhs?), Box::new(rhs?))),
|
|
|
|
_ => unreachable!(),
|
2020-11-26 20:42:54 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
fn negate(self) -> FilterCondition {
|
2020-11-26 20:42:54 +01:00
|
|
|
match self {
|
2021-05-03 11:45:45 +02:00
|
|
|
Operator(fid, op) => match op.negate() {
|
|
|
|
(op, None) => Operator(fid, op),
|
|
|
|
(a, Some(b)) => Or(Box::new(Operator(fid, a)), Box::new(Operator(fid, b))),
|
2021-04-07 11:57:16 +02:00
|
|
|
},
|
2020-11-26 20:42:54 +01:00
|
|
|
Or(a, b) => And(Box::new(a.negate()), Box::new(b.negate())),
|
|
|
|
And(a, b) => Or(Box::new(a.negate()), Box::new(b.negate())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn between(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
|
|
|
let (lresult, _) = pest_parse(items.next().unwrap());
|
|
|
|
let (rresult, _) = pest_parse(items.next().unwrap());
|
|
|
|
|
2021-06-15 17:22:04 +02:00
|
|
|
let lvalue = lresult.map_err(UserError::InvalidFilter)?;
|
|
|
|
let rvalue = rresult.map_err(UserError::InvalidFilter)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
|
|
|
Ok(Operator(fid, Between(lvalue, rvalue)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn equal(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
let value = items.next().unwrap();
|
2021-05-03 11:45:45 +02:00
|
|
|
let (result, svalue) = pest_parse(value);
|
|
|
|
|
2021-05-03 15:58:47 +02:00
|
|
|
let svalue = svalue.to_lowercase();
|
|
|
|
Ok(Operator(fid, Equal(result.ok(), svalue)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn greater_than(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
let value = items.next().unwrap();
|
2021-05-03 11:45:45 +02:00
|
|
|
let (result, _svalue) = pest_parse(value);
|
2021-06-15 17:22:04 +02:00
|
|
|
let value = result.map_err(UserError::InvalidFilter)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
Ok(Operator(fid, GreaterThan(value)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn greater_than_or_equal(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
let value = items.next().unwrap();
|
2021-05-03 11:45:45 +02:00
|
|
|
let (result, _svalue) = pest_parse(value);
|
2021-06-15 17:22:04 +02:00
|
|
|
let value = result.map_err(UserError::InvalidFilter)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
Ok(Operator(fid, GreaterThanOrEqual(value)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_than(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
let value = items.next().unwrap();
|
2021-05-03 11:45:45 +02:00
|
|
|
let (result, _svalue) = pest_parse(value);
|
2021-06-15 17:22:04 +02:00
|
|
|
let value = result.map_err(UserError::InvalidFilter)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
Ok(Operator(fid, LowerThan(value)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_than_or_equal(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
2021-06-01 12:19:55 +02:00
|
|
|
filterable_fields: &HashSet<FieldId>,
|
2020-11-26 20:42:54 +01:00
|
|
|
item: Pair<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<FilterCondition> {
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut items = item.into_inner();
|
2021-06-14 16:46:19 +02:00
|
|
|
let fid = field_id(fields_ids_map, filterable_fields, &mut items)
|
2021-06-15 17:20:33 +02:00
|
|
|
.map_err(UserError::InvalidFilterAttribute)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
let value = items.next().unwrap();
|
2021-05-03 11:45:45 +02:00
|
|
|
let (result, _svalue) = pest_parse(value);
|
2021-06-15 17:22:04 +02:00
|
|
|
let value = result.map_err(UserError::InvalidFilter)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
Ok(Operator(fid, LowerThanOrEqual(value)))
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
impl FilterCondition {
|
2020-11-26 20:42:54 +01:00
|
|
|
/// Aggregates the documents ids that are part of the specified range automatically
|
|
|
|
/// going deeper through the levels.
|
2021-04-07 11:57:16 +02:00
|
|
|
fn explore_facet_number_levels(
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
db: heed::Database<FacetLevelValueF64Codec, CboRoaringBitmapCodec>,
|
2020-11-26 20:42:54 +01:00
|
|
|
field_id: FieldId,
|
|
|
|
level: u8,
|
2021-04-07 11:57:16 +02:00
|
|
|
left: Bound<f64>,
|
|
|
|
right: Bound<f64>,
|
2020-11-26 20:42:54 +01:00
|
|
|
output: &mut RoaringBitmap,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<()> {
|
2020-11-26 20:42:54 +01:00
|
|
|
match (left, right) {
|
|
|
|
// If the request is an exact value we must go directly to the deepest level.
|
|
|
|
(Included(l), Included(r)) if l == r && level > 0 => {
|
2021-06-16 18:33:33 +02:00
|
|
|
return Self::explore_facet_number_levels(
|
|
|
|
rtxn, db, field_id, 0, left, right, output,
|
|
|
|
);
|
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
// lower TO upper when lower > upper must return no result
|
|
|
|
(Included(l), Included(r)) if l > r => return Ok(()),
|
|
|
|
(Included(l), Excluded(r)) if l >= r => return Ok(()),
|
|
|
|
(Excluded(l), Excluded(r)) if l >= r => return Ok(()),
|
|
|
|
(Excluded(l), Included(r)) if l >= r => return Ok(()),
|
|
|
|
(_, _) => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut left_found = None;
|
|
|
|
let mut right_found = None;
|
|
|
|
|
|
|
|
// We must create a custom iterator to be able to iterate over the
|
|
|
|
// requested range as the range iterator cannot express some conditions.
|
2021-04-07 11:57:16 +02:00
|
|
|
let iter = FacetRange::new(rtxn, db, field_id, level, left, right)?;
|
2020-11-26 20:42:54 +01:00
|
|
|
|
|
|
|
debug!("Iterating between {:?} and {:?} (level {})", left, right, level);
|
|
|
|
|
|
|
|
for (i, result) in iter.enumerate() {
|
|
|
|
let ((_fid, level, l, r), docids) = result?;
|
|
|
|
debug!("{:?} to {:?} (level {}) found {} documents", l, r, level, docids.len());
|
|
|
|
output.union_with(&docids);
|
|
|
|
// We save the leftest and rightest bounds we actually found at this level.
|
2021-06-16 18:33:33 +02:00
|
|
|
if i == 0 {
|
|
|
|
left_found = Some(l);
|
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
right_found = Some(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can we go deeper?
|
|
|
|
let deeper_level = match level.checked_sub(1) {
|
|
|
|
Some(level) => level,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// We must refine the left and right bounds of this range by retrieving the
|
|
|
|
// missing part in a deeper level.
|
|
|
|
match left_found.zip(right_found) {
|
|
|
|
Some((left_found, right_found)) => {
|
|
|
|
// If the bound is satisfied we avoid calling this function again.
|
|
|
|
if !matches!(left, Included(l) if l == left_found) {
|
|
|
|
let sub_right = Excluded(left_found);
|
2021-06-16 18:33:33 +02:00
|
|
|
debug!(
|
|
|
|
"calling left with {:?} to {:?} (level {})",
|
|
|
|
left, sub_right, deeper_level
|
|
|
|
);
|
|
|
|
Self::explore_facet_number_levels(
|
|
|
|
rtxn,
|
|
|
|
db,
|
|
|
|
field_id,
|
|
|
|
deeper_level,
|
|
|
|
left,
|
|
|
|
sub_right,
|
|
|
|
output,
|
|
|
|
)?;
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
if !matches!(right, Included(r) if r == right_found) {
|
|
|
|
let sub_left = Excluded(right_found);
|
2021-06-16 18:33:33 +02:00
|
|
|
debug!(
|
|
|
|
"calling right with {:?} to {:?} (level {})",
|
|
|
|
sub_left, right, deeper_level
|
|
|
|
);
|
|
|
|
Self::explore_facet_number_levels(
|
|
|
|
rtxn,
|
|
|
|
db,
|
|
|
|
field_id,
|
|
|
|
deeper_level,
|
|
|
|
sub_left,
|
|
|
|
right,
|
|
|
|
output,
|
|
|
|
)?;
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
None => {
|
|
|
|
// If we found nothing at this level it means that we must find
|
|
|
|
// the same bounds but at a deeper, more precise level.
|
2021-06-16 18:33:33 +02:00
|
|
|
Self::explore_facet_number_levels(
|
|
|
|
rtxn,
|
|
|
|
db,
|
|
|
|
field_id,
|
|
|
|
deeper_level,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
output,
|
|
|
|
)?;
|
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-03 11:45:45 +02:00
|
|
|
fn evaluate_operator(
|
2021-04-07 11:57:16 +02:00
|
|
|
rtxn: &heed::RoTxn,
|
2020-11-26 20:42:54 +01:00
|
|
|
index: &Index,
|
2021-05-03 11:45:45 +02:00
|
|
|
numbers_db: heed::Database<FacetLevelValueF64Codec, CboRoaringBitmapCodec>,
|
|
|
|
strings_db: heed::Database<FacetValueStringCodec, CboRoaringBitmapCodec>,
|
2020-11-26 20:42:54 +01:00
|
|
|
field_id: FieldId,
|
2021-05-03 11:45:45 +02:00
|
|
|
operator: &Operator,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> Result<RoaringBitmap> {
|
2020-11-26 20:42:54 +01:00
|
|
|
// Make sure we always bound the ranges with the field id and the level,
|
|
|
|
// as the facets values are all in the same database and prefixed by the
|
|
|
|
// field id and the level.
|
|
|
|
let (left, right) = match operator {
|
2021-06-16 18:33:33 +02:00
|
|
|
GreaterThan(val) => (Excluded(*val), Included(f64::MAX)),
|
2021-05-03 11:45:45 +02:00
|
|
|
GreaterThanOrEqual(val) => (Included(*val), Included(f64::MAX)),
|
2021-06-16 18:33:33 +02:00
|
|
|
Equal(number, string) => {
|
2021-05-03 11:45:45 +02:00
|
|
|
let string_docids = strings_db.get(rtxn, &(field_id, &string))?.unwrap_or_default();
|
|
|
|
let number_docids = match number {
|
|
|
|
Some(n) => {
|
|
|
|
let n = Included(*n);
|
|
|
|
let mut output = RoaringBitmap::new();
|
2021-06-16 18:33:33 +02:00
|
|
|
Self::explore_facet_number_levels(
|
|
|
|
rtxn,
|
|
|
|
numbers_db,
|
|
|
|
field_id,
|
|
|
|
0,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
&mut output,
|
|
|
|
)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
output
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-05-03 11:45:45 +02:00
|
|
|
None => RoaringBitmap::new(),
|
|
|
|
};
|
|
|
|
return Ok(string_docids | number_docids);
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2021-05-03 11:45:45 +02:00
|
|
|
NotEqual(number, string) => {
|
|
|
|
let all_numbers_ids = if number.is_some() {
|
|
|
|
index.number_faceted_documents_ids(rtxn, field_id)?
|
|
|
|
} else {
|
|
|
|
RoaringBitmap::new()
|
|
|
|
};
|
|
|
|
let all_strings_ids = index.string_faceted_documents_ids(rtxn, field_id)?;
|
|
|
|
let operator = Equal(*number, string.clone());
|
2021-06-16 18:33:33 +02:00
|
|
|
let docids = Self::evaluate_operator(
|
|
|
|
rtxn, index, numbers_db, strings_db, field_id, &operator,
|
|
|
|
)?;
|
2021-05-03 11:45:45 +02:00
|
|
|
return Ok((all_numbers_ids | all_strings_ids) - docids);
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
|
|
|
LowerThan(val) => (Included(f64::MIN), Excluded(*val)),
|
2021-05-03 11:45:45 +02:00
|
|
|
LowerThanOrEqual(val) => (Included(f64::MIN), Included(*val)),
|
2021-06-16 18:33:33 +02:00
|
|
|
Between(left, right) => (Included(*left), Included(*right)),
|
2020-11-26 20:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Ask for the biggest value that can exist for this specific field, if it exists
|
|
|
|
// that's fine if it don't, the value just before will be returned instead.
|
2021-05-03 11:45:45 +02:00
|
|
|
let biggest_level = numbers_db
|
2021-04-07 11:57:16 +02:00
|
|
|
.remap_data_type::<DecodeIgnore>()
|
|
|
|
.get_lower_than_or_equal_to(rtxn, &(field_id, u8::MAX, f64::MAX, f64::MAX))?
|
2020-11-26 20:42:54 +01:00
|
|
|
.and_then(|((id, level, _, _), _)| if id == field_id { Some(level) } else { None });
|
|
|
|
|
|
|
|
match biggest_level {
|
|
|
|
Some(level) => {
|
|
|
|
let mut output = RoaringBitmap::new();
|
2021-06-16 18:33:33 +02:00
|
|
|
Self::explore_facet_number_levels(
|
|
|
|
rtxn,
|
|
|
|
numbers_db,
|
|
|
|
field_id,
|
|
|
|
level,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
&mut output,
|
|
|
|
)?;
|
2020-11-26 20:42:54 +01:00
|
|
|
Ok(output)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
None => Ok(RoaringBitmap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
pub fn evaluate(&self, rtxn: &heed::RoTxn, index: &Index) -> Result<RoaringBitmap> {
|
2021-05-03 11:45:45 +02:00
|
|
|
let numbers_db = index.facet_id_f64_docids;
|
|
|
|
let strings_db = index.facet_id_string_docids;
|
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
match self {
|
2021-05-03 11:45:45 +02:00
|
|
|
Operator(fid, op) => {
|
|
|
|
Self::evaluate_operator(rtxn, index, numbers_db, strings_db, *fid, op)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
Or(lhs, rhs) => {
|
|
|
|
let lhs = lhs.evaluate(rtxn, index)?;
|
|
|
|
let rhs = rhs.evaluate(rtxn, index)?;
|
|
|
|
Ok(lhs | rhs)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
And(lhs, rhs) => {
|
|
|
|
let lhs = lhs.evaluate(rtxn, index)?;
|
|
|
|
let rhs = rhs.evaluate(rtxn, index)?;
|
|
|
|
Ok(lhs & rhs)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:22:58 +02:00
|
|
|
/// Retrieve the field id base on the pest value, returns an error is
|
|
|
|
/// the field does not exist or is not filterable.
|
|
|
|
///
|
|
|
|
/// The pest pair is simply a string associated with a span, a location to highlight in
|
|
|
|
/// the error message.
|
|
|
|
fn field_id(
|
|
|
|
fields_ids_map: &FieldsIdsMap,
|
|
|
|
filterable_fields: &HashSet<FieldId>,
|
|
|
|
items: &mut Pairs<Rule>,
|
2021-06-16 18:33:33 +02:00
|
|
|
) -> StdResult<FieldId, PestError<Rule>> {
|
2021-06-01 15:22:58 +02:00
|
|
|
// lexing ensures that we at least have a key
|
|
|
|
let key = items.next().unwrap();
|
|
|
|
|
|
|
|
let field_id = match fields_ids_map.id(key.as_str()) {
|
|
|
|
Some(field_id) => field_id,
|
2021-06-16 18:33:33 +02:00
|
|
|
None => {
|
|
|
|
return Err(PestError::new_from_span(
|
|
|
|
ErrorVariant::CustomError {
|
|
|
|
message: format!(
|
|
|
|
"attribute `{}` not found, available attributes are: {}",
|
|
|
|
key.as_str(),
|
|
|
|
fields_ids_map.iter().map(|(_, n)| n).collect::<Vec<_>>().join(", "),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
key.as_span(),
|
|
|
|
))
|
|
|
|
}
|
2021-06-01 15:22:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if !filterable_fields.contains(&field_id) {
|
|
|
|
return Err(PestError::new_from_span(
|
|
|
|
ErrorVariant::CustomError {
|
|
|
|
message: format!(
|
|
|
|
"attribute `{}` is not filterable, available filterable attributes are: {}",
|
|
|
|
key.as_str(),
|
2021-06-16 18:33:33 +02:00
|
|
|
filterable_fields
|
|
|
|
.iter()
|
|
|
|
.flat_map(|id| { fields_ids_map.name(*id) })
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
2021-06-01 15:22:58 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
key.as_span(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(field_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to parse the pest pair into the type `T` specified, always returns
|
|
|
|
/// the original string that we tried to parse.
|
|
|
|
///
|
|
|
|
/// Returns the parsing error associated with the span if the conversion fails.
|
2021-06-14 16:46:19 +02:00
|
|
|
fn pest_parse<T>(pair: Pair<Rule>) -> (StdResult<T, pest::error::Error<Rule>>, String)
|
2021-06-16 18:33:33 +02:00
|
|
|
where
|
|
|
|
T: FromStr,
|
|
|
|
T::Err: ToString,
|
2021-06-01 15:22:58 +02:00
|
|
|
{
|
|
|
|
let result = match pair.as_str().parse::<T>() {
|
|
|
|
Ok(value) => Ok(value),
|
|
|
|
Err(e) => Err(PestError::<Rule>::new_from_span(
|
|
|
|
ErrorVariant::CustomError { message: e.to_string() },
|
|
|
|
pair.as_span(),
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
|
|
|
(result, pair.as_str().to_string())
|
|
|
|
}
|
|
|
|
|
2020-11-26 20:42:54 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-16 18:33:33 +02:00
|
|
|
use big_s::S;
|
2020-11-26 20:42:54 +01:00
|
|
|
use heed::EnvOpenOptions;
|
2021-05-03 15:58:47 +02:00
|
|
|
use maplit::hashset;
|
2021-06-16 18:33:33 +02:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::update::Settings;
|
2020-11-26 20:42:54 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
2021-06-01 12:19:55 +02:00
|
|
|
// Set the filterable fields to be the channel.
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2020-12-22 16:21:07 +01:00
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, 0);
|
2021-06-16 18:33:33 +02:00
|
|
|
builder.set_filterable_fields(hashset! { S("channel") });
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(|_, _| ()).unwrap();
|
2020-11-26 20:42:54 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Test that the facet condition is correctly generated.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(&rtxn, &index, "channel = Ponce").unwrap();
|
2021-05-03 15:58:47 +02:00
|
|
|
let expected = Operator(0, Operator::Equal(None, S("ponce")));
|
2020-11-26 20:42:54 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(&rtxn, &index, "channel != ponce").unwrap();
|
2021-05-03 15:58:47 +02:00
|
|
|
let expected = Operator(0, Operator::NotEqual(None, S("ponce")));
|
2020-11-26 20:42:54 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(&rtxn, &index, "NOT channel = ponce").unwrap();
|
2021-05-03 15:58:47 +02:00
|
|
|
let expected = Operator(0, Operator::NotEqual(None, S("ponce")));
|
2020-11-26 20:42:54 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-07 11:57:16 +02:00
|
|
|
fn number() {
|
2020-11-26 20:42:54 +01:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
2021-06-01 12:19:55 +02:00
|
|
|
// Set the filterable fields to be the channel.
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2020-12-22 16:21:07 +01:00
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, 0);
|
2021-06-16 18:33:33 +02:00
|
|
|
builder.set_filterable_fields(hashset! { "timestamp".into() });
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(|_, _| ()).unwrap();
|
2020-11-26 20:42:54 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Test that the facet condition is correctly generated.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(&rtxn, &index, "timestamp 22 TO 44").unwrap();
|
2021-05-03 15:58:47 +02:00
|
|
|
let expected = Operator(0, Between(22.0, 44.0));
|
2020-11-26 20:42:54 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(&rtxn, &index, "NOT timestamp 22 TO 44").unwrap();
|
2021-06-16 18:33:33 +02:00
|
|
|
let expected =
|
|
|
|
Or(Box::new(Operator(0, LowerThan(22.0))), Box::new(Operator(0, GreaterThan(44.0))));
|
2020-11-26 20:42:54 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parentheses() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
2021-06-01 12:19:55 +02:00
|
|
|
// Set the filterable fields to be the channel.
|
2020-11-26 20:42:54 +01:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2020-12-22 16:21:07 +01:00
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, 0);
|
2021-05-03 15:58:47 +02:00
|
|
|
builder.set_searchable_fields(vec![S("channel"), S("timestamp")]); // to keep the fields order
|
2021-06-16 18:33:33 +02:00
|
|
|
builder.set_filterable_fields(hashset! { S("channel"), S("timestamp") });
|
2020-12-22 16:21:07 +01:00
|
|
|
builder.execute(|_, _| ()).unwrap();
|
2020-11-26 20:42:54 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Test that the facet condition is correctly generated.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(
|
2021-06-16 18:33:33 +02:00
|
|
|
&rtxn,
|
|
|
|
&index,
|
2020-11-26 20:42:54 +01:00
|
|
|
"channel = gotaga OR (timestamp 22 TO 44 AND channel != ponce)",
|
2021-06-16 18:33:33 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-26 20:42:54 +01:00
|
|
|
let expected = Or(
|
2021-05-03 15:58:47 +02:00
|
|
|
Box::new(Operator(0, Operator::Equal(None, S("gotaga")))),
|
2020-11-26 20:42:54 +01:00
|
|
|
Box::new(And(
|
2021-05-03 15:58:47 +02:00
|
|
|
Box::new(Operator(1, Between(22.0, 44.0))),
|
|
|
|
Box::new(Operator(0, Operator::NotEqual(None, S("ponce")))),
|
2021-06-16 18:33:33 +02:00
|
|
|
)),
|
2020-11-26 20:42:54 +01:00
|
|
|
);
|
|
|
|
assert_eq!(condition, expected);
|
|
|
|
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_str(
|
2021-06-16 18:33:33 +02:00
|
|
|
&rtxn,
|
|
|
|
&index,
|
2020-11-26 20:42:54 +01:00
|
|
|
"channel = gotaga OR NOT (timestamp 22 TO 44 AND channel != ponce)",
|
2021-06-16 18:33:33 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-26 20:42:54 +01:00
|
|
|
let expected = Or(
|
2021-05-03 15:58:47 +02:00
|
|
|
Box::new(Operator(0, Operator::Equal(None, S("gotaga")))),
|
2020-11-26 20:42:54 +01:00
|
|
|
Box::new(Or(
|
|
|
|
Box::new(Or(
|
2021-05-03 15:58:47 +02:00
|
|
|
Box::new(Operator(1, LowerThan(22.0))),
|
|
|
|
Box::new(Operator(1, GreaterThan(44.0))),
|
2020-11-26 20:42:54 +01:00
|
|
|
)),
|
2021-05-03 15:58:47 +02:00
|
|
|
Box::new(Operator(0, Operator::Equal(None, S("ponce")))),
|
2020-11-26 20:42:54 +01:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
assert_eq!(condition, expected);
|
|
|
|
}
|
2020-12-21 19:36:14 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_array() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
2021-06-01 12:19:55 +02:00
|
|
|
// Set the filterable fields to be the channel.
|
2020-12-21 19:36:14 +01:00
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-02-01 14:38:04 +01:00
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, 0);
|
2021-05-03 15:58:47 +02:00
|
|
|
builder.set_searchable_fields(vec![S("channel"), S("timestamp")]); // to keep the fields order
|
2021-06-16 18:33:33 +02:00
|
|
|
builder.set_filterable_fields(hashset! { S("channel"), S("timestamp") });
|
2021-02-01 14:38:04 +01:00
|
|
|
builder.execute(|_, _| ()).unwrap();
|
2020-12-21 19:36:14 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Test that the facet condition is correctly generated.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
2021-06-01 15:25:17 +02:00
|
|
|
let condition = FilterCondition::from_array(
|
2021-06-16 18:33:33 +02:00
|
|
|
&rtxn,
|
|
|
|
&index,
|
|
|
|
vec![
|
|
|
|
Either::Right("channel = gotaga"),
|
|
|
|
Either::Left(vec!["timestamp = 44", "channel != ponce"]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2021-06-01 15:25:17 +02:00
|
|
|
let expected = FilterCondition::from_str(
|
2021-06-16 18:33:33 +02:00
|
|
|
&rtxn,
|
|
|
|
&index,
|
2020-12-21 19:36:14 +01:00
|
|
|
"channel = gotaga AND (timestamp = 44 OR channel != ponce)",
|
2021-06-16 18:33:33 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-21 19:36:14 +01:00
|
|
|
assert_eq!(condition, expected);
|
|
|
|
}
|
2020-11-26 20:42:54 +01:00
|
|
|
}
|