ankane / disco

Recommendations for Ruby and Rails using collaborative filtering
MIT License
565 stars 11 forks source link

Recommendations for multiple products? #30

Closed mbajur closed 2 months ago

mbajur commented 3 months ago

Hey! I was wondering - would that be possible to get recommendations for multiple items and not just one? Let's say i want to display a list of recommendations on an e-commerce Cart view page. Cart can have multiple items and i would love to be able to display reccomendations for all of them at once, with something like Product.recommendations_for([item1.id, item2.id, item3.id])

Thank you!

ankane commented 2 months ago

Hi @mbajur, you'll probably want to use a different library for online recommendations, but you can use the recommendation model directly to get multiple items.

recommended_products =
  Disco::Recommendation
    .where(context: "products", subject: [item1, item2, item3])
    .order(score: :desc).includes(:item).map(&:item).uniq(&:id)