Intersect set and sorted set?

507 views
Skip to first unread message

paulsc

unread,
Nov 23, 2011, 12:19:40 PM11/23/11
to Redis DB
Hello everyone,

Long time lurker, first time poster. Does anybody know if there is a
way to use a redis command to intersect a set and a sorted set in
memory on the redis server ?

Thanks,

Paul

Dvir Volk

unread,
Nov 23, 2011, 12:27:50 PM11/23/11
to redi...@googlegroups.com
not without scripting...


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.




--
Dvir Volk
System Architect, DoAT, http://doat.com

Josiah Carlson

unread,
Nov 23, 2011, 12:52:42 PM11/23/11
to redi...@googlegroups.com
I'm sorry, but Dvir is incorrect.

You can intersect sets and sorted sets via zinterstore (or union them
via zunionstore). Scores for sets are defaulted to 1, but you can
adjust the scores on the entire set by using the weights parameter. I
managed to get that patch in a little over a year ago ;)

Regards,
- Josiah

Dvir Volk

unread,
Nov 23, 2011, 3:33:08 PM11/23/11
to redi...@googlegroups.com, Josiah Carlson
wow, I haven't been so happy being wrong about something, in a long time!
this is really useful, I can't believe I've missed it in the documentation!

thanks a lot for the patch, Josiah!

Josiah Carlson

unread,
Nov 23, 2011, 4:14:35 PM11/23/11
to Dvir Volk, redi...@googlegroups.com
I'm glad to help,
- Josiah

kehai chen

unread,
Nov 23, 2011, 11:39:35 PM11/23/11
to redi...@googlegroups.com
I am s starter ,are you list seveal demo ? 

2011/11/24 Josiah Carlson <josiah....@gmail.com>

Josiah Carlson

unread,
Nov 24, 2011, 4:06:13 AM11/24/11
to redi...@googlegroups.com
Here is an example in Python:

>>> import redis
>>> c = redis.Redis()
>>> c.sadd('foo', 'bar')
True
>>> c.sadd('foo', 'baz')
True
>>> c.zadd('goo', 'baz', 3)
True
>>> c.zadd('goo', 'boo', 2)
True
>>> c.zinterstore('out', ['foo', 'goo']) # the default aggregate is SUM
1L
>>> c.zrange('out', 0, -1, withscores=True)
[('baz', 4.0)]
>>> c.zunionstore('out', ['foo', 'goo'])
3L
>>> c.zrange('out', 0, -1, withscores=True)
[('bar', 1.0), ('boo', 2.0), ('baz', 4.0)]
>>>

Regards,
- Josiah

Reply all
Reply to author
Forward
0 new messages