Skip to content

Commit 01e40fc

Browse files
committed
Support RecommendNextItems endpoint
1 parent df1b1ef commit 01e40fc

14 files changed

+158
-27
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ begin
5555
client.send(Batch.new(purchases))
5656

5757
# Get recommendations for user 'user-25'
58-
recommended = client.send(RecommendItemsToUser.new('user-25', 5))
59-
puts "Recommended items for user-25: #{recommended}"
58+
response = client.send(RecommendItemsToUser.new('user-25', 5))
59+
puts "Recommended items for user-25: #{response}"
60+
61+
# User scrolled down - get next 3 recommended items
62+
response = client.send(RecommendNextItems.new(response['recommId'], 3))
63+
puts "Next recommended items for user-25: #{response}"
64+
6065
rescue APIError => e
6166
puts e
6267
# Use fallback
@@ -146,7 +151,7 @@ recommended = client.send(
146151

147152
# Perform personalized full-text search with a user's search query (e.g. 'computers').
148153
matches = client.send(
149-
SearchItems.new('user-42', 'computers', 5)
154+
SearchItems.new('user-42', 'computers', 5, {:scenario => 'search_top'})
150155
)
151156
puts "Matched items: #{matches}"
152157
```

lib/recombee_api_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RecombeeClient
1818
include HTTParty
1919

2020
BATCH_MAX_SIZE = 10000
21-
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/3.0.0'}
21+
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/3.1.0'}
2222

2323
##
2424
# - +account+ -> Name of your account at Recombee

lib/recombee_api_client/api/recommend_items_to_item.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ module RecombeeApiClient
99
##
1010
#Recommends set of items that are somehow related to one given item, *X*. Typical scenario is when user *A* is viewing *X*. Then you may display items to the user that he might be also interested in. Recommend items to item request gives you Top-N such items, optionally taking the target user *A* into account.
1111
#
12-
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
12+
#The returned items are sorted by relevance (first item being the most relevant).
13+
#
14+
#Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to:
1315
#
14-
#The returned items are sorted by relevancy (first item being the most relevant).
16+
#- Let Recombee know that this recommendation was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics).
17+
#- Get subsequent recommended items when the user scrolls down (*infinite scroll*) or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items).
18+
#
19+
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
1520
#
1621
class RecommendItemsToItem < ApiRequest
1722
attr_reader :item_id, :target_user_id, :count, :scenario, :cascade_create, :return_properties, :included_properties, :filter, :booster, :logic, :user_impact, :diversity, :min_relevance, :rotation_rate, :rotation_time, :expert_settings, :return_ab_group
@@ -75,7 +80,8 @@ class RecommendItemsToItem < ApiRequest
7580
# "url": "myshop.com/mixer-42"
7681
# }
7782
# }
78-
# ]
83+
# ],
84+
# "numberNextRecommsCalls": 0
7985
# }
8086
#```
8187
#
@@ -101,7 +107,8 @@ class RecommendItemsToItem < ApiRequest
101107
# "price": 39
102108
# }
103109
# }
104-
# ]
110+
# ],
111+
# "numberNextRecommsCalls": 0
105112
# }
106113
#```
107114
#
@@ -124,7 +131,7 @@ class RecommendItemsToItem < ApiRequest
124131
#
125132
# - +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.
126133
#
127-
# - +minRelevance+ -> **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it.
134+
# - +minRelevance+ -> **Expert option** If the *targetUserId* is provided: Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it.
128135
#
129136
# - +rotationRate+ -> **Expert option** If the *targetUserId* is provided: If your users browse the system in real-time, it may easily happen that you wish to offer them recommendations multiple times. Here comes the question: how much should the recommendations change? Should they remain the same, or should they rotate? Recombee API allows you to control this per-request in backward fashion. You may penalize an item for being recommended in the near past. For the specific user, `rotationRate=1` means maximal rotation, `rotationRate=0` means absolutely no rotation. You may also use, for example `rotationRate=0.2` for only slight rotation of recommended items.
130137
#

lib/recombee_api_client/api/recommend_items_to_user.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ module RecombeeApiClient
1111
#
1212
#The most typical use cases are recommendations at homepage, in some "Picked just for you" section or in email.
1313
#
14-
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
14+
#The returned items are sorted by relevance (first item being the most relevant).
15+
#
16+
#Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to:
1517
#
16-
#The returned items are sorted by relevancy (first item being the most relevant).
18+
#- Let Recombee know that this recommendation was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics).
19+
#- Get subsequent recommended items when the user scrolls down (*infinite scroll*) or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items).
20+
#
21+
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
1722
#
1823
class RecommendItemsToUser < ApiRequest
1924
attr_reader :user_id, :count, :scenario, :cascade_create, :return_properties, :included_properties, :filter, :booster, :logic, :diversity, :min_relevance, :rotation_rate, :rotation_time, :expert_settings, :return_ab_group
@@ -61,7 +66,8 @@ class RecommendItemsToUser < ApiRequest
6166
# "url": "myshop.com/mixer-42"
6267
# }
6368
# }
64-
# ]
69+
# ],
70+
# "numberNextRecommsCalls": 0
6571
# }
6672
#```
6773
#
@@ -87,7 +93,8 @@ class RecommendItemsToUser < ApiRequest
8793
# "price": 39
8894
# }
8995
# }
90-
# ]
96+
# ],
97+
# "numberNextRecommsCalls": 0
9198
# }
9299
#```
93100
#
@@ -108,7 +115,7 @@ class RecommendItemsToUser < ApiRequest
108115
#
109116
# - +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.
110117
#
111-
# - +minRelevance+ -> **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevancy, and may return less than *count* items when there is not enough data to fulfill it.
118+
# - +minRelevance+ -> **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it.
112119
#
113120
# - +rotationRate+ -> **Expert option** If your users browse the system in real-time, it may easily happen that you wish to offer them recommendations multiple times. Here comes the question: how much should the recommendations change? Should they remain the same, or should they rotate? Recombee API allows you to control this per-request in backward fashion. You may penalize an item for being recommended in the near past. For the specific user, `rotationRate=1` means maximal rotation, `rotationRate=0` means absolutely no rotation. You may also use, for example `rotationRate=0.2` for only slight rotation of recommended items. Default: `0.1`.
114121
#
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# This file is auto-generated, do not edit
3+
#
4+
5+
module RecombeeApiClient
6+
require_relative 'request'
7+
require_relative '../errors'
8+
9+
##
10+
#Returns 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.
11+
#
12+
#It accepts `recommId` of a base recommendation request (e.g. request from the first page) and number of items that shall be returned (`count`).
13+
#The base request can be one of:
14+
# - [Recommend items to item](https://docs.recombee.com/api.html#recommend-items-to-item)
15+
# - [Recommend items to user](https://docs.recombee.com/api.html#recommend-items-to-user)
16+
# - [Search items](https://docs.recombee.com/api.html#search-items)
17+
#
18+
#All the other parameters are inherited from the base request.
19+
#
20+
#*Recommend next items* can be called many times for a single `recommId` and each call returns different (previously not recommended) items.
21+
#The number of *Recommend next items* calls performed so far is returned in the `numberNextRecommsCalls` field.
22+
#
23+
#*Recommend next items* can be requested up to 30 minutes after the base request or a previous *Recommend next items* call.
24+
#
25+
#For billing purposes, each call to *Recommend next items* is counted as a separate recommendation request.
26+
#
27+
class RecommendNextItems < ApiRequest
28+
attr_reader :recomm_id, :count
29+
attr_accessor :timeout
30+
attr_accessor :ensure_https
31+
32+
##
33+
# * *Required arguments*
34+
# - +recomm_id+ -> ID of the base recommendation request for which next recommendations should be returned
35+
# - +count+ -> Number of items to be recommended
36+
#
37+
#
38+
def initialize(recomm_id, count)
39+
@recomm_id = recomm_id
40+
@count = count
41+
@timeout = 3000
42+
@ensure_https = false
43+
end
44+
45+
# HTTP method
46+
def method
47+
:post
48+
end
49+
50+
# Values of body parameters as a Hash
51+
def body_parameters
52+
p = Hash.new
53+
p['count'] = @count
54+
p
55+
end
56+
57+
# Values of query parameters as a Hash.
58+
# name of parameter => value of the parameter
59+
def query_parameters
60+
params = {}
61+
params
62+
end
63+
64+
# Relative path to the endpoint
65+
def path
66+
"/{databaseId}/recomms/next/items/#{@recomm_id}"
67+
end
68+
end
69+
end

lib/recombee_api_client/api/recommend_users_to_item.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class RecommendUsersToItem < ApiRequest
5353
# "sex": "M"
5454
# }
5555
# }
56-
# ]
56+
# ],
57+
# "numberNextRecommsCalls": 0
5758
# }
5859
#```
5960
#
@@ -77,7 +78,8 @@ class RecommendUsersToItem < ApiRequest
7778
# "country": "CAN"
7879
# }
7980
# }
80-
# ]
81+
# ],
82+
# "numberNextRecommsCalls": 0
8183
# }
8284
#```
8385
#

lib/recombee_api_client/api/recommend_users_to_user.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class RecommendUsersToUser < ApiRequest
5353
# "sex": "M"
5454
# }
5555
# }
56-
# ]
57-
# }
56+
# ],
57+
# "numberNextRecommsCalls": 0
58+
# }
5859
#```
5960
#
6061
# - +includedProperties+ -> Allows to specify, which properties should be returned when `returnProperties=true` is set. The properties are given as a comma-separated list.
@@ -77,7 +78,8 @@ class RecommendUsersToUser < ApiRequest
7778
# "country": "CAN"
7879
# }
7980
# }
80-
# ]
81+
# ],
82+
# "numberNextRecommsCalls": 0
8183
# }
8284
#```
8385
#

lib/recombee_api_client/api/search_items.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ module RecombeeApiClient
1313
#
1414
#This endpoint should be used in a search box at your website/app. It can be called multiple times as the user is typing the query in order to get the most viable suggestions based on current state of the query, or once after submitting the whole query.
1515
#
16-
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
16+
#The returned items are sorted by relevance (first item being the most relevant).
17+
#
18+
#Besides the recommended items, also a unique `recommId` is returned in the response. It can be used to:
1719
#
18-
#The returned items are sorted by relevancy (first item being the most relevant).
20+
#- Let Recombee know that this search was successful (e.g. user clicked one of the recommended items). See [Reported metrics](https://docs.recombee.com/admin_ui.html#reported-metrics).
21+
#- Get subsequent search results when the user scrolls down or goes to the next page. See [Recommend Next Items](https://docs.recombee.com/api.html#recommend-next-items).
22+
#
23+
#It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
1924
#
2025
class SearchItems < ApiRequest
2126
attr_reader :user_id, :search_query, :count, :scenario, :cascade_create, :return_properties, :included_properties, :filter, :booster, :logic, :expert_settings, :return_ab_group
@@ -62,7 +67,8 @@ class SearchItems < ApiRequest
6267
# "url": "myshop.com/mixer-42"
6368
# }
6469
# }
65-
# ]
70+
# ],
71+
# "numberNextRecommsCalls": 0
6672
# }
6773
#```
6874
#
@@ -88,7 +94,8 @@ class SearchItems < ApiRequest
8894
# "price": 39
8995
# }
9096
# }
91-
# ]
97+
# ],
98+
# "numberNextRecommsCalls": 0
9299
# }
93100
#```
94101
#

lib/recombee_api_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RecombeeApiClient
2-
VERSION = '3.0.0'
2+
VERSION = '3.1.0'
33
end

spec/api/add_entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
it 'fails with invalid entity id' do
16-
req = described_class.new('$$$not_valid$$$')
16+
req = described_class.new('***not_valid$$$')
1717
expect { @client.send(req) }.to raise_exception { |exception|
1818
expect(exception).to be_a(RecombeeApiClient::ResponseError)
1919
expect(exception.status_code).to eq 400

0 commit comments

Comments
 (0)