Yun Jae Jung
Yun Jae Jung

Reputation: 159

Sort Geospatial Data by Time Posted with Redis

I'm trying to sort a bunch of data that I get from georadius to be sorted reverse-chronologically based on when the data gets posted. So if I post item1, item3, item2, item4 and they're all within range of the georadius command it should return item4, item2, item3, and item1 regardless of distance. How would I do this with redis's geospatial commands? Thanks!

Upvotes: 1

Views: 436

Answers (1)

for_stack
for_stack

Reputation: 22926

First of all, you need to save the post time of each item. For example, you can set a key-value pair for each item, with key as item name, and value as post time.

When you post an item, add it into the GEO structure with geo info, and also record the post time:

  1. GEOADD geo lng lat item1
  2. SET posttime:item1 timestamp-in-seconds

When you want to do a search, take the following steps:

  1. Use GEORADIOUS command with STORE option to save the result into a sorted set. Let's name the sorted set as temp result.
  2. Use the SORT command to sort temp result with post time: SORT temp_result BY posttime:* DESC

Upvotes: 3

Related Questions