Redis GEORADIUSBYMEMBER command with example – Redis Tutorial
1 week 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 redis GEORADIUSBYMEMBER command in redis-cli.
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 :-
- m for meter ( default ).
- km for kilometer.
- mi for miles.
- ft for feet.
The Redis GEORADIUSBYMEMBER command takes following optional arguments :-
- 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.
- WITHCOORD :- It returns the longitude, latitude coordinates of all the returned elements.
- 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.
- ASC :- It returns the elements from nearest to farthest sorting order, relative to the center. By default elements are returned in unsorted order.
- DESC :- It returns the elements from farthest to nearest sorting order, relative to the center.
- COUNT <count> :- It returns the elements limited to first count matching elements. By default all the matching elements are returned.
This command returns an array reply with following type of values :-
- If [WITH] optional argument are not specified, this command returns an array containing names of element.
- 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 :-
- The distance from the center as a floating point number, in the same unit specified in the radius.
- The geohash integer.
- 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 :-
redis host:post> GEORADIUSBYMEMBER <keyname> <member> <radius> [UNIT] [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
- (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.
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.