Use a Tair (Redis OSS-compatible) instance to build item correlation analysis for e-commerce recommendation features.
Use cases
Item correlation tracks which products are frequently purchased together. Use this data to:
Recommend related items on a product detail page
Suggest add-ons when a user adds an item to the shopping cart
Group highly correlated items together in display layouts
How it works
For each item, maintain a sorted set with the following structure:
| Element | Meaning |
|---|---|
| Key | The item being tracked, named as <namespace>:<product-name> |
| Member | Another item that has been purchased in the same cart |
| Score | The number of times the two items appeared together in a cart |
Each time items A and B are purchased together, call ZINCRBY on both sorted sets: increment B's score in A's set, and A's score in B's set. To retrieve the top correlated items for any product, call ZREVRANGE ... WITHSCORES on its sorted set. Items with higher scores appear first.
Key naming convention: Use a descriptive key per item, such as <namespace>:<product-name>. In production, replace the namespace and product name with values appropriate for your catalog.
Build item correlation with Jedis
This example uses Jedis 4.2.3. Add the following dependency to your pom.xml:
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
</dependencies>The following code simulates 5 users each making a purchase from one of three shopping scenarios, then queries the co-purchase counts for all items.
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.resps.Tuple;
public class AliyunShoppingMall {
public static void main(String[] args) {
// Specify the connection information of the instance. This information can be obtained from the console.
String host = "r-uf66rt5q0vsstw****.tairpena.rds.aliyuncs.com";
int port = 6379;
Jedis jedis = new Jedis(host, port);
try {
// Specify the password used to connect to the instance.
String authString = jedis.auth("password");//password
if (!authString.equals("OK")) {
System.err.println("AUTH Failed: " + authString);
return;
}
// List the products.
String key0 = "Alibaba Cloud: Product: Beer";
String key1 = "Alibaba Cloud: Product: Chocolate";
String key2 = "Alibaba Cloud: Product: Cola";
String key3 = "Alibaba Cloud: Product: Gum";
String key4 = "Alibaba Cloud: Product: Beef Jerky";
String key5 = "Alibaba Cloud: Product: Chicken Wings";
final String[] aliyunProducts = new String[]{key0, key1, key2, key3, key4, key5};
// Initialize settings to clear the possible existing data.
for (int i = 0; i < aliyunProducts.length; i++) {
jedis.del(aliyunProducts[i]);
}
// Simulate shopping behaviors.
for (int i = 0; i < 5; i++) {// Simulate the shopping behaviors of multiple users.
customersShopping(aliyunProducts, i, jedis);
}
System.out.println();
// Use Tair (Redis OSS-compatible) to generate the correlated relationship between items.
for (int i = 0; i < aliyunProducts.length; i++) {
System.out.println(">>>>>>>>>>" + aliyunProducts[i] + "was purchased together with<<<<<<<<<<<<<<<");
List<Tuple> relatedList = jedis.zrevrangeWithScores(aliyunProducts[i], 0, -1);
for (Tuple item : relatedList) {
System.out.println("Item name:" + item.getElement() + ", Number of times it has been purchased together with other products:" + Double.valueOf(item.getScore()).intValue());
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.quit();
jedis.close();
}
}
private static void customersShopping(String[] products, int i, Jedis jedis) {
// Simulate three simple shopping behaviors and randomly select one as the behavior of the user.
int bought = (int) (Math.random() * 3);
if (bought == 1) {
// Simulate business logic: The user has purchased the following products:
System.out.println("User" + i + "purchased" + products[0] + "," + products[2] + "," + products[1]);
// Store the correlations between the products in sorted sets of the instance.
jedis.zincrby(products[0], 1, products[1]);
jedis.zincrby(products[0], 1, products[2]);
jedis.zincrby(products[1], 1, products[0]);
jedis.zincrby(products[1], 1, products[2]);
jedis.zincrby(products[2], 1, products[0]);
jedis.zincrby(products[2], 1, products[1]);
} else if (bought == 2) {
// Simulate business logic: The user has purchased the following products:
System.out.println("User" + i + "purchased" + products[4] + "," + products[2] + "," + products[3]);
// Store the correlations between the products in sorted sets.
jedis.zincrby(products[4], 1, products[2]);
jedis.zincrby(products[4], 1, products[3]);
jedis.zincrby(products[3], 1, products[4]);
jedis.zincrby(products[3], 1, products[2]);
jedis.zincrby(products[2], 1, products[4]);
jedis.zincrby(products[2], 1, products[3]);
} else if (bought == 0) {
// Simulate business logic: The user has purchased the following products:
System.out.println("User" + i + "purchased" + products[1] + "," + products[5]);
// Store the correlations between the products in sorted sets of the instance.
jedis.zincrby(products[5], 1, products[1]);
jedis.zincrby(products[1], 1, products[5]);
}
}
}After you enter the correct endpoint and password to connect to the instance and run the preceding Java code, the following output is displayed:
User 0 purchased Alibaba Cloud: Product: Chocolate, Alibaba Cloud: Product: Chicken Wings
User 1 purchased Alibaba Cloud: Product: Beef Jerky, Alibaba Cloud: Product: Cola, Alibaba Cloud: Product: Gum
User 2 purchased Alibaba Cloud: Product: Beer, Alibaba Cloud: Product: Cola, Alibaba Cloud: product: Chocolate
User 3 purchased Alibaba Cloud: Product: Beef Jerky, Alibaba Cloud: Product: Cola, Alibaba Cloud: Product: Gum
User 4 purchased Alibaba Cloud: Product: Chocolate, Alibaba Cloud: Product: Chicken Wings
>>>>>>>>>>Alibaba Cloud: Product: Beer was purchased together with<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Chocolate was purchased together with<<<<<<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chicken Wings, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Beer, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Cola was purchased together with<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Beef Jerky, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Gum, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 1
Item name: Alibaba Cloud: Product: Beer, Number of times it has been purchased together with other products: 1
>>>>>>>>>>Alibaba Cloud: Product: Gum was purchased together with<<<<<<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Beef Jerky, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 2
>>>>>>>>>>Alibaba Cloud: Product: Beef Jerky was purchased together with<<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Cola, Number of times it has been purchased together with other products: 2
Item name: Alibaba Cloud: Product: Gum, Number of times it has been purchased together with other products: 2
>>>>>>>>>>Alibaba Cloud: Product: Chicken Wings was purchased together with<<<<<<<<<<<<<<
Item name: Alibaba Cloud: Product: Chocolate, Number of times it has been purchased together with other products: 2The score for each item reflects how many times it was purchased in the same cart as the queried item. ZREVRANGE ... WITHSCORES returns items sorted by score in descending order, so the strongest correlations appear first. Use these rankings to drive your recommendation logic — for example, surface the top-K items with the highest co-purchase counts.