Skip to content

feat: Add K-Nearest Neighbor (<->) operator for blazing fast spatial searches#6

Merged
seuros merged 1 commit into
masterfrom
feat/knn-spatial-search
Jul 21, 2025
Merged

feat: Add K-Nearest Neighbor (<->) operator for blazing fast spatial searches#6
seuros merged 1 commit into
masterfrom
feat/knn-spatial-search

Conversation

@seuros

@seuros seuros commented Jul 21, 2025

Copy link
Copy Markdown
Owner

Summary

This PR adds the PostGIS K-Nearest Neighbor (<->) operator to the gem, enabling lightning-fast spatial searches that utilize spatial indexes.

Why This Matters

Traditional "find nearest" queries using ST_Distance + ORDER BY scan the entire table and calculate distances for every row. The <-> operator uses spatial indexes to return results orders of magnitude faster.

Implementation

New Arel Methods

  • distance_operator(geometry) - The primary method for KNN searches
  • :<-> alias - For those who prefer symbolic operators

Usage Examples

# Find 10 nearest locations (uses spatial index\!)
Location
  .order(Location.arel_table[:position].distance_operator(my_position))
  .limit(10)

# Find nearest restaurants with actual distances
Restaurant
  .select("*, ST_Distance(location, ST_GeomFromText('#{origin.as_text}', 4326)) as distance")
  .order(Restaurant.arel_table[:location].distance_operator(origin))
  .limit(5)

# Emergency: Find nearest hospitals
Hospital
  .where(emergency_room: true)
  .order(Hospital.arel_table[:coordinates].distance_operator(accident_location))
  .first

Performance Impact

The <-> operator can be 100-1000x faster than traditional distance queries for large datasets because it uses the spatial index directly rather than calculating distances for every row.

Documentation

Updated both the technical documentation and the space-themed guides with examples of tactical uses for the KNN operator.


That's it folks! This completes the extraction of custom spatial code I had in my projects. Any additional features can be added via PRs with tests.

…searches

- Added the PostGIS <-> operator (K-Nearest Neighbor) to Arel
- This operator uses spatial indexes for incredibly fast 'find nearest' queries
- Much faster than ST_Distance + ORDER BY for finding closest geometries
- Added comprehensive tests for the new operator
- Updated documentation with space combat examples showing tactical uses

The <-> operator is a game-changer for performance when finding nearest:
- Restaurants, stores, or locations
- Ships, satellites, or space stations
- Any spatial objects where you need the N closest items

Example usage:
Location.order(Location.arel_table[:position].distance_operator(origin)).limit(10)
@seuros seuros merged commit 6c7b1d9 into master Jul 21, 2025
1 check passed
@seuros seuros deleted the feat/knn-spatial-search branch July 21, 2025 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant