Skip to content

Commit df1b1ef

Browse files
committed
Support SearchItems endpoint
1 parent 979e399 commit df1b1ef

12 files changed

+371
-85
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ NUM = 100
7272
PROBABILITY_PURCHASED = 0.1
7373

7474
client = RecombeeClient('--my-database-id--', '--db-private-token--')
75-
client.send(ResetDatabase.new) # Clear everything from the database
75+
client.send(ResetDatabase.new) # Clear everything from the database (asynchronous)
7676

7777
# We will use computers as items in this example
7878
# Computers have five properties
@@ -103,7 +103,7 @@ requests = (1..NUM).map do |i|
103103
},
104104
#optional parameters:
105105
{
106-
'cascadeCreate' => true # Use cascadeCreate for creating item
106+
:cascade_create => true # Use cascade_create for creating item
107107
# with given itemId, if it doesn't exist
108108
}
109109
)
@@ -117,29 +117,38 @@ requests = []
117117
(1..NUM).map{|i| "computer-#{i}"}.each do |item_id|
118118
user_ids = (1..NUM).map{|i| "user-#{i}"}
119119
user_ids = user_ids.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
120-
# Use cascadeCreate to create unexisting users
121-
user_ids.each { |user_id| requests.push(AddPurchase.new(user_id, item_id, 'cascadeCreate' => true)) }
120+
# Use cascade_create to create unexisting users
121+
user_ids.each { |user_id| requests.push(AddPurchase.new(user_id, item_id, :cascade_create => true)) }
122122
end
123123

124124
# Send purchases to the recommender system
125125
client.send(Batch.new(requests))
126126

127127
# Get 5 recommendations for user-42, who is currently viewing computer-6
128-
recommended = client.send(RecommendItemsToItem.new('computer-6', 'user-42', 5) )
129-
puts "Recommended items: #{recommended}"
130-
131128
# Recommend only computers that have at least 3 cores
132129
recommended = client.send(
133-
RecommendItemsToItem.new('computer-6', 'user-42', 5, {'filter' => "'num-cores'>=3"})
130+
RecommendItemsToItem.new('computer-6', 'user-42', 5, {:filter => "'num-cores'>=3"})
134131
)
135132
puts "Recommended items with at least 3 processor cores: #{recommended}"
136133

137-
# Recommend only items thatare more expensive then currently viewed item (up-sell)
134+
# Recommend only items that are more expensive then currently viewed item (up-sell)
138135
recommended = client.send(
139136
RecommendItemsToItem.new('computer-6', 'user-42', 5,
140-
{'filter' => "'price' > context_item[\"price\"]"})
137+
{:filter => "'price' > context_item[\"price\"]"})
141138
)
142139
puts "Recommended up-sell items: #{recommended}"
140+
141+
# Filters, boosters and other settings can be also set in the Admin UI (admin.recombee.com)
142+
# when scenario is specified
143+
recommended = client.send(
144+
RecommendItemsToItem.new('computer-6', 'user-42', 5, {:scenario => 'product_detail'})
145+
)
146+
147+
# Perform personalized full-text search with a user's search query (e.g. 'computers').
148+
matches = client.send(
149+
SearchItems.new('user-42', 'computers', 5)
150+
)
151+
puts "Matched items: #{matches}"
143152
```
144153

145154
### Exception handling

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/2.4.0'}
21+
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/3.0.0'}
2222

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

lib/recombee_api_client/api/recommend_items_to_item.rb

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module RecombeeApiClient
1414
#The returned items are sorted by relevancy (first item being the most relevant).
1515
#
1616
class RecommendItemsToItem < ApiRequest
17-
attr_reader :item_id, :target_user_id, :count, :filter, :booster, :cascade_create, :scenario, :logic, :return_properties, :included_properties, :user_impact, :diversity, :min_relevance, :rotation_rate, :rotation_time, :expert_settings, :return_ab_group
17+
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
1818
attr_accessor :timeout
1919
attr_accessor :ensure_https
2020

@@ -42,15 +42,13 @@ class RecommendItemsToItem < ApiRequest
4242
# - +count+ -> Number of items to be recommended (N for the top-N recommendation).
4343
#
4444
# * *Optional arguments (given as hash optional)*
45-
# - +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.
46-
# - +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.
47-
# - +cascadeCreate+ -> 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.
48-
# - +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.
49-
# - +logic+ -> Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain (e-commerce, multimedia, fashion ...) and use case.
50-
#See [this section](https://docs.recombee.com/recommendation_logic.html) for list of available logics and other details.
45+
# - +scenario+ -> Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
5146
#
52-
#The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
47+
#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.
5348
#
49+
#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.
50+
#
51+
# - +cascadeCreate+ -> 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.
5452
# - +returnProperties+ -> 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.
5553
#
5654
#Example response:
@@ -107,6 +105,21 @@ class RecommendItemsToItem < ApiRequest
107105
# }
108106
#```
109107
#
108+
# - +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.
109+
#
110+
#Filters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
111+
#
112+
# - +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.
113+
#
114+
#Boosters can be also assigned to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
115+
#
116+
# - +logic+ -> Logic specifies particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.
117+
#See [this section](https://docs.recombee.com/recommendation_logics.html) for list of available logics and other details.
118+
#
119+
#The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
120+
#
121+
#Logic can be also set to a [scenario](https://docs.recombee.com/scenarios.html) in the [Admin UI](https://admin.recombee.com).
122+
#
110123
# - +userImpact+ -> **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`.
111124
#
112125
# - +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.
@@ -127,13 +140,13 @@ def initialize(item_id, target_user_id, count, optional = {})
127140
@target_user_id = target_user_id
128141
@count = count
129142
optional = normalize_optional(optional)
130-
@filter = optional['filter']
131-
@booster = optional['booster']
132-
@cascade_create = optional['cascadeCreate']
133143
@scenario = optional['scenario']
134-
@logic = optional['logic']
144+
@cascade_create = optional['cascadeCreate']
135145
@return_properties = optional['returnProperties']
136146
@included_properties = optional['includedProperties']
147+
@filter = optional['filter']
148+
@booster = optional['booster']
149+
@logic = optional['logic']
137150
@user_impact = optional['userImpact']
138151
@diversity = optional['diversity']
139152
@min_relevance = optional['minRelevance']
@@ -145,7 +158,7 @@ def initialize(item_id, target_user_id, count, optional = {})
145158
@timeout = 3000
146159
@ensure_https = false
147160
@optional.each do |par, _|
148-
fail UnknownOptionalParameter.new(par) unless ["filter","booster","cascadeCreate","scenario","logic","returnProperties","includedProperties","userImpact","diversity","minRelevance","rotationRate","rotationTime","expertSettings","returnAbGroup"].include? par
161+
fail UnknownOptionalParameter.new(par) unless ["scenario","cascadeCreate","returnProperties","includedProperties","filter","booster","logic","userImpact","diversity","minRelevance","rotationRate","rotationTime","expertSettings","returnAbGroup"].include? par
149162
end
150163
end
151164

@@ -159,13 +172,13 @@ def body_parameters
159172
p = Hash.new
160173
p['targetUserId'] = @target_user_id
161174
p['count'] = @count
162-
p['filter'] = @optional['filter'] if @optional.include? 'filter'
163-
p['booster'] = @optional['booster'] if @optional.include? 'booster'
164-
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
165175
p['scenario'] = @optional['scenario'] if @optional.include? 'scenario'
166-
p['logic'] = @optional['logic'] if @optional.include? 'logic'
176+
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
167177
p['returnProperties'] = @optional['returnProperties'] if @optional.include? 'returnProperties'
168178
p['includedProperties'] = @optional['includedProperties'] if @optional.include? 'includedProperties'
179+
p['filter'] = @optional['filter'] if @optional.include? 'filter'
180+
p['booster'] = @optional['booster'] if @optional.include? 'booster'
181+
p['logic'] = @optional['logic'] if @optional.include? 'logic'
169182
p['userImpact'] = @optional['userImpact'] if @optional.include? 'userImpact'
170183
p['diversity'] = @optional['diversity'] if @optional.include? 'diversity'
171184
p['minRelevance'] = @optional['minRelevance'] if @optional.include? 'minRelevance'

0 commit comments

Comments
 (0)