Help Center/ GeminiDB/ GeminiDB Redis API/ Best Practices/ GeminiDB Redis API for Product Correlation Analysis
Updated on 2025-07-29 GMT+08:00

GeminiDB Redis API for Product Correlation Analysis

This section describes how to use Redis data structures (such as sets, sorted sets, and hash tables) to build an application for correlation analysis on e-commerce store items. You can use GeminiDB Redis API in the following scenarios.

Application Scenarios

The correlation can be analyzed based on products added to the shopping cart of a user. The results are crucial for the e-commerce industry and can be used to analyze user's shopping habits. For example, merchants can:

  • Recommend related items to the user who is browsing on the details page of a specific item.
  • Recommend related items to a user who just added an item to the shopping cart.
  • Display highly correlated items together.

Data Structure Design

  • Product browse records
    Store IDs of products that users have browsed in a set of a Redis instance.
    • Key: user:<user_id>:viewed
    • Value: set of product IDs

    Example:

    User 123 browsed products 1001, 1002, and 1003.

    user:123:viewed -> {1001, 1002, 1003}
  • Purchase records
    Store IDs of products that users have purchased in a set.
    • Key: user:<user_id>:purchased
    • Value: set of product IDs

    Example:

    User 123 purchased products 1001 and 1005.

    user:123:purchased -> {1001, 1005}
  • Product correlation
    Store co-occurrence times of products in a zset (sorted set) of a Redis instance.
    • Key: product:<product_id>:related
    • Value: IDs of other related products and co-occurrence times

    Example:

    Other product ID related to product 1001 and their co-occurrence times

    product:1001:related -> {1002:5, 1003:3, 1005:2}

Data Collection

  • User browse behavior

Each time a user browses a product, add its ID to the user's browse records. The following is an example of C++ code:

void recordView(int user_id, int product_id) {
    std::cout << "User ID " << user_id << " has viewed Product ID " << product_id << std::endl;
    redis.sadd("user:" + std::to_string(user_id) + ":viewed", std::to_string(product_id));
} 
  • User purchase behavior

Each time a user purchases a product, add its ID to the user's purchase records. The following is an example of C++ code:

void recordPurchase(int user_id, int product_id) {
    std::cout << "User ID " << user_id << " has purchased Product ID " << product_id << std::endl;
    redis.sadd("user:" + std::to_string(user_id) + ":purchased", std::to_string(product_id));
} 

Query API

The following is an example of C++ code for querying other products most related to a specified product based on its ID:

std::vector<std::pair<std::string, double>> getRelatedProducts(int product_id) {
    std::vector<std::pair<std::string, double>> result;
    redis.zrevrange("product:" + std::to_string(product_id) + ":related", 0, -1, std::back_inserter(result));
    return result;
} 

Complete Example of C++ Code

The following is a complete example of C++ code, which is implemented using C++ Redis client redis++.

#include <iostream>
#include <iterator>
#include <set>
#include <vector>
#include <string>
#include <utility>
 
#include "sw/redis++/redis++.h"
 
using namespace sw::redis;
 
auto redis = Redis("tcp://127.0.0.1:6379");
 
void recordView(int user_id, int product_id) {
    std::cout << "User ID " << user_id << " has viewed Product ID " << product_id << std::endl;
    redis.sadd("user:" + std::to_string(user_id) + ":viewed", std::to_string(product_id));
}
 
void recordPurchase(int user_id, int product_id) {
    std::cout << "User ID " << user_id << " has purchased Product ID " << product_id << std::endl;
    redis.sadd("user:" + std::to_string(user_id) + ":purchased", std::to_string(product_id));
}
 
void updateRelatedProducts(int user_id) {
    std::set<std::string> viewed_products;
    redis.smembers("user:" + std::to_string(user_id) + ":viewed", std::inserter(viewed_products, viewed_products.end()));
 
    std::set<std::string> purchased_products;
    redis.smembers("user:" + std::to_string(user_id) + ":purchased", std::inserter(purchased_products, purchased_products.end()));
 
    for (const auto& product_id : viewed_products) {
        for (const auto& related_product_id : viewed_products) {
            if (product_id != related_product_id) {
              //Sets the browse weight to 1.
                redis.zincrby("product:" + product_id + ":related", 1, related_product_id);
            }
        }
    }
 
    for (const auto& product_id : purchased_products) {
        for (const auto& related_product_id : purchased_products) {
            if (product_id != related_product_id) {
             //Higher weight of purchase behavior
                redis.zincrby("product:" + product_id + ":related", 2, related_product_id);
            }
        }
    }
}
 
std::vector<std::pair<std::string, double>> getRelatedProducts(int product_id) {
    std::vector<std::pair<std::string, double>> result;
    redis.zrevrange("product:" + std::to_string(product_id) + ":related", 0, -1, std::back_inserter(result));
    return result;
}
 
int main() {
    int user_id = 123;
 
    //Simulates user behavior.
    recordView(user_id, 1001);
    recordView(user_id, 1002);
    recordView(user_id, 1003);
    recordView(user_id, 1004);
    recordView(user_id, 1005);
    recordPurchase(user_id, 1001);
    recordPurchase(user_id, 1005);
 
     //Updates product correlation information based on user behavior.
    updateRelatedProducts(user_id);
 
    //Queries products related to product 1001.
    int product_id = 1001;
    auto related_products = getRelatedProducts(product_id);
    std::cout << "Product ID " << product_id << "'s related product is: " << std::endl;
    for (const auto& [product_id, score] : related_products) {
        std::cout << "Product ID: " << product_id << ", Score: " << score << std::endl;
    }
 
    return 0;
}

Returned results: