Sorted indices for Ohm
Sortedmodules in your model:
include Ohm::Callbacks include Ohm::Sorted
Add the sorted indices you want to your model:
If you want a complete index:
If you want to partition the index based on an attribute:
sorted :created_at, :group_by => :site_id
You can use both indices for the same attribute, as the partition keys are
namespaced by the
You will need to resave every model if they already exist for the index to get built.
The ranking attribute must be of a type that responds to
To query the sorted index, use the
sorted_find class method.
>> Post.sorted_find(:created_at, site_id: "ar")
This returns an
Ohm::SortedSet, similiar to an
Ohm::Set but backed by a sorted
To limit the results to a certain score range, use the
This and the following methods return a new copy of the set object, but data
is not read until it is necessary.
>> Post.sorted_find(:created_at, site_id: "ar").between(start_time, end_time)
To take a slice of the results, use the
slice method. This returns a new copy
of the set which will pass its
count parameters to Redis.
>> Post.sorted_find(:created_at, site_id: "ar").slice(2, 4)
This plugin works with Ohm versions higher than 0.1.3.
Many thanks to Damian Janowski (https://github.com/djanowski)