recommendations: filter out own uploads and favorites.

Filter out the user's own uploads and favorites from their
recommendations.

Note that in most cases a user's top-N recommendations will be things
they've already favorited. If a user has 10,000 favorites, most of their
top 10,000 recommendations will be their own favorites, so we have to
generate a little more than 10,000 recommendations to be sure they won't
all be filtered out.

In other words, the more favorites a user has, the more recommendations
we have to generate. The upper bound is clamped to 50,000 for
performance reasons. If a user has more favorites than this we may not
be able to find any recommendations for them.
This commit is contained in:
evazion
2019-12-01 18:36:28 -06:00
parent 41b30fc64c
commit fa985f08ab
2 changed files with 30 additions and 15 deletions

View File

@@ -2,17 +2,10 @@ class RecommendedPostsController < ApplicationController
respond_to :html, :json, :xml, :js
def show
@max_recommendations = params.fetch(:max_recommendations, 100).to_i.clamp(0, 1000)
if params[:user_id].present?
@recs = RecommenderService.recommend_for_user(params[:user_id], @max_recommendations)
elsif params[:post_id].present?
@recs = RecommenderService.recommend_for_post(params[:post_id], @max_recommendations)
else
@recs = []
end
limit = params.fetch(:limit, 100).to_i.clamp(0, 200)
@recs = RecommenderService.search(params).take(limit)
@posts = @recs.map { |rec| rec[:post] }
respond_with(@recs)
end
end