Skip to content

Commit

Permalink
Support SearchItems endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraFiedler committed Jan 29, 2020
1 parent 6dfb0b6 commit 089b7e8
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 186 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ or
```
{
"require": {
"recombee/php-api-client": "^2.4.0"
"recombee/php-api-client": "^3.0.0"
}
}
```
Expand Down Expand Up @@ -75,7 +75,7 @@ const NUM = 100;
const PROBABILITY_PURCHASED = 0.1;

$client = new Client('--my-database-id--', '--db-private-token--');
$client->send(new Reqs\ResetDatabase()); //Clear everything from the database
$client->send(new Reqs\ResetDatabase()); // Clear everything from the database

/*
We will use computers as items in this example
Expand Down Expand Up @@ -135,9 +135,6 @@ for($i=0; $i<NUM; $i++)
$client->send(new Reqs\Batch($requests));

// Get 5 items related to item computer-6. Personalize them for user-42, who is currently viewing that item.
$recommended = $client->send(new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5));
echo 'Recommended items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";

// Recommend only computers that have at least 3 cores
$recommended = $client->send(
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['filter' => "'num-cores'>=3"])
Expand All @@ -149,7 +146,21 @@ $recommended = $client->send(
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5,
['filter' => "'price' > context_item[\"price\"]"])
);
echo 'Recommended up-sell items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n"
echo 'Recommended up-sell items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";


// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)
// when scenario is specified
$recommended = $client->send(
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['scenario' => 'product_detail'])
);

// Perform personalized full-text search with a user's search query (e.g. 'computers')
$matches = $client->send(
new Reqs\SearchItems('user-42', 'computers', 5)
);
echo 'Matched items: ' . json_encode($matches, JSON_PRETTY_PRINT) . "\n";

```

### Exception handling
Expand Down
9 changes: 6 additions & 3 deletions src/RecommApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ public function __construct($account, $token, $protocol = 'https', $options= arr
$this->token = $token;
$this->protocol = $protocol;
$this->base_uri = Client::BASE_URI;
if(getenv("RAPI_URI") !== false)
$this->base_uri = getenv("RAPI_URI");
$this->options = $options;

if(getenv('RAPI_URI') !== false)
$this->base_uri = getenv('RAPI_URI');
else if (isset($this->options['baseUri']))
$this->base_uri = $this->options['baseUri'];
$this->user_agent = $this->getUserAgent();
}

protected function getUserAgent() {
$user_agent = 'recombee-php-api-client/2.4.0';
$user_agent = 'recombee-php-api-client/3.0.0';
if (isset($this->options['serviceName']))
$user_agent .= ' '.($this->options['serviceName']);
return $user_agent;
Expand Down
94 changes: 52 additions & 42 deletions src/RecommApi/Requests/RecommendItemsToItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,15 @@ class RecommendItemsToItem extends Request {
*/
protected $count;
/**
* @var string $filter Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to filter recommended items based on the values of their attributes.
*/
protected $filter;
/**
* @var string $booster Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes.
* @var string $scenario Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
* You can set various settings to the [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). You can also see performance of each scenario in the Admin UI separately, so you can check how well each application performs.
* The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
*/
protected $booster;
protected $scenario;
/**
* @var bool $cascade_create If item of given *itemId* or user of given *targetUserId* doesn't exist in the database, it creates the missing entity/entities and returns some (non-personalized) recommendations. This allows for example rotations in the following recommendations for the user of given *targetUserId*, as the user will be already known to the system.
*/
protected $cascade_create;
/**
* @var string $scenario Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing". You can see each scenario in the UI separately, so you can check how well each application performs. The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
*/
protected $scenario;
/**
* @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
* See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
*/
protected $logic;
/**
* @var bool $return_properties With `returnProperties=true`, property values of the recommended items are returned along with their IDs in a JSON dictionary. The acquired property values can be used for easy displaying of the recommended items to the user.
* Example response:
Expand Down Expand Up @@ -120,6 +108,23 @@ class RecommendItemsToItem extends Request {
* ```
*/
protected $included_properties;
/**
* @var string $filter Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to filter recommended items based on the values of their attributes.
* Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
*/
protected $filter;
/**
* @var string $booster Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes.
* Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
*/
protected $booster;
/**
* @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
* See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
* Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
*/
protected $logic;
/**
* @var float $user_impact **Expert option** If *targetUserId* parameter is present, the recommendations are biased towards the given user. Using *userImpact*, you may control this bias. For an extreme case of `userImpact=0.0`, the interactions made by the user are not taken into account at all (with the exception of history-based blacklisting), for `userImpact=1.0`, you'll get user-based recommendation. The default value is `0`.
*/
Expand Down Expand Up @@ -172,23 +177,14 @@ class RecommendItemsToItem extends Request {
* @param int $count Number of items to be recommended (N for the top-N recommendation).
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
* - Allowed parameters:
* - *filter*
* - Type: string
* - Description: Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to filter recommended items based on the values of their attributes.
* - *booster*
* - *scenario*
* - Type: string
* - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes.
* - Description: Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
* You can set various settings to the [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com). You can also see performance of each scenario in the Admin UI separately, so you can check how well each application performs.
* The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
* - *cascadeCreate*
* - Type: bool
* - Description: If item of given *itemId* or user of given *targetUserId* doesn't exist in the database, it creates the missing entity/entities and returns some (non-personalized) recommendations. This allows for example rotations in the following recommendations for the user of given *targetUserId*, as the user will be already known to the system.
* - *scenario*
* - Type: string
* - Description: Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing". You can see each scenario in the UI separately, so you can check how well each application performs. The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
* - *logic*
* - Type: string|
* - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
* See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
* - *returnProperties*
* - Type: bool
* - Description: With `returnProperties=true`, property values of the recommended items are returned along with their IDs in a JSON dictionary. The acquired property values can be used for easy displaying of the recommended items to the user.
Expand Down Expand Up @@ -245,6 +241,20 @@ class RecommendItemsToItem extends Request {
* ]
* }
* ```
* - *filter*
* - Type: string
* - Description: Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to filter recommended items based on the values of their attributes.
* Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
* - *booster*
* - Type: string
* - Description: Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes.
* Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
* - *logic*
* - Type: string|
* - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
* See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
* Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
* - *userImpact*
* - Type: float
* - Description: **Expert option** If *targetUserId* parameter is present, the recommendations are biased towards the given user. Using *userImpact*, you may control this bias. For an extreme case of `userImpact=0.0`, the interactions made by the user are not taken into account at all (with the exception of history-based blacklisting), for `userImpact=1.0`, you'll get user-based recommendation. The default value is `0`.
Expand Down Expand Up @@ -272,13 +282,13 @@ public function __construct($item_id, $target_user_id, $count, $optional = array
$this->item_id = $item_id;
$this->target_user_id = $target_user_id;
$this->count = $count;
$this->filter = isset($optional['filter']) ? $optional['filter'] : null;
$this->booster = isset($optional['booster']) ? $optional['booster'] : null;
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
$this->scenario = isset($optional['scenario']) ? $optional['scenario'] : null;
$this->logic = isset($optional['logic']) ? $optional['logic'] : null;
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
$this->return_properties = isset($optional['returnProperties']) ? $optional['returnProperties'] : null;
$this->included_properties = isset($optional['includedProperties']) ? $optional['includedProperties'] : null;
$this->filter = isset($optional['filter']) ? $optional['filter'] : null;
$this->booster = isset($optional['booster']) ? $optional['booster'] : null;
$this->logic = isset($optional['logic']) ? $optional['logic'] : null;
$this->user_impact = isset($optional['userImpact']) ? $optional['userImpact'] : null;
$this->diversity = isset($optional['diversity']) ? $optional['diversity'] : null;
$this->min_relevance = isset($optional['minRelevance']) ? $optional['minRelevance'] : null;
Expand All @@ -288,7 +298,7 @@ public function __construct($item_id, $target_user_id, $count, $optional = array
$this->return_ab_group = isset($optional['returnAbGroup']) ? $optional['returnAbGroup'] : null;
$this->optional = $optional;

$existing_optional = array('filter','booster','cascadeCreate','scenario','logic','returnProperties','includedProperties','userImpact','diversity','minRelevance','rotationRate','rotationTime','expertSettings','returnAbGroup');
$existing_optional = array('scenario','cascadeCreate','returnProperties','includedProperties','filter','booster','logic','userImpact','diversity','minRelevance','rotationRate','rotationTime','expertSettings','returnAbGroup');
foreach ($this->optional as $key => $value) {
if (!in_array($key, $existing_optional))
throw new UnknownOptionalParameterException($key);
Expand Down Expand Up @@ -330,20 +340,20 @@ public function getBodyParameters() {
$p = array();
$p['targetUserId'] = $this->target_user_id;
$p['count'] = $this->count;
if (isset($this->optional['filter']))
$p['filter'] = $this-> optional['filter'];
if (isset($this->optional['booster']))
$p['booster'] = $this-> optional['booster'];
if (isset($this->optional['cascadeCreate']))
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
if (isset($this->optional['scenario']))
$p['scenario'] = $this-> optional['scenario'];
if (isset($this->optional['logic']))
$p['logic'] = $this-> optional['logic'];
if (isset($this->optional['cascadeCreate']))
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
if (isset($this->optional['returnProperties']))
$p['returnProperties'] = $this-> optional['returnProperties'];
if (isset($this->optional['includedProperties']))
$p['includedProperties'] = $this-> optional['includedProperties'];
if (isset($this->optional['filter']))
$p['filter'] = $this-> optional['filter'];
if (isset($this->optional['booster']))
$p['booster'] = $this-> optional['booster'];
if (isset($this->optional['logic']))
$p['logic'] = $this-> optional['logic'];
if (isset($this->optional['userImpact']))
$p['userImpact'] = $this-> optional['userImpact'];
if (isset($this->optional['diversity']))
Expand Down
Loading

0 comments on commit 089b7e8

Please sign in to comment.