Class: RecombeeApiClient::RecommendItemsToItem

Inherits:
ApiRequest
  • Object
show all
Defined in:
lib/recombee_api_client/api/recommend_items_to_item.rb

Overview

Recommends a set of items that are somehow related to one given item, X. A typical scenario is when the user A is viewing X. Then you may display items to the user that he might also be interested in. Recommend items to item request gives you Top-N such items, optionally taking the target user A into account.

The returned items are sorted by relevance (the first item being the most relevant).

Besides the recommended items, also a unique recommId is returned in the response. It can be used to:

  • Let Recombee know that this recommendation was successful (e.g., user clicked one of the recommended items). See Reported metrics.
  • Get subsequent recommended items when the user scrolls down (infinite scroll) or goes to the next page. See Recommend Next Items.

It is also possible to use POST HTTP method (for example in the case of a very long ReQL filter) - query parameters then become body parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashNormalizer

#camelize, #normalize_hash_to_camel_case

Constructor Details

#initialize(item_id, target_user_id, count, optional = {}) ⇒ RecommendItemsToItem

  • Required arguments
    • item_id -> ID of the item for which the recommendations are to be generated.
    • target_user_id -> ID of the user who will see the recommendations.

Specifying the targetUserId is beneficial because:

  • It makes the recommendations personalized
  • Allows the calculation of Actions and Conversions in the graphical user interface, as Recombee can pair the user who got recommendations and who afterward viewed/purchased an item.

If you insist on not specifying the user, pass null (None, nil, NULL etc., depending on the language) to targetUserId. Do not create some special dummy user for getting recommendations, as it could mislead the recommendation models, and result in wrong recommendations.

For anonymous/unregistered users, it is possible to use, for example, their session ID.

- +count+ -> Number of items to be recommended (N for the top-N recommendation).
  • Optional arguments (given as hash optional)
    • scenario -> Scenario defines a particular application of recommendations. It can be, for example, "homepage", "cart", or "emailing".

You can set various settings to the scenario in the Admin UI. You can also see the performance of each scenario in the Admin UI separately, so you can check how well each application performs.

The AI that optimizes models to get the best results may optimize different scenarios separately or even use different models in each of the scenarios.

- +cascadeCreate+ -> If an item of the given *itemId* or user of the 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 the 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 to easily display the recommended items to the user.

Example response:

 {
   "recommId": "0c6189e7-dc1a-429a-b613-192696309361",
   "recomms":
     [
       {
         "id": "tv-178",
         "values": {
           "description": "4K TV with 3D feature",
           "categories":   ["Electronics", "Televisions"],
           "price": 342,
           "url": "myshop.com/tv-178"
         }
       },
       {
         "id": "mixer-42",
         "values": {
           "description": "Stainless Steel Mixer",
           "categories":   ["Home & Kitchen"],
           "price": 39,
           "url": "myshop.com/mixer-42"
         }
       }
     ],
   "numberNextRecommsCalls": 0
 }
- +includedProperties+ -> Allows specifying which properties should be returned when `returnProperties=true` is set. The properties are given as a comma-separated list.

Example response for includedProperties=description,price:

 {
   "recommId": "6842c725-a79f-4537-a02c-f34d668a3f80",
   "recomms":
     [
       {
         "id": "tv-178",
         "values": {
           "description": "4K TV with 3D feature",
           "price": 342
         }
       },
       {
         "id": "mixer-42",
         "values": {
           "description": "Stainless Steel Mixer",
           "price": 39
         }
       }
     ],
   "numberNextRecommsCalls": 0
 }
- +filter+ -> Boolean-returning [ReQL](https://docs.recombee.com/reql) expression, which allows you to filter recommended items based on the values of their attributes.

Filters can also be assigned to a scenario in the Admin UI.

- +booster+ -> Number-returning [ReQL](https://docs.recombee.com/reql) expression, which allows you to boost the recommendation rate of some items based on the values of their attributes.

Boosters can also be assigned to a scenario in the Admin UI.

- +logic+ -> Logic specifies the particular behavior of the recommendation models. You can pick tailored logic for your domain and use case.

See this section for a 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 also be set to a scenario in the Admin UI.

- +reqlExpressions+ -> A dictionary of [ReQL](https://docs.recombee.com/reql) expressions that will be executed for each recommended item.

This can be used to compute additional properties of the recommended items that are not stored in the database.

The keys are the names of the expressions, and the values are the actual ReQL expressions.

Example request:

{
 "reqlExpressions": {
   "isInUsersCity": "context_user[\"city\"] in 'cities'",
   "distanceToUser": "earth_distance('location', context_user[\"location\"])",
   "isFromSameCompany": "'company' == context_item[\"company\"]"
 }
}

Example response:

{
 "recommId": "ce52ada4-e4d9-4885-943c-407db2dee837",
 "recomms":
   [
     {
       "id": "restaurant-178",
       "reqlEvaluations": {
         "isInUsersCity": true,
         "distanceToUser": 5200.2,
         "isFromSameCompany": false
       }
     },
     {
       "id": "bar-42",
       "reqlEvaluations": {
         "isInUsersCity": false,
         "distanceToUser": 2516.0,
         "isFromSameCompany": true
       }
     }
   ],
  "numberNextRecommsCalls": 0
}
- +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  at all (with the exception of history-based blacklisting), for `userImpact=1.0`, you'll get a user-based recommendation. The default value is `0`.

- +diversity+ -> **Expert option:** Real number from [0.0, 1.0], which determines how mutually dissimilar the recommended items should be. The default value is 0.0, i.e., no diversification. Value 1.0 means maximal diversification.

- +minRelevance+ -> **Expert option:** If the *targetUserId* is provided:  Specifies the threshold of how 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 a number of items equal to *count* at any cost. If there is not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations being 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.

- +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 a 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.

- +rotationTime+ -> **Expert option:** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long it takes for an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized.

- +expertSettings+ -> Dictionary of custom options.

- +returnAbGroup+ -> If there is a custom AB-testing running, return the name of the group to which the request belongs.


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 188

def initialize(item_id, target_user_id, count, optional = {})
  @item_id = item_id
  @target_user_id = target_user_id
  @count = count
  optional = normalize_hash_to_camel_case(optional)
  @scenario = optional['scenario']
  @cascade_create = optional['cascadeCreate']
  @return_properties = optional['returnProperties']
  @included_properties = optional['includedProperties']
  @filter = optional['filter']
  @booster = optional['booster']
  @logic = optional['logic']
  @reql_expressions = optional['reqlExpressions']
  @user_impact = optional['userImpact']
  @diversity = optional['diversity']
  @min_relevance = optional['minRelevance']
  @rotation_rate = optional['rotationRate']
  @rotation_time = optional['rotationTime']
  @expert_settings = optional['expertSettings']
  @return_ab_group = optional['returnAbGroup']
  @optional = optional
  @timeout = 3000
  @ensure_https = false
  @optional.each do |par, _|
    raise UnknownOptionalParameter.new(par) unless %w[scenario cascadeCreate returnProperties
                                                      includedProperties filter booster logic reqlExpressions userImpact diversity minRelevance rotationRate rotationTime expertSettings returnAbGroup].include? par
  end
end

Instance Attribute Details

#boosterObject (readonly)

Returns the value of attribute booster.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def booster
  @booster
end

#cascade_createObject (readonly)

Returns the value of attribute cascade_create.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def cascade_create
  @cascade_create
end

#countObject (readonly)

Returns the value of attribute count.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def count
  @count
end

#diversityObject (readonly)

Returns the value of attribute diversity.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def diversity
  @diversity
end

#ensure_httpsObject

Returns the value of attribute ensure_https.



24
25
26
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 24

def ensure_https
  @ensure_https
end

#expert_settingsObject (readonly)

Returns the value of attribute expert_settings.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def expert_settings
  @expert_settings
end

#filterObject (readonly)

Returns the value of attribute filter.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def filter
  @filter
end

#included_propertiesObject (readonly)

Returns the value of attribute included_properties.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def included_properties
  @included_properties
end

#item_idObject (readonly)

Returns the value of attribute item_id.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def item_id
  @item_id
end

#logicObject (readonly)

Returns the value of attribute logic.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def logic
  @logic
end

#min_relevanceObject (readonly)

Returns the value of attribute min_relevance.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def min_relevance
  @min_relevance
end

#reql_expressionsObject (readonly)

Returns the value of attribute reql_expressions.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def reql_expressions
  @reql_expressions
end

#return_ab_groupObject (readonly)

Returns the value of attribute return_ab_group.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def return_ab_group
  @return_ab_group
end

#return_propertiesObject (readonly)

Returns the value of attribute return_properties.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def return_properties
  @return_properties
end

#rotation_rateObject (readonly)

Returns the value of attribute rotation_rate.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def rotation_rate
  @rotation_rate
end

#rotation_timeObject (readonly)

Returns the value of attribute rotation_time.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def rotation_time
  @rotation_time
end

#scenarioObject (readonly)

Returns the value of attribute scenario.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def scenario
  @scenario
end

#target_user_idObject (readonly)

Returns the value of attribute target_user_id.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def target_user_id
  @target_user_id
end

#timeoutObject

Returns the value of attribute timeout.



24
25
26
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 24

def timeout
  @timeout
end

#user_impactObject (readonly)

Returns the value of attribute user_impact.



22
23
24
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 22

def user_impact
  @user_impact
end

Instance Method Details

#body_parametersObject

Values of body parameters as a Hash



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 223

def body_parameters
  p = {}
  p['targetUserId'] = @target_user_id
  p['count'] = @count
  p['scenario'] = @optional['scenario'] if @optional.include? 'scenario'
  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['reqlExpressions'] = @optional['reqlExpressions'] if @optional.include? 'reqlExpressions'
  p['userImpact'] = @optional['userImpact'] if @optional.include? 'userImpact'
  p['diversity'] = @optional['diversity'] if @optional.include? 'diversity'
  p['minRelevance'] = @optional['minRelevance'] if @optional.include? 'minRelevance'
  p['rotationRate'] = @optional['rotationRate'] if @optional.include? 'rotationRate'
  p['rotationTime'] = @optional['rotationTime'] if @optional.include? 'rotationTime'
  p['expertSettings'] = @optional['expertSettings'] if @optional.include? 'expertSettings'
  p['returnAbGroup'] = @optional['returnAbGroup'] if @optional.include? 'returnAbGroup'

  p
end

#methodObject

HTTP method



218
219
220
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 218

def method
  :post
end

#pathObject

Relative path to the endpoint



253
254
255
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 253

def path
  "/{databaseId}/recomms/items/#{@item_id}/items/"
end

#query_parametersObject

Values of query parameters as a Hash. name of parameter => value of the parameter



248
249
250
# File 'lib/recombee_api_client/api/recommend_items_to_item.rb', line 248

def query_parameters
  {}
end