Upstash Documentation

GEOSEARCH

Search for members in an area.
3 min read

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#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
(FROMMEMBER <member> | FROMLONLAT <longitude> <latitude>)YesNoWhere 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))YesNoThe 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)NoNoSort the matches by distance from the center: ASC (nearest first) or DESC (farthest first). Unsorted when omitted.
COUNT <count> [ANY]NoNoReturn at most <count> matches. ANY returns as soon as enough matches are found, instead of sorting every match first.
WITHCOORDNoNoAlso return the longitude and latitude of each match.
WITHDISTNoNoAlso return the distance from the center, in the requested unit.
WITHHASHNoNoAlso 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.

ProtocolReply
RESP2Array of bulk-string members or member-detail arrays
RESP3Array of bulk-string members or member-detail arrays
Note

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.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs