Get recommendation results

Updated at:
Copy as MD

Overview

This document describes how to retrieve recommendation results from the Artificial Intelligence Recommendation (AIRec) service. When you request recommendation results, the data is returned in JSON format.

This document applies to the Industry Operation and Algorithm Configuration editions.

Parameters

Parameter

Type

Description

Required

Notes

userId

string

The target user for recommendations.

Either `userId` or `imei` is required.

If the user is not in the user table, personalized recommendations are not generated.

imei

string

The device ID of the user. This is the IMEI for Android devices and the IDFA for iOS devices.

For unregistered users, you must provide a non-empty value for this parameter or `userId`.

A 32-bit MD5 hash.

returnCount

Integer

The number of recommendation results to return in a single request.

Yes

The recommended value is 20.

sceneId

string

The ID of the scenario where items are displayed.

Yes

The default value is 1. This value must match the scenario ID in the uploaded data.

items

string

The base item in a "Related Recommendations" scenario.

Required for the "Related Recommendations" scenario.

Format: itemId:itemType,itemId:itemType

ip

string

The IP address that the user last used to log on.

No

—

rankOpen

boolean

Specifies whether to sort results based on the user ID for personalization during the retrieval and sorting phase.

No

The default value is true.

recType

string

The type of recommendation service.

No

Valid values: `hot` (Hot Recommendations), `rr` (Related Recommendations), and `gul` (Guess You Like). Note: Both `rr` and `gul` are personalized recommendation services. To completely disable personalized recommendations, set `rankOpen` to `false` and `recType` to `hot`.

filter

string

The filter conditions for recommendations.

No

For more information about how to use the recommendation filter feature, see Use the recommendation filter feature to personalize feed streams.

strategy

string

A switch for the top-item and fixed-slot policy features.

No

For the first page request from a user, set `strategy` to 'fixedSlot' to return results based on top-item and fixed-slot configurations. If you do not specify this parameter, the default algorithm is used for recommendations.

clearExposure

string

Specifies whether to clear the exposure filter records.

No

Set `clearExposure` to `true` to clear the exposure filter records for the current user. The user can then see previously exposed items. The system restarts accumulating items for exposure filtering. Do not specify this parameter if this feature is not needed.

Common errors when getting recommendation results using a server-side SDK

Empty results are returned:

1. The request parameters are incorrect. For a test scenario, specify a `scene_id` and ensure that this `scene_id` exists in the reported data.

For a test user, specify a `user_id`. If the `user_id` does not exist in the user table, the user is treated as a new user.

Number of recommended items: The value must be less than the number of recommendable items in the scenario.

Note: You can obtain a maximum of 50 recommended items using the server-side SDK. For related recommendations, you must also specify item_id:item_type as a request parameter.Request parameters for related recommendations If the number of recommended items is too small, it may be because the user has already viewed all items for the current scenario. This is because the exposure filtering feature is enabled by default. (This means that all items have been exposed.)

Sample code

package com.aliyun.airec;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.airec.model.v20181012.RecommendRequest;
import com.aliyuncs.airec.model.v20181012.RecommendResponse;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Recommend {

    public static void main(String args[]) {
        // An Alibaba Cloud account AccessKey has full access to all APIs. Use a Resource Access Management (RAM) user for API calls and daily O&M.
        // Do not save your AccessKey ID and AccessKey secret in your project code. Leaked AccessKey pairs can compromise the security of all resources in your account.

        // This example shows how to use environment variables to store your AccessKey pair for identity verification.
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultProfile.addEndpoint("cn-hangzhou", "Airec", "airec.cn-hangzhou.aliyuncs.com");

        DefaultAcsClient client = new DefaultAcsClient(profile);

        RecommendRequest request = new RecommendRequest();

        // The following three parameters are required.
        request.setInstanceId("airec-xxx");
        request.setReturnCount(10);
        request.setSceneId("your_scene_id");

        // You must specify at least one of the following parameters (imei/user_id).
        request.setUserId("0");
        request.setImei("0");

        // Optional for related recommendation scenarios.
        //request.setItems("88:article,33:image");

        // To disable personalized recommendations.
        //request.setRankOpen(false);
        //request.setRecType("hot");

        request.setAcceptFormat(FormatType.JSON);

        try {
            RecommendResponse response = client.getAcsResponse(request);

            for (RecommendResponse.ResultItem item : response.getResult()) {
                System.out.println(item.getItemId());
                System.out.println(item.getItemType());
                System.out.println(item.getTraceId());
                System.out.println(item.getTraceInfo());
                System.out.println(item.getMatchInfo());
                System.out.println(item.getWeight());
                System.out.println(item.getPosition());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Demo project for pushing data and getting recommendation results

For more information, see airec demo for pushing data and retrieving recommendation results.