Use GEOSEARCH to find the members of a geospatial index that fall inside a circle or a rectangle.
The center is either an existing member of the index (FROMMEMBER) or an explicit coordinate pair (FROMLONLAT). The area is either a circle of a given radius (BYRADIUS) or an axis-aligned box of a given width and height centered on that point (BYBOX), which is the shape to use when you are covering a map viewport rather than a "within N km" question.
By default only member names come back. WITHDIST adds the distance from the center in the unit of the query, WITHCOORD the member's coordinates, and WITHHASH its raw geohash score. ASC and DESC sort by distance, and COUNT caps the number of results; adding ANY lets the server return as soon as it has enough matches, which is faster but no longer gives you the nearest ones.
GEOSEARCH replaces the deprecated GEORADIUS and GEORADIUSBYMEMBER commands and is the command to use for new code. Use GEOSEARCHSTORE when the result should be stored instead of returned.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
(FROMMEMBER <member> | FROMLONLAT <longitude> <latitude>) | Yes | No | Where to center the search: FROMMEMBER uses the stored position of an existing member, FROMLONLAT uses the given coordinates. |
(BYRADIUS <radius> (m | km | ft | mi) | BYBOX <width> <height> (m | km | ft | mi)) | Yes | No | The area to search: BYRADIUS a circle of the given radius, BYBOX an axis-aligned box of the given width and height centered on the search point. The unit is m (meters), km (kilometers), ft (feet), or mi (miles). |
(ASC | DESC) | No | No | Sort the matches by distance from the center: ASC (nearest first) or DESC (farthest first). Unsorted when omitted. |
COUNT <count> [ANY] | No | No | Return at most <count> matches. ANY returns as soon as enough matches are found, instead of sorting every match first. |
WITHCOORD | No | No | Also return the longitude and latitude of each match. |
WITHDIST | No | No | Also return the distance from the center, in the requested unit. |
WITHHASH | No | No | Also return the raw 52-bit geohash score of each match. |
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Array of bulk-string members or member-detail arrays |
| RESP3 | Array of bulk-string members or member-detail arrays |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.