Reputation: 1706
I am reading the docs for redis geospatial and I see that I can store only a key
, latitude
, longitude
, and name
.
I have some hashes stored, such as events:id
, listings:id
, etc. Events for example holds the JSON for an event object. This is because these items don't change much and I am caching them in redis.
In order to find some events
within some radius, how can I do that?
Would I have to do something like this?
GEOADD [event:id] {event.latitude} {event.longitude} {event.id}
and then map these against the events:id
hash?
Upvotes: 2
Views: 1211
Reputation: 6764
GEOADD key longitude latitude member
adds an entry (member
with longitude
and latitude
) to a sorted set key
, the geospatial index. The sorted is created if it doesn't exist.
To be able to query for events within some radius, you want to have all your events in the same geospatial sorted set.
This means you add all to the same key using:
GEOADD events:location {event.longitude} {event.latitude} {event.id}
You can add more than one event at a time:
GEOADD events:location 13.361389 38.115556 "event:1" 15.087269 37.502669 "event:2"
Note longitude goes first
Then you get all events near to a user-defined location using GEORADIUS
> GEORADIUS events:location 15 37 200 km WITHDIST
1) 1) "event:1"
2) "190.4424"
2) 1) "event:2"
2) "56.4413"
Other commands available are:
GEODIST
- the distance between two membersGEOHASH
- Geohash strings representing the positionGEOPOS
- the positions (longitude, latitude) of all the specified membersGEORADIUSBYMEMBER
- as GEORADIUS
but it takes the name of a member already existing inside the geospatial indexYou can also use the sorted set commands on the geospatial index. For example, to get how many events you have on the geospatial index:
> ZCARD events:location
(integer) 2
You can have your whole JSON-encoded event as the member of the geospatial index, or just the event:id
which is also key to another key with the event data, your call.
Upvotes: 2