Skip to content

Commit 089b7e8

Browse files
committed
Support SearchItems endpoint
1 parent 6dfb0b6 commit 089b7e8

11 files changed

+606
-186
lines changed

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ or
1717
```
1818
{
1919
"require": {
20-
"recombee/php-api-client": "^2.4.0"
20+
"recombee/php-api-client": "^3.0.0"
2121
}
2222
}
2323
```
@@ -75,7 +75,7 @@ const NUM = 100;
7575
const PROBABILITY_PURCHASED = 0.1;
7676

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

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

137137
// Get 5 items related to item computer-6. Personalize them for user-42, who is currently viewing that item.
138-
$recommended = $client->send(new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5));
139-
echo 'Recommended items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";
140-
141138
// Recommend only computers that have at least 3 cores
142139
$recommended = $client->send(
143140
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['filter' => "'num-cores'>=3"])
@@ -149,7 +146,21 @@ $recommended = $client->send(
149146
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5,
150147
['filter' => "'price' > context_item[\"price\"]"])
151148
);
152-
echo 'Recommended up-sell items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n"
149+
echo 'Recommended up-sell items: ' . json_encode($recommended, JSON_PRETTY_PRINT) . "\n";
150+
151+
152+
// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)
153+
// when scenario is specified
154+
$recommended = $client->send(
155+
new Reqs\RecommendItemsToItem('computer-6', 'user-42', 5, ['scenario' => 'product_detail'])
156+
);
157+
158+
// Perform personalized full-text search with a user's search query (e.g. 'computers')
159+
$matches = $client->send(
160+
new Reqs\SearchItems('user-42', 'computers', 5)
161+
);
162+
echo 'Matched items: ' . json_encode($matches, JSON_PRETTY_PRINT) . "\n";
163+
153164
```
154165

155166
### Exception handling

src/RecommApi/Client.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ public function __construct($account, $token, $protocol = 'https', $options= arr
4545
$this->token = $token;
4646
$this->protocol = $protocol;
4747
$this->base_uri = Client::BASE_URI;
48-
if(getenv("RAPI_URI") !== false)
49-
$this->base_uri = getenv("RAPI_URI");
5048
$this->options = $options;
49+
50+
if(getenv('RAPI_URI') !== false)
51+
$this->base_uri = getenv('RAPI_URI');
52+
else if (isset($this->options['baseUri']))
53+
$this->base_uri = $this->options['baseUri'];
5154
$this->user_agent = $this->getUserAgent();
5255
}
5356

5457
protected function getUserAgent() {
55-
$user_agent = 'recombee-php-api-client/2.4.0';
58+
$user_agent = 'recombee-php-api-client/3.0.0';
5659
if (isset($this->options['serviceName']))
5760
$user_agent .= ' '.($this->options['serviceName']);
5861
return $user_agent;

src/RecommApi/Requests/RecommendItemsToItem.php

+52-42
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,15 @@ class RecommendItemsToItem extends Request {
4141
*/
4242
protected $count;
4343
/**
44-
* @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.
45-
*/
46-
protected $filter;
47-
/**
48-
* @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.
44+
* @var string $scenario Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
45+
* 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.
46+
* 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.
4947
*/
50-
protected $booster;
48+
protected $scenario;
5149
/**
5250
* @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.
5351
*/
5452
protected $cascade_create;
55-
/**
56-
* @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.
57-
*/
58-
protected $scenario;
59-
/**
60-
* @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.
61-
* See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
62-
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
63-
*/
64-
protected $logic;
6553
/**
6654
* @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.
6755
* Example response:
@@ -120,6 +108,23 @@ class RecommendItemsToItem extends Request {
120108
* ```
121109
*/
122110
protected $included_properties;
111+
/**
112+
* @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.
113+
* Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
114+
*/
115+
protected $filter;
116+
/**
117+
* @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.
118+
* Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
119+
*/
120+
protected $booster;
121+
/**
122+
* @var string| $logic Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
123+
* See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
124+
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
125+
* Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
126+
*/
127+
protected $logic;
123128
/**
124129
* @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`.
125130
*/
@@ -172,23 +177,14 @@ class RecommendItemsToItem extends Request {
172177
* @param int $count Number of items to be recommended (N for the top-N recommendation).
173178
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
174179
* - Allowed parameters:
175-
* - *filter*
176-
* - Type: string
177-
* - 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.
178-
* - *booster*
180+
* - *scenario*
179181
* - Type: string
180-
* - 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.
182+
* - Description: Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
183+
* 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.
184+
* 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.
181185
* - *cascadeCreate*
182186
* - Type: bool
183187
* - 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.
184-
* - *scenario*
185-
* - Type: string
186-
* - 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.
187-
* - *logic*
188-
* - Type: string|
189-
* - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
190-
* See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
191-
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
192188
* - *returnProperties*
193189
* - Type: bool
194190
* - 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.
@@ -245,6 +241,20 @@ class RecommendItemsToItem extends Request {
245241
* ]
246242
* }
247243
* ```
244+
* - *filter*
245+
* - Type: string
246+
* - 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.
247+
* Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
248+
* - *booster*
249+
* - Type: string
250+
* - 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.
251+
* Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
252+
* - *logic*
253+
* - Type: string|
254+
* - Description: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
255+
* See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
256+
* The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
257+
* Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
248258
* - *userImpact*
249259
* - Type: float
250260
* - 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`.
@@ -272,13 +282,13 @@ public function __construct($item_id, $target_user_id, $count, $optional = array
272282
$this->item_id = $item_id;
273283
$this->target_user_id = $target_user_id;
274284
$this->count = $count;
275-
$this->filter = isset($optional['filter']) ? $optional['filter'] : null;
276-
$this->booster = isset($optional['booster']) ? $optional['booster'] : null;
277-
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
278285
$this->scenario = isset($optional['scenario']) ? $optional['scenario'] : null;
279-
$this->logic = isset($optional['logic']) ? $optional['logic'] : null;
286+
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
280287
$this->return_properties = isset($optional['returnProperties']) ? $optional['returnProperties'] : null;
281288
$this->included_properties = isset($optional['includedProperties']) ? $optional['includedProperties'] : null;
289+
$this->filter = isset($optional['filter']) ? $optional['filter'] : null;
290+
$this->booster = isset($optional['booster']) ? $optional['booster'] : null;
291+
$this->logic = isset($optional['logic']) ? $optional['logic'] : null;
282292
$this->user_impact = isset($optional['userImpact']) ? $optional['userImpact'] : null;
283293
$this->diversity = isset($optional['diversity']) ? $optional['diversity'] : null;
284294
$this->min_relevance = isset($optional['minRelevance']) ? $optional['minRelevance'] : null;
@@ -288,7 +298,7 @@ public function __construct($item_id, $target_user_id, $count, $optional = array
288298
$this->return_ab_group = isset($optional['returnAbGroup']) ? $optional['returnAbGroup'] : null;
289299
$this->optional = $optional;
290300

291-
$existing_optional = array('filter','booster','cascadeCreate','scenario','logic','returnProperties','includedProperties','userImpact','diversity','minRelevance','rotationRate','rotationTime','expertSettings','returnAbGroup');
301+
$existing_optional = array('scenario','cascadeCreate','returnProperties','includedProperties','filter','booster','logic','userImpact','diversity','minRelevance','rotationRate','rotationTime','expertSettings','returnAbGroup');
292302
foreach ($this->optional as $key => $value) {
293303
if (!in_array($key, $existing_optional))
294304
throw new UnknownOptionalParameterException($key);
@@ -330,20 +340,20 @@ public function getBodyParameters() {
330340
$p = array();
331341
$p['targetUserId'] = $this->target_user_id;
332342
$p['count'] = $this->count;
333-
if (isset($this->optional['filter']))
334-
$p['filter'] = $this-> optional['filter'];
335-
if (isset($this->optional['booster']))
336-
$p['booster'] = $this-> optional['booster'];
337-
if (isset($this->optional['cascadeCreate']))
338-
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
339343
if (isset($this->optional['scenario']))
340344
$p['scenario'] = $this-> optional['scenario'];
341-
if (isset($this->optional['logic']))
342-
$p['logic'] = $this-> optional['logic'];
345+
if (isset($this->optional['cascadeCreate']))
346+
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
343347
if (isset($this->optional['returnProperties']))
344348
$p['returnProperties'] = $this-> optional['returnProperties'];
345349
if (isset($this->optional['includedProperties']))
346350
$p['includedProperties'] = $this-> optional['includedProperties'];
351+
if (isset($this->optional['filter']))
352+
$p['filter'] = $this-> optional['filter'];
353+
if (isset($this->optional['booster']))
354+
$p['booster'] = $this-> optional['booster'];
355+
if (isset($this->optional['logic']))
356+
$p['logic'] = $this-> optional['logic'];
347357
if (isset($this->optional['userImpact']))
348358
$p['userImpact'] = $this-> optional['userImpact'];
349359
if (isset($this->optional['diversity']))

0 commit comments

Comments
 (0)