Geo-proximity helps find places near a location quickly. Sorted sets store locations with scores to keep them ordered by distance.
Geo-proximity with sorted sets in Redis
GEOADD key longitude latitude member GEORADIUS key longitude latitude radius m|km|ft|mi [WITHDIST] [WITHCOORD] [COUNT count]
GEOADD adds a location with longitude and latitude to a sorted set.
GEORADIUS finds members within a radius from a point, sorted by distance (ascending).
GEOADD places 13.361389 38.115556 "Palermo"
GEORADIUS places 15 37 200 km WITHDIST
GEORADIUS places 15 37 100 km COUNT 3 WITHDIST
This adds two cities to the sorted set "cities" and then finds all cities within 200 km of longitude 15 and latitude 37, showing their distances sorted nearest first.
GEOADD cities 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania" GEORADIUS cities 15 37 200 km WITHDIST
Longitude comes before latitude in GEOADD and GEORADIUS commands.
Distances returned by GEORADIUS are in the unit you specify (m, km, ft, mi).
Sorted sets keep locations ordered by their geohash score internally.
Use GEOADD to store locations with longitude and latitude.
Use GEORADIUS to find nearby locations within a radius.
You can get distances and sort results by proximity easily.
Practice
GEOADD do in the context of geo-proximity with sorted sets?Solution
Step 1: Understand the purpose of GEOADD
The GEOADD command stores geographic coordinates (longitude and latitude) associated with a member in a sorted set.Step 2: Differentiate GEOADD from other geo commands
Commands like GEORADIUS find nearby members, while GEOADD adds new locations.Final Answer:
Adds geographic locations (longitude, latitude) to a sorted set -> Option DQuick Check:
GEOADD = Add locations [OK]
- Confusing GEOADD with GEORADIUS
- Thinking GEOADD removes locations
- Assuming GEOADD returns distances
Solution
Step 1: Identify the command to find nearby members
GEORADIUS is used to find members within a radius from given longitude and latitude.Step 2: Check syntax correctness
GEORADIUS locations 13.361389 38.115556 5 km correctly specifies key, longitude, latitude, radius, and unit.Final Answer:
GEORADIUS locations 13.361389 38.115556 5 km -> Option CQuick Check:
GEORADIUS + lon lat + radius + unit = correct syntax [OK]
- Using GEOADD instead of GEORADIUS for searching
- Mixing GEORADIUSBYMEMBER syntax with coordinates
- Using GEODIST which calculates distance, not radius search
GEOADD cities 13.361389 38.115556 Palermo GEOADD cities 15.087269 37.502669 Catania GEORADIUS cities 15 37 200 km WITHDIST ASCWhat will be the output?
Solution
Step 1: Add two cities with their coordinates
Palermo and Catania are added with their longitude and latitude to the 'cities' key.Step 2: Use GEORADIUS to find cities within 200 km of (15, 37) sorted by distance ascending
Catania is closer (~56.44 km), Palermo is farther (~190.44 km), so output lists Catania first with distances.Final Answer:
[["Catania", "56.4413"], ["Palermo", "190.4424"]] -> Option BQuick Check:
GEORADIUS WITHDIST ASC sorts by nearest first [OK]
- Confusing order of results when using ASC
- Ignoring WITHDIST option for distances
- Swapping longitude and latitude values
GEORADIUS cities 15 37 100 km WITHDIST but get no results, even though you know cities exist nearby. What is the most likely cause?Solution
Step 1: Check if the key exists and has data
If the 'cities' key does not exist or is empty, GEORADIUS returns no results regardless of radius.Step 2: Consider other causes
Radius 100 km is reasonable, WITHDIST is supported, and longitude/latitude order is correct, so these are less likely.Final Answer:
The key 'cities' does not exist or is empty -> Option AQuick Check:
Empty key = no results [OK]
- Assuming WITHDIST causes errors
- Swapping lat/lon but still expecting results
- Using too small radius without verifying data
Solution
Step 1: Use GEORADIUSBYMEMBER to search by member name
GEORADIUSBYMEMBER finds locations near a member's coordinates, here 'Denver'.Step 2: Specify radius and unit, and sort ascending by distance
10 miles radius with WITHDIST ASC returns nearby locations sorted nearest first.Final Answer:
GEORADIUSBYMEMBER places Denver 10 mi WITHDIST ASC -> Option AQuick Check:
GEORADIUSBYMEMBER + WITHDIST ASC = nearest first [OK]
- Using GEORADIUS with member name instead of coordinates
- Sorting DESC instead of ASC for nearest first
- Using GEODIST which returns distance between two members only
Ready to prove it?
Pass the test and this pattern moves unknown
