Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Store the geopoint in three dimensions #424

Merged
merged 1 commit into from
Jan 10, 2022
Merged

Store the geopoint in three dimensions #424

merged 1 commit into from
Jan 10, 2022

Conversation

irevoire
Copy link
Member

@irevoire irevoire commented Dec 14, 2021

Related to this issue: meilisearch/meilisearch#1872

Fix the whole computation of distance for any “geo” operations (sort or filter). Now when you sort points they are returned to you in the right order.
And when you filter on a specific radius you only get points included in the radius.

This PR changes the way we store the geo points in the RTree.
Instead of considering the latitude and longitude as orthogonal coordinates, we convert them to real orthogonal coordinates projected on a sphere with a radius of 1.
This is the conversion formulae.
image
Which, in rust, translate to this function:

pub fn lat_lng_to_xyz(coord: &[f64; 2]) -> [f64; 3] {
    let [lat, lng] = coord.map(|f| f.to_radians());
    let x = lat.cos() * lng.cos();
    let y = lat.cos() * lng.sin();
    let z = lat.sin();

    [x, y, z]
}

Storing the points on a sphere is easier / faster to compute than storing the point on an approximation of the real earth shape.
But when we need to compute the distance between two points we still need to use the haversine distance which works with latitude and longitude.
So, to do the fewest search-time computation possible I'm now associating every point with its DocId and its lat/lng.

@irevoire irevoire added DB breaking The related changes break the DB enhancement New feature or request indexing Related to the documents/settings indexing algorithms. querying Related to the searching/fetch data algorithms. and removed enhancement New feature or request labels Dec 14, 2021
@curquiza
Copy link
Member

curquiza commented Dec 14, 2021

⚠️ Should be merged once MeiliSearch v0.25.0 is released :) This fix is for v0.26.0

My bad there are already changed unrelated to v0.25.0 on main, so you can merge it!

Copy link
Member

@Kerollmops Kerollmops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope the RTree works better with 3D points, will merge this in v0.26.

Copy link
Member

@Kerollmops Kerollmops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors merge

@bors
Copy link
Contributor

bors bot commented Jan 10, 2022

@bors bors bot merged commit 559e019 into main Jan 10, 2022
@bors bors bot deleted the fix_geo_search branch January 10, 2022 15:42
@curquiza curquiza mentioned this pull request Jan 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
DB breaking The related changes break the DB indexing Related to the documents/settings indexing algorithms. querying Related to the searching/fetch data algorithms.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants