Apply suggestions from code review

Co-authored-by: Clément Renault <clement@meilisearch.com>
This commit is contained in:
Irevoire 2021-09-09 12:20:08 +02:00 committed by Tamo
parent c81ff22c5b
commit a84f3a8b31
No known key found for this signature in database
GPG key ID: 20CD8020AFA88D69
13 changed files with 77 additions and 69 deletions

View file

@ -1,3 +1,5 @@
use std::iter;
use roaring::RoaringBitmap;
use rstar::RTree;
@ -23,7 +25,7 @@ impl<'t> Geo<'t> {
parent: Box<dyn Criterion + 't>,
point: [f64; 2],
) -> Result<Self> {
let candidates = Box::new(std::iter::empty());
let candidates = Box::new(iter::empty());
let allowed_candidates = index.geo_faceted_documents_ids(rtxn)?;
let bucket_candidates = RoaringBitmap::new();
let rtree = index.geo_rtree(rtxn)?;
@ -41,7 +43,7 @@ impl<'t> Geo<'t> {
}
}
impl<'t> Criterion for Geo<'t> {
impl Criterion for Geo<'_> {
fn next(&mut self, params: &mut CriterionParameters) -> Result<Option<CriterionResult>> {
// if there is no rtree we have nothing to returns
let rtree = match self.rtree.as_ref() {
@ -108,7 +110,7 @@ fn geo_point(
let results = rtree
.nearest_neighbor_iter(&point)
.filter_map(move |point| candidates.contains(point.data).then(|| point.data))
.map(|id| std::iter::once(id).collect::<RoaringBitmap>())
.map(|id| iter::once(id).collect::<RoaringBitmap>())
.collect::<Vec<_>>();
Box::new(results.into_iter())

View file

@ -311,7 +311,9 @@ impl<'t> CriteriaBuilder<'t> {
point.clone(),
)?),
AscDescName::Desc(Member::Geo(_point)) => {
return Err(UserError::InvalidSortName { name: "Sorting in descending order is currently not supported for the geosearch".to_string() })?
return Err(UserError::InvalidSortName {
name: "Sorting in descending order is currently not supported for the geosearch".to_string(),
})?
}
};
}

View file

@ -21,7 +21,9 @@ use crate::error::UserError;
use crate::heed_codec::facet::{
FacetLevelValueF64Codec, FacetStringLevelZeroCodec, FacetStringLevelZeroValueCodec,
};
use crate::{CboRoaringBitmapCodec, FieldId, FieldsIdsMap, Index, Result};
use crate::{
distance_between_two_points, CboRoaringBitmapCodec, FieldId, FieldsIdsMap, Index, Result,
};
#[derive(Debug, Clone, PartialEq)]
pub enum Operator {
@ -198,10 +200,10 @@ impl FilterCondition {
ErrorVariant::CustomError {
message: format!("The `_geoRadius` filter expect three arguments: `_geoRadius(latitude, longitude, radius)`"),
},
// we want to point to the last parameters and if there was no parameters we
// we want to point to the last parameters and if there was no parameters we
// point to the parenthesis
parameters.last().map(|param| param.1.clone()).unwrap_or(param_span),
)))?;
parameters.last().map(|param| param.1.clone()).unwrap_or(param_span),
)))?;
}
let (lat, lng, distance) = (&parameters[0], &parameters[1], parameters[2].0);
if let Some(span) = (!(-181.0..181.).contains(&lat.0))
@ -505,19 +507,18 @@ impl FilterCondition {
LowerThanOrEqual(val) => (Included(f64::MIN), Included(*val)),
Between(left, right) => (Included(*left), Included(*right)),
GeoLowerThan(base_point, distance) => {
let mut result = RoaringBitmap::new();
let rtree = match index.geo_rtree(rtxn)? {
Some(rtree) => rtree,
None => return Ok(result),
None => return Ok(RoaringBitmap::new()),
};
rtree
let result = rtree
.nearest_neighbor_iter(base_point)
.take_while(|point| {
dbg!(crate::distance_between_two_points(base_point, point.geom()))
< *distance
distance_between_two_points(base_point, point.geom()) < *distance
})
.for_each(|point| drop(result.insert(point.data)));
.map(|point| point.data)
.collect();
return Ok(result);
}
@ -600,14 +601,15 @@ fn field_id(
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: {}",
ErrorVariant::CustomError {
message: format!(
"`{}` is a reserved keyword and therefore can't be used as a filter expression. \
Available filterable attributes are: {}",
key.as_str(),
filterable_fields.iter().join(", "),
),
},
key.as_span(),
),
},
key.as_span(),
));
}
@ -691,7 +693,7 @@ mod tests {
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."
"`_geo` is a reserved keyword and therefore can't be used as a filter expression."
));
}