From e5ef0cad9a78d6db3aa94c4f83486b9ee7ab5f8c Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 7 Sep 2021 12:11:03 +0200 Subject: [PATCH] use meters in the filters --- milli/Cargo.toml | 1 + milli/src/lib.rs | 8 ++++++++ milli/src/search/facet/filter_condition.rs | 13 ++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/milli/Cargo.toml b/milli/Cargo.toml index d2767afd4..be507332e 100644 --- a/milli/Cargo.toml +++ b/milli/Cargo.toml @@ -16,6 +16,7 @@ flate2 = "1.0.20" fst = "0.4.5" fxhash = "0.2.1" grenad = { version = "0.3.1", default-features = false, features = ["tempfile"] } +haversine = "0.2.1" heed = { git = "https://github.com/Kerollmops/heed", tag = "v0.12.1", default-features = false, features = ["lmdb", "sync-read-txn"] } human_format = "1.0.3" levenshtein_automata = { version = "0.2.0", features = ["fst_automaton"] } diff --git a/milli/src/lib.rs b/milli/src/lib.rs index a3cede1fd..3c7713308 100644 --- a/milli/src/lib.rs +++ b/milli/src/lib.rs @@ -142,6 +142,14 @@ where Some((head, tail)) } +/// Return the distance between two points in meters +fn distance_between_two_points(a: &[f64; 2], b: &[f64; 2]) -> f64 { + let a = haversine::Location { latitude: a[0], longitude: a[1] }; + let b = haversine::Location { latitude: b[0], longitude: b[1] }; + + haversine::distance(a, b, haversine::Units::Kilometers) * 1000. +} + #[cfg(test)] mod tests { use serde_json::json; diff --git a/milli/src/search/facet/filter_condition.rs b/milli/src/search/facet/filter_condition.rs index a36fddb01..08a84899f 100644 --- a/milli/src/search/facet/filter_condition.rs +++ b/milli/src/search/facet/filter_condition.rs @@ -504,17 +504,20 @@ impl FilterCondition { LowerThan(val) => (Included(f64::MIN), Excluded(*val)), LowerThanOrEqual(val) => (Included(f64::MIN), Included(*val)), Between(left, right) => (Included(*left), Included(*right)), - GeoLowerThan(point, distance) => { + GeoLowerThan(base_point, distance) => { let mut result = RoaringBitmap::new(); let rtree = match index.geo_rtree(rtxn)? { Some(rtree) => rtree, None => return Ok(result), }; - let iter = rtree - .nearest_neighbor_iter_with_distance_2(point) - .take_while(|(_, dist)| dist <= distance); - iter.for_each(|(point, _)| drop(result.insert(point.data))); + rtree + .nearest_neighbor_iter(base_point) + .take_while(|point| { + dbg!(crate::distance_between_two_points(base_point, point.geom())) + < *distance + }) + .for_each(|point| drop(result.insert(point.data))); return Ok(result); }