Skip to content

Commit b314778

Browse files
committed
Support SearchItems endpoint
1 parent 0971df8 commit b314778

20 files changed

+467
-109
lines changed

README.rst

+14-6
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ Using property values
7171
7272
from recombee_api_client.api_client import RecombeeClient
7373
from recombee_api_client.api_requests import AddItemProperty, SetItemValues, AddPurchase
74-
from recombee_api_client.api_requests import RecommendItemsToItem, Batch, ResetDatabase
74+
from recombee_api_client.api_requests import RecommendItemsToItem, SearchItems, Batch, ResetDatabase
7575
import random
7676
7777
NUM = 100
7878
PROBABILITY_PURCHASED = 0.1
7979
8080
client = RecombeeClient('--my-database-id--', '--db-private-token--')
8181
82-
#Clear the entire database
82+
# Clear the entire database
8383
client.send(ResetDatabase())
8484
8585
# We will use computers as items in this example
@@ -127,9 +127,6 @@ Using property values
127127
client.send(Batch(requests))
128128
129129
# Get 5 recommendations for user-42, who is currently viewing computer-6
130-
recommended = client.send(RecommendItemsToItem('computer-6', 'user-42', 5))
131-
print("Recommended items: %s" % recommended)
132-
133130
# Recommend only computers that have at least 3 cores
134131
recommended = client.send(
135132
RecommendItemsToItem('computer-6', 'user-42', 5, filter="'num-cores'>=3")
@@ -142,6 +139,16 @@ Using property values
142139
)
143140
print("Recommended up-sell items: %s" % recommended)
144141
142+
# Filters, boosters and other settings can be also set in the Admin UI (admin.recombee.com)
143+
# when scenario is specified
144+
recommended = client.send(
145+
RecommendItemsToItem('computer-6', 'user-42', 5, scenario='product_detail')
146+
)
147+
148+
# Perform personalized full-text search with a user's search query (e.g. 'computers').
149+
matches = client.send(SearchItems('user-42', 'computers', 5))
150+
print("Matched items: %s" % matches)
151+
145152
------------------
146153
Exception handling
147154
------------------
@@ -153,6 +160,7 @@ We are doing our best to provide the fastest and most reliable service, but prod
153160
Example:
154161

155162
.. code-block:: python
163+
from recombee_api_client.exceptions import *
156164
157165
try:
158166
recommended = client.send(
@@ -163,4 +171,4 @@ Example:
163171
except ApiTimeoutException as e:
164172
#Handle timeout => use fallback
165173
except APIException as e:
166-
#APIException is parent of both ResponseException and ApiTimeoutException
174+
#APIException is parent of both ResponseException and ApiTimeoutException

recombee_api_client/api_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def send(self, request):
6363

6464
@staticmethod
6565
def __get_http_headers(additional_headers=None):
66-
headers = {'User-Agent': 'recombee-python-api-client/2.4.0'}
66+
headers = {'User-Agent': 'recombee-python-api-client/3.0.0'}
6767
if additional_headers:
6868
headers.update(additional_headers)
6969
return headers

recombee_api_client/api_requests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from recombee_api_client.api_requests.recommend_users_to_user import RecommendUsersToUser
5858
from recombee_api_client.api_requests.recommend_items_to_item import RecommendItemsToItem
5959
from recombee_api_client.api_requests.recommend_users_to_item import RecommendUsersToItem
60+
from recombee_api_client.api_requests.search_items import SearchItems
6061
from recombee_api_client.api_requests.user_based_recommendation import UserBasedRecommendation
6162
from recombee_api_client.api_requests.item_based_recommendation import ItemBasedRecommendation
6263
from recombee_api_client.api_requests.reset_database import ResetDatabase

recombee_api_client/api_requests/recommend_items_to_item.py

+40-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RecommendItemsToItem(Request):
1313
1414
"""
1515

16-
def __init__(self, item_id, target_user_id, count, filter=DEFAULT, booster=DEFAULT, cascade_create=DEFAULT, scenario=DEFAULT, logic=DEFAULT, return_properties=DEFAULT, included_properties=DEFAULT, user_impact=DEFAULT, diversity=DEFAULT, min_relevance=DEFAULT, rotation_rate=DEFAULT, rotation_time=DEFAULT, expert_settings=DEFAULT, return_ab_group=DEFAULT):
16+
def __init__(self, item_id, target_user_id, count, scenario=DEFAULT, cascade_create=DEFAULT, return_properties=DEFAULT, included_properties=DEFAULT, filter=DEFAULT, booster=DEFAULT, logic=DEFAULT, user_impact=DEFAULT, diversity=DEFAULT, min_relevance=DEFAULT, rotation_rate=DEFAULT, rotation_time=DEFAULT, expert_settings=DEFAULT, return_ab_group=DEFAULT):
1717
"""
1818
Required parameters:
1919
@param item_id: ID of the item for which the recommendations are to be generated.
@@ -53,21 +53,16 @@ def __init__(self, item_id, target_user_id, count, filter=DEFAULT, booster=DEFAU
5353
5454
5555
Optional parameters:
56-
@param 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.
57-
58-
@param 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.
56+
@param scenario: Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
5957
60-
@param 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.
6158
62-
@param 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.
59+
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.
6360
64-
@param logic: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
6561
66-
See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
62+
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.
6763
6864
69-
The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
70-
65+
@param 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.
7166
7267
@param 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.
7368
@@ -167,6 +162,29 @@ def __init__(self, item_id, target_user_id, count, filter=DEFAULT, booster=DEFAU
167162
```
168163
169164
165+
@param 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.
166+
167+
168+
Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
169+
170+
171+
@param 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.
172+
173+
174+
Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
175+
176+
177+
@param logic: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
178+
179+
See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
180+
181+
182+
The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
183+
184+
185+
Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
186+
187+
170188
@param 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`.
171189
172190
@@ -192,13 +210,13 @@ def __init__(self, item_id, target_user_id, count, filter=DEFAULT, booster=DEFAU
192210
self.item_id = item_id
193211
self.target_user_id = target_user_id
194212
self.count = count
195-
self.filter = filter
196-
self.booster = booster
197-
self.cascade_create = cascade_create
198213
self.scenario = scenario
199-
self.logic = logic
214+
self.cascade_create = cascade_create
200215
self.return_properties = return_properties
201216
self.included_properties = included_properties
217+
self.filter = filter
218+
self.booster = booster
219+
self.logic = logic
202220
self.user_impact = user_impact
203221
self.diversity = diversity
204222
self.min_relevance = min_relevance
@@ -218,20 +236,20 @@ def get_body_parameters(self):
218236
p = dict()
219237
p['targetUserId'] = self.target_user_id
220238
p['count'] = self.count
221-
if self.filter is not DEFAULT:
222-
p['filter'] = self.filter
223-
if self.booster is not DEFAULT:
224-
p['booster'] = self.booster
225-
if self.cascade_create is not DEFAULT:
226-
p['cascadeCreate'] = self.cascade_create
227239
if self.scenario is not DEFAULT:
228240
p['scenario'] = self.scenario
229-
if self.logic is not DEFAULT:
230-
p['logic'] = self.logic
241+
if self.cascade_create is not DEFAULT:
242+
p['cascadeCreate'] = self.cascade_create
231243
if self.return_properties is not DEFAULT:
232244
p['returnProperties'] = self.return_properties
233245
if self.included_properties is not DEFAULT:
234246
p['includedProperties'] = self.included_properties
247+
if self.filter is not DEFAULT:
248+
p['filter'] = self.filter
249+
if self.booster is not DEFAULT:
250+
p['booster'] = self.booster
251+
if self.logic is not DEFAULT:
252+
p['logic'] = self.logic
235253
if self.user_impact is not DEFAULT:
236254
p['userImpact'] = self.user_impact
237255
if self.diversity is not DEFAULT:

recombee_api_client/api_requests/recommend_items_to_user.py

+44-22
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@ class RecommendItemsToUser(Request):
77
"""
88
Based on user's past interactions (purchases, ratings, etc.) with the items, recommends top-N items that are most likely to be of high value for a given user.
99
10+
The most typical use cases are recommendations at homepage, in some "Picked just for you" section or in email.
11+
1012
It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
1113
1214
The returned items are sorted by relevancy (first item being the most relevant).
1315
1416
"""
1517

16-
def __init__(self, user_id, count, filter=DEFAULT, booster=DEFAULT, cascade_create=DEFAULT, scenario=DEFAULT, logic=DEFAULT, return_properties=DEFAULT, included_properties=DEFAULT, diversity=DEFAULT, min_relevance=DEFAULT, rotation_rate=DEFAULT, rotation_time=DEFAULT, expert_settings=DEFAULT, return_ab_group=DEFAULT):
18+
def __init__(self, user_id, count, scenario=DEFAULT, cascade_create=DEFAULT, return_properties=DEFAULT, included_properties=DEFAULT, filter=DEFAULT, booster=DEFAULT, logic=DEFAULT, diversity=DEFAULT, min_relevance=DEFAULT, rotation_rate=DEFAULT, rotation_time=DEFAULT, expert_settings=DEFAULT, return_ab_group=DEFAULT):
1719
"""
1820
Required parameters:
19-
@param user_id: ID of the user for which personalized recommendations are to be generated.
21+
@param user_id: ID of the user for whom personalized recommendations are to be generated.
2022
2123
@param count: Number of items to be recommended (N for the top-N recommendation).
2224
2325
24-
Optional parameters:
25-
@param 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.
2626
27-
@param 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.
27+
Optional parameters:
28+
@param scenario: Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
2829
29-
@param cascade_create: If the user does not exist in the database, returns a list of non-personalized recommendations and creates the user in the database. This allows for example rotations in the following recommendations for that user, as the user will be already known to the system.
3030
31-
@param 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.
31+
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.
3232
33-
@param logic: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
3433
35-
See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
34+
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.
3635
3736
38-
The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
37+
@param cascade_create: If the user does not exist in the database, returns a list of non-personalized recommendations and creates the user in the database. This allows for example rotations in the following recommendations for that user, as the user will be already known to the system.
3938
4039
4140
@param 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.
@@ -136,6 +135,29 @@ def __init__(self, user_id, count, filter=DEFAULT, booster=DEFAULT, cascade_crea
136135
```
137136
138137
138+
@param 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.
139+
140+
141+
Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
142+
143+
144+
@param 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.
145+
146+
147+
Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
148+
149+
150+
@param logic: Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
151+
152+
See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
153+
154+
155+
The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
156+
157+
158+
Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
159+
160+
139161
@param diversity: **Expert option** Real number from [0.0, 1.0] which determines how much mutually dissimilar should the recommended items be. The default value is 0.0, i.e., no diversification. Value 1.0 means maximal diversification.
140162
141163
@@ -157,13 +179,13 @@ def __init__(self, user_id, count, filter=DEFAULT, booster=DEFAULT, cascade_crea
157179
"""
158180
self.user_id = user_id
159181
self.count = count
160-
self.filter = filter
161-
self.booster = booster
162-
self.cascade_create = cascade_create
163182
self.scenario = scenario
164-
self.logic = logic
183+
self.cascade_create = cascade_create
165184
self.return_properties = return_properties
166185
self.included_properties = included_properties
186+
self.filter = filter
187+
self.booster = booster
188+
self.logic = logic
167189
self.diversity = diversity
168190
self.min_relevance = min_relevance
169191
self.rotation_rate = rotation_rate
@@ -181,20 +203,20 @@ def get_body_parameters(self):
181203
"""
182204
p = dict()
183205
p['count'] = self.count
184-
if self.filter is not DEFAULT:
185-
p['filter'] = self.filter
186-
if self.booster is not DEFAULT:
187-
p['booster'] = self.booster
188-
if self.cascade_create is not DEFAULT:
189-
p['cascadeCreate'] = self.cascade_create
190206
if self.scenario is not DEFAULT:
191207
p['scenario'] = self.scenario
192-
if self.logic is not DEFAULT:
193-
p['logic'] = self.logic
208+
if self.cascade_create is not DEFAULT:
209+
p['cascadeCreate'] = self.cascade_create
194210
if self.return_properties is not DEFAULT:
195211
p['returnProperties'] = self.return_properties
196212
if self.included_properties is not DEFAULT:
197213
p['includedProperties'] = self.included_properties
214+
if self.filter is not DEFAULT:
215+
p['filter'] = self.filter
216+
if self.booster is not DEFAULT:
217+
p['booster'] = self.booster
218+
if self.logic is not DEFAULT:
219+
p['logic'] = self.logic
198220
if self.diversity is not DEFAULT:
199221
p['diversity'] = self.diversity
200222
if self.min_relevance is not DEFAULT:

0 commit comments

Comments
 (0)