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 28, 2020
1 parent 979e399 commit df1b1ef
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 85 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ NUM = 100
PROBABILITY_PURCHASED = 0.1

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

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

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

# Get 5 recommendations for user-42, who is currently viewing computer-6
recommended = client.send(RecommendItemsToItem.new('computer-6', 'user-42', 5) )
puts "Recommended items: #{recommended}"

# Recommend only computers that have at least 3 cores
recommended = client.send(
RecommendItemsToItem.new('computer-6', 'user-42', 5, {'filter' => "'num-cores'>=3"})
RecommendItemsToItem.new('computer-6', 'user-42', 5, {:filter => "'num-cores'>=3"})
)
puts "Recommended items with at least 3 processor cores: #{recommended}"

# Recommend only items thatare more expensive then currently viewed item (up-sell)
# Recommend only items that are more expensive then currently viewed item (up-sell)
recommended = client.send(
RecommendItemsToItem.new('computer-6', 'user-42', 5,
{'filter' => "'price' > context_item[\"price\"]"})
{:filter => "'price' > context_item[\"price\"]"})
)
puts "Recommended up-sell items: #{recommended}"

# Filters, boosters and other settings can be also set in the Admin UI (admin.recombee.com)
# when scenario is specified
recommended = client.send(
RecommendItemsToItem.new('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(
SearchItems.new('user-42', 'computers', 5)
)
puts "Matched items: #{matches}"
```

### Exception handling
Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RecombeeClient
include HTTParty

BATCH_MAX_SIZE = 10000
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/2.4.0'}
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/3.0.0'}

##
# - +account+ -> Name of your account at Recombee
Expand Down
47 changes: 30 additions & 17 deletions lib/recombee_api_client/api/recommend_items_to_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module RecombeeApiClient
#The returned items are sorted by relevancy (first item being the most relevant).
#
class RecommendItemsToItem < ApiRequest
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
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
attr_accessor :timeout
attr_accessor :ensure_https

Expand Down Expand Up @@ -42,15 +42,13 @@ class RecommendItemsToItem < ApiRequest
# - +count+ -> Number of items to be recommended (N for the top-N recommendation).
#
# * *Optional arguments (given as hash optional)*
# - +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.
# - +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.
# - +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.
# - +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.
# - +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.
# - +scenario+ -> Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing".
#
#The difference between `logic` and `scenario` is that `logic` specifies mainly behavior, while `scenario` specifies the place where recommendations are shown to the users.
#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+ -> 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.
# - +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.
#
#Example response:
Expand Down Expand Up @@ -107,6 +105,21 @@ class RecommendItemsToItem < ApiRequest
# }
#```
#
# - +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).
#
# - +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).
#
# - +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).
#
# - +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`.
#
# - +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.
Expand All @@ -127,13 +140,13 @@ def initialize(item_id, target_user_id, count, optional = {})
@target_user_id = target_user_id
@count = count
optional = normalize_optional(optional)
@filter = optional['filter']
@booster = optional['booster']
@cascade_create = optional['cascadeCreate']
@scenario = optional['scenario']
@logic = optional['logic']
@cascade_create = optional['cascadeCreate']
@return_properties = optional['returnProperties']
@included_properties = optional['includedProperties']
@filter = optional['filter']
@booster = optional['booster']
@logic = optional['logic']
@user_impact = optional['userImpact']
@diversity = optional['diversity']
@min_relevance = optional['minRelevance']
Expand All @@ -145,7 +158,7 @@ def initialize(item_id, target_user_id, count, optional = {})
@timeout = 3000
@ensure_https = false
@optional.each do |par, _|
fail UnknownOptionalParameter.new(par) unless ["filter","booster","cascadeCreate","scenario","logic","returnProperties","includedProperties","userImpact","diversity","minRelevance","rotationRate","rotationTime","expertSettings","returnAbGroup"].include? par
fail UnknownOptionalParameter.new(par) unless ["scenario","cascadeCreate","returnProperties","includedProperties","filter","booster","logic","userImpact","diversity","minRelevance","rotationRate","rotationTime","expertSettings","returnAbGroup"].include? par
end
end

Expand All @@ -159,13 +172,13 @@ def body_parameters
p = Hash.new
p['targetUserId'] = @target_user_id
p['count'] = @count
p['filter'] = @optional['filter'] if @optional.include? 'filter'
p['booster'] = @optional['booster'] if @optional.include? 'booster'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p['scenario'] = @optional['scenario'] if @optional.include? 'scenario'
p['logic'] = @optional['logic'] if @optional.include? 'logic'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p['returnProperties'] = @optional['returnProperties'] if @optional.include? 'returnProperties'
p['includedProperties'] = @optional['includedProperties'] if @optional.include? 'includedProperties'
p['filter'] = @optional['filter'] if @optional.include? 'filter'
p['booster'] = @optional['booster'] if @optional.include? 'booster'
p['logic'] = @optional['logic'] if @optional.include? 'logic'
p['userImpact'] = @optional['userImpact'] if @optional.include? 'userImpact'
p['diversity'] = @optional['diversity'] if @optional.include? 'diversity'
p['minRelevance'] = @optional['minRelevance'] if @optional.include? 'minRelevance'
Expand Down
Loading

0 comments on commit df1b1ef

Please sign in to comment.