Search code examples
redisgeolocation

How to get intersection of 2 sets in redis?


I have 2 sets in Redis, one is made by hmset:

HMSET cars:1 make Ferrari model 458 color red topSpeed 202mph

then:

SADD make:Ferrari 1

and when I enter this command:

SMEMBERS make:Ferrari

I get "1" then I add:geoadd carLoc 13.361389 38.115556 1

I am trying to get intersection of these 2 sets, with this command: georadius carLoc 13.361389 38.115556 1 store key1 I filter the first set and store in another set but I can't find a way to store the result of SMEMBERS make:Ferrari to another set and get intersection of them. Can anybody help me?


Solution

  • If the problem is that you want to find the intersection of a set and a zset, then there is no native way of doing this. You have two options that I can tell:

    1. Convert make:Ferrari to a zset, choosing some arbitrary weights
    2. Create a lua script to do this programmatically

    In general, #1 would be easier to do if it is at all possible to convert that key to a zset. If it's not easy to do, then consider creating two keys: one is a set make:Ferrari, and another is a zset z:make:Ferrari and use the second for the intersection.