Skip to content

7. Getting Recommendations

Jesus Baron edited this page Jul 15, 2022 · 3 revisions

Getting Recommendations

At this point, you are set and ready to start getting personalized recommendations for your users.

By default, results are limited to 25 entries, you may specify a different limit by chaining the take method. Due to Recombee's own limitations, results cannot be paginated.

Recommendations for a given user

Using Facade

// Recommends items to a given user, typically used in a "Picked just for you" section.
Recombee::user($userId)->recommendItems()->get();

// Recommends similar users to some given user, based on the user's past interactions and values of properties.
Recombee::user($userId)->recommendUsers()->get();

Using Model Instance

// Recommends items to a given user, typically used in a "Picked just for you" section.
User::first()->recommendItems()->get();

// Recommends similar users to some given user, based on the user's past interactions and values of properties.
User::first()->recommendUsers()->get();

Recommendations for a given item

Using Facade

// Recommends items that are related to a given item, typically used in a "Similar Products" section.
Recombee::item($itemId)->recommendItems()->get();

// Recommends users that are likely to be interested in a given item.
Recombee::item($itemId)->recommendUsers()->get();

Using Model Instance

// Recommends items that are related to a given item, typically used in a "Similar Products" section.
Item::first()->recommendItems()->get();

// Recommends users that are likely to be interested in a given item.
Item::first()->recommendUsers()->get();

I highly encourage you to visit the Recombee API Reference for more details on recommendations and all the customizable options available which can be passed by chaining the option method.

Getting "Next Page"

Due to Recombee's design, recommendations cannot be paginated but we can get a similar result by passing a base recommendation ID (which is returned on every recommendations call as metadata) as an argument to the recommendItems method, this way all the parameters will be inherited from the base request and it will return different (previously not recommended) items.

Typically, this is used to return items that shall be shown to a user as next recommendations when the user e.g. scrolls the page down (infinite scroll) or goes to a next page. Please visit Recombee API Reference for more information.

// 1st call for recommendations
$results1 = User::first()->recommendItems()->get();
$baseRecommendationId = $results1->additional['meta']['recommId'];

// 2nd call for recommendations
$results2 = User::first()->recommendItems($baseRecommendationId)->get();
$baseRecommendationId = $results2->additional['meta']['recommId'];

// 3rd call for recommendations
$results3 = User::first()->recommendItems($baseRecommendationId)->get();
$baseRecommendationId = $results3->additional['meta']['recommId'];

Customize number of results

Results are limited to 25 entries by default, but you may chain the take method to customize the number of results.

Recombee::user($userId)->recommendItems()->take(50)->get();

Customize fields returned

By default, all the user/item properties are returned in the response, you may customize the properties to return by chaining the select method and passing the property names as multiple arguments.

Recombee::user($userId)->recommendItems()->get();                             # It will return all the properties.
Recombee::user($userId)->recommendItems()->select()->get();                   # It will return the ID only.
Recombee::user($userId)->recommendItems()->select('brand', 'price')->get();   # It will return the ID, brand, and price properties.

Passing Additional Options

Recombee can accept additional options to customize the results even further, for example, you may send the filter option which accepts a ReQL expression that allows you to filter users to be listed. This and any other option can be added to the query by chaining the option method which takes the option name as the 1st argument and its value as the 2nd argument.

Please visit the Recombee API Reference for all available options.

Recombee::user($userId)
    ->recommendItems()
    ->take(50)
    ->select('brand', 'price')
    ->option('filter', "'price' > 250")
    ->option('scenario', 'default')
    ->option('logic', 'popular')
    ->get();
Clone this wiki locally