All Products
Search
Document Center

Artificial Intelligence Recommendation:Use the recommendation filtering feature to customize the filtering of feed streams

Last Updated:Jun 15, 2023

This topic describes how to use the recommendation filtering feature to customize recommendations based on users' preferences.

1. Background information

In some scenarios, users prefer only specific brands or products with specific attributes. No matter whether their preferences are shaped by specific needs or just habits, their preferences can be echoed by using the recommendation filtering feature. When you use this feature in your services, you can specify logic for instrumentation on the client and include filtering conditions in requests for recommendations sent by server SDKs. The filtering condition can be categories, tags, cities, or authors.

Scenarios

The recommendation filtering feature can be used in scenarios such as the following examples:

Example 1:

When users scan the information about B&Bs and hotels, they always have preferences on locations. In this case, recommendations can be made based on location-related tags to help users efficiently find B&Bs or hotels that best meet their requirements. This feature is also suitable for new users who generate little behavioral data. Their preferences can be captured directly from the choices they make.

IMG_6061

Example 2:

The recommendation filtering feature is used on the promotion page of new products to display products that users may prefer.

IMG_6065

In addition to intelligent recommendations, this feature also allows users to customize filtering conditions, such as brands, when they view products on the page.

In this case, recommendations are narrowed down from new products for promotion to only the products that meet the customized filtering conditions. This ensures that the recommendation results are more specific and customized.

2. Enable the feature

The recommendation filtering feature provides additional filtering on recommendation results. Therefore, you cannot enable this feature in the Artificial Intelligence Recommendation (AIRec) console. Instead, you can enable it by adding parameters to requests for recommendations. The following paragraphs describe the details:

Use JSON strings to specify filtering rules. After you encode the JSON strings in the Base64 format, replace plus signs (+), forward slashes (/), and equal signs (=) in the strings respectively with hyphens (-), underscores (_), and periods (.). Then, use the modified result as the value of the filter parameter in the request.

Important

When you specify filtering rules, take note of the following requirements:

  1. You can specify up to 10 filtering rules for single-value fields.

  2. All filtering rules in the third layer must be specified for single-value fields.

  3. The strings cannot contain too many characters. Otherwise, errors may occur in SDKs because Alibaba Cloud has limits on the length of URLs.

    In most cases, if the first two requirements are met, the length will not exceed the upper limit.

A sample request for recommendations is provided at the end of this topic.

3. Details of the feature

The recommendation filtering feature allows you to filter recommendation results based on one or more attribute fields of products.

Filtering based on one attribute field:

This type of filtering supports the following pairs of filtering conditions:

  1. contain/not_contain: This pair of filtering conditions works only on multi-value fields. Multi-value fields, such as tags and author, contain a string of values separated by commas (,).

  2. equal/not_equal: This pair of filtering conditions works only on single-value fields, such as item_type, title, and city.

  3. category_match/category_not_match: This pair of filtering conditions works only on the category_path field that specifies the path of an expected category. Categories must be filtered from the left end to the right end of the path. For example, If the category_path field is set to 1_2_3_4, categories must be filtered from 1 to the rest in sequence. In this case, 1, 1_2, 1_2_3, and 1_2_3_4 are matched.

Filtering based on multiple attribute fields:

You can perform an AND or OR operation on filtering results. The filtering conditions work on different fields in different scenarios. The following list describes the details:

  1. Scenarios of e-commerce:

    Single-value fields:

    item_type, category_level, category_path, weight, cur_price, brand_id, shop_id, and source_id

    Multi-value fields:

    tags

  2. Scenarios of content delivery: Single-value fields:

    item_type, category_level, category_path, weight, pub_id, source_id, country, and city

    Multi-value fields:

    tags, channel, organization, and author

  3. Scenarios of news delivery: Single-value fields:

    item_type, category_level, category_path, weight, pub_id, source_id, country, and city

    Multi-value fields:

    tags, channel, organization, and author

The JSON strings that specify filtering rules must be in the following syntax: Filtering based on one attribute field:

{
                "cond": "contain" | "not_contain" | "equal" | "not_equal"| "category_match" | "category_not_match", // The filtering condition to be used.
                "field": "<field_name>", // The name of the field on which the filtering condition works, such as tags or channel.
                "value": "<compare_value>" // The value to be selected during the filtering, such as military or Stephen Chow.
}

Filtering based on multiple attribute fields:

{
                "join": "and" | "or",
                "filters": [
                                {The rule for filtering based on multiple attribute fields | The rule for filtering based on one attribute field},
                                {The rule for filtering based on multiple attribute fields | The rule for filtering based on one attribute field},
                                ...
                ]
}

4. Sample cases

Example 3:

For example, you are an e-commerce platform provider in tourism. Your platform provides selections for items such as vehicles and hotels. In terms of vehicles, your platform provides four categories: planes, trains, buses or ships, and taxis or car rentals.

In this example, a user uses your platform to rent a car.

The user has the following requirements: 1. The car can be delivered to the specified location. 2. The license plate of the car must be registered in Beijing. 3. The car has a parking sensor.

Thus, the user can select products whose brand_id is "Car rentals" from the "taxis or car rentals" category under the "vehicles" item. However, because of the preceding requirements, the user needs to filter the selected results based on multiple attribute fields.

If the attribute data that meets the preceding requirements is stored as values of the tags field, you can write the recommendation filtering expression in the following manner:

(category_path category_match "Vehicles_Taxis or car rentals") AND (brand_id = "Car rentals") AND

((tags contain "Delivery") OR (tags contain "Beijing license plate") OR (tags contain "Parking sensor"))

You can write the JSON-format filtering conditions in the following manner:

{
  "join": "and",
  "filters": [
  {
    "cond": "contain",
    "field": "brand_id",
    "value": "Car rentals"
  },
  {
    "cond": "category_match",
    "field": "category_path",
    "value": "Vehicles_Taxis or car rentals"
  },
  {
    "join": "or",
    "filters": [{
      "cond": "contain",
      "field": "tags",
      "value": "Delivery"
    },
    {
      "cond": "contain",
      "field": "tags",
      "value": "Beijing license plate"
    },
    {
      "cond": "contain",
      "field": "tags",
      "value": "Parking sensor"
    }]
  }]
}

Include the filtering conditions in the request for recommendations.

Sample request

The following code provides a sample request:

@Data
private static abstract class BaseFilterRule {}

@EqualsAndHashCode(callSuper = true)
@Data
private static final class JoinFilterRule extends BaseFilterRule {
    String join;
    List<BaseFilterRule> filters = new ArrayList<>();
}

@EqualsAndHashCode(callSuper = true)
@Data
private static final class SingleFilterRule extends BaseFilterRule {
    String cond;
    String field;
    String value;
}

public void testRecommendWithFilterDemo() throws ClientException {
    JoinFilterRule rootRule = new JoinFilterRule();
    rootRule.setJoin("and");
    {
        JoinFilterRule tagsRule = new JoinFilterRule();
        tagsRule.setJoin("or");
        {
            SingleFilterRule rule = new SingleFilterRule();
            rule.setCond("contain");
            rule.setField("tags");
            rule.setValue("Xiaohongshu");
            tagsRule.getFilters().add(rule);
        }
        {
            SingleFilterRule rule = new SingleFilterRule();
            rule.setCond("contain");
            rule.setField("tags");
            rule.setValue("Popular");
            tagsRule.getFilters().add(rule);
        }
        rootRule.getFilters().add(tagsRule);
    }
    {
        SingleFilterRule rule = new SingleFilterRule();
        rule.setCond("equal");
        rule.setField("channel");
        rule.setValue("Makeup");
        rootRule.getFilters().add(rule);
    }
    {
        SingleFilterRule rule = new SingleFilterRule();
        rule.setCond("not_equal");
        rule.setField("category");
        rule.setValue("Promotion");
        rootRule.getFilters().add(rule);
    }
    {
        SingleFilterRule rule = new SingleFilterRule();
        rule.setCond("category_match");
        rule.setField("category_path");
        rule.setValue("Women's clothing_Dresses");
        rootRule.getFilters().add(rule);
    }
    String filterRuleString = new Gson().toJson(rootRule);
    filterRuleString = new String(Base64.getEncoder().encode(filterRuleString.getBytes()));
    filterRuleString = filterRuleString.replaceAll("\\+", "-");
    filterRuleString = filterRuleString.replaceAll("/", "_");
    filterRuleString = filterRuleString.replaceAll("=", ".");
    DefaultAcsClient client = createClient();
    RecommendRequest request = new RecommendRequest();
    request.setInstanceId(instanceId);
    request.setSceneId(sceneId);
    request.setUserId(userId);
    request.putQueryParameter("filter", filterRuleString);  // Add the filter parameter. 
    request.setReturnCount(2);
    request.setAcceptFormat(FormatType.JSON);
    RecommendResponse response = client.getAcsResponse(request);
    System.out.println(String.format("got %d results", response.getResult().size()));
}