Skip to content

Product recommendations with our Java client

Looking for setup prerequisites, authentication details, and package references? Start with the Java SDK landing page.

Start by making a Recommender instance.

java
Recommender recommender = new Recommender(
    "00000000-0000-0000-0000-000000000001",
    "your API Key",
    "the server URL for the dataset");

Before going any further, read about handling different types of users here.

java
PopularProductsRequest request = PopularProductsRequest.create(
    Language.create("da"),
    Currency.create("DKK"),
    "Front Page",
    getUser(),
    PopularityTypes.MostPurchased,
    30)
    .setSettings(
        ProductRecommendationRequestSettings.create()
            .setNumberOfRecommendations(10)
            .setSelectedProductProperties(
                SelectedProductPropertiesSettings.create()
                    .setDisplayName(true)
                    .setBrand(true)
                    .setPricing(true)
            )
    );

ProductRecommendationResponse response = recommender.recommend(request);

Batching Recommendation Requests in Java

It is best practice to batch all Recommendation requests on a page into a single collection. This minimizes network traffic and can ensure that the results contain distinct Products.

java
User user = getUser();
ProductAndVariantId id = ProductAndVariantId.create("productId", "variantId");

PurchasedWithProductRequest purchasedWithProductRequest = PurchasedWithProductRequest.create(
    Language.create("da"),
    Currency.create("DKK"),
    "Product Details Page",
    user,
    id
);

ProductsViewedAfterViewingProductRequest productsViewedAfterViewingProductRequest =
    ProductsViewedAfterViewingProductRequest.create(
        Language.create("da"),
        Currency.create("DKK"),
        "Product Details Page",
        user,
        id
    );

ProductRecommendationRequestCollection collection =
    ProductRecommendationRequestCollection.create(
        true, // require distinct Products across results
        purchasedWithProductRequest,
        productsViewedAfterViewingProductRequest
    );

ProductRecommendationResponseCollection response = recommender.recommend(collection);

The example uses Product Recommendation requests. Content Recommendation requests can be batched separately using ContentRecommendationRequestCollection.

Collections that exceed the configured client-side batch size are automatically split into multiple API requests. The responses are combined in the same order as the original requests. The default batch size is 1,000, but you can configure a different maximum before sending the collection:

java
recommender.setBatchSize(500);

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com