Redis GEORADIUSBYMEMBER command with example – Redis Tutorial

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get elements of geospatial value stored at a key, which fall under the specific area. For this we will use a Redis GEORADIUSBYMEMBER command.

GEORADIUSBYMEMBER Command

This command is used to return one or more members of geospatial value ( Sorted Set ) stored at a key, which are inside the border of the area calculated by using longitude, latitude value of specified member and radius arguments. This area is calculated by using specified member’s longitude, latitude value as a center location of circle and radius in specified unit as a radius of circle.

The radius is specified using one of the following unit :- 

  1. m for meter ( default ).
  2. km for kilometer.
  3. mi for miles.
  4. ft for feet.

Optional Arguments

The Redis GEORADIUSBYMEMBER command takes following optional arguments :-

  1. WITHDIST :- It returns the distance of the returned elements from the center of the circle. The unit of distance is same as the unit of the radius argument.
  2. WITHCOORD :- It returns the longitude, latitude coordinates of all the returned elements.
  3. WITHHASH :- It returns the raw Geohash string ( 52 bit unsigned integer ) of all the returned elements. This Geohash string is the score of the an element in the sorted set.
  4. ASC :- It returns the elements from nearest to farthest sorting order, relative to the center. By default elements are returned in unsorted order.
  5. DESC :- It returns the elements from farthest to nearest sorting order, relative to the center.
  6. COUNT <count> :- It returns the elements limited to first count matching elements. By default all the matching elements are returned.

Return Values

This command returns an array reply with following type of values :-  

  1. If [WITH] optional argument are not specified, this command returns an array containing names of element.
  2. If WITHCOORD, WITHDIST, WITHHASH options are specified, this command returns an array of arrays, where each sub array represents a single element. The first item in the sub array is always the name of the returned element and other information are returned in the following order :-
    1. The distance from the center as a floating point number, in the same unit specified in the radius.
    2. The geohash integer.
    3. The coordinates as a two items x,y array (longitude,latitude).

Nil is returned when key does not exist and error is returned when key exist but value stored at the key is not of sorted set datatype, populated using GEOADD command. The syntax of redis GEORADIUSBYMEMBER command is as follows :-

Syntax :-

redis host:post> GEORADIUSBYMEMBER <keyname> <member> <radius> [UNIT] [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]

Output :- 

- (array) reply, representing the elements of geospatial values.
- Nil, if key does not exist.
- Error, if key exist and value stored at the key is not a sorted set populated using GEOADD command.

Example :-

Redis GEORADIUSBYMEMBER

References :-

  1. Redis GEORADIUSBYMEMBER Command Docs

That’s all for how to get elements of geospatial value stored in redis datastore, which fall under the specific area. If you liked it, please share your thoughts in comments section and share it with others too.

<- Geo Commands