Can liked_by_ids be paginated ? #79

Closed
sharp opened this Issue Jun 14, 2013 · 12 comments

Comments

Projects
None yet
3 participants

sharp commented Jun 14, 2013

Or else it fetches too many records , > 1000000 in my application

hderms commented Jun 14, 2013

Interesting request. I'll consider looking into it tomorrow.

On Thu, Jun 13, 2013 at 9:29 PM, Sharp notifications@github.com wrote:

or else it fetches too many records , > 1000000 in my application


Reply to this email directly or view it on GitHubhttps://github.com/davidcelis/recommendable/issues/79
.

Owner

davidcelis commented Jun 14, 2013

Not possible with Redis. All of the things having to do with likes, dislikes, bookmarks, and hidden items, are stored as SETs. In Redis, you have two choices: get a random member of a SET, or get every member of a SET.

If you're fetching actual records, you should rely on whatever pagination is possible with your ORM. If you're using ActiveRecord, for instance, you'd want to use limit and offset:

@record.liked_by.limit(25).offset(50) # fetches page 3 if you paginate by 25 items

If, for some reason, you must get IDs (using liked_by_ids) and then use those IDs to get records some other way, just use Ruby to slice the Array up. Or use kaminari, which can paginate arrays.

The only thing in Recommendable that it would make sense to paginate would be Recommendations and Similar Users and, since those both use ZSETs, it's totally doable. I'd be more than willing to accept a patch for that or, if you're willing to wait, I can code it up when I have a bit of free time.

@davidcelis davidcelis closed this Jun 14, 2013

sharp commented Jun 14, 2013

Thanks

BTW: My case is: when one user likes some subjects, then system will recommend him some users who also liked these subjects, so i plan to get these user ids and exclude the user ids who current use has been following.

davidcelis added a commit that referenced this issue Jun 14, 2013

Allow pagination of recommendations and users
A request made in #79 to paginate likes made me think this would be nice
for users.

```ruby
@user.recommended_movies(10, 20)
```

Signed-off-by: David Celis <me@davidcel.is>
Owner

davidcelis commented Jun 14, 2013

@sharpv Why can't you just use the built-in similar_raters method?

Owner

davidcelis commented Jun 14, 2013

BTW, recommendations and similar raters can now be paginated (I bumped the version):

user.similar_raters(10, 30) # 10 raters, offset by 30 (page 4)

user.recommended_movies(25, 50) # 25 movies, offset by 50 (page 3)

hderms commented Jun 14, 2013

It would be possible if you stored the data both in a set and a list, right?

Owner

davidcelis commented Jun 14, 2013

Sure, but no way I'd do that in a system where I'm storing tons of ratings, considering how Redis can end up using a lot of RAM.

sharp commented Jun 14, 2013

I have done this by :

scope :excluding_ids, lambda { |ids|
  where(['id NOT IN (?)', ids]) if ids.any?
}

scope :including_ids, lambda { |ids|
  where(['id IN (?)', ids]) if ids.any?
}

def recommand_users
  User.including_ids(similar_raters.map(&:id)).excluding_ids(liked_users.map(&:id).append(id)).order('created_at desc').limit(50)
end

I will improve it by new version of recommendable later.

sharp commented Jun 14, 2013

@davidcelis Doessimilar_raters can also return similar likers ? I can not find the related code.

Owner

davidcelis commented Jun 14, 2013

current_user.similar_raters, for instance, will return the 10 users that are most similar to current_user based on likes and dislikes.

Owner

davidcelis commented Jun 14, 2013

@sharpv Have you read the detailed guide linked in the README? https://davidcelis.github.io/recommendable

sharp commented Jun 14, 2013

@davidcelis I have read that , thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment