Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

46 Commits

Repository files navigation

Recombee API Client

A Java client (SDK) for easy use of the Recombee recommendation API.

If you don't have an account at Recombee yet, you can create a free account here.

Documentation of the API can be found at docs.recombee.com.

Installation

The client is available in the Maven Central Repository, so you just need to add the following <dependency> entry to your project's POM:

 <dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client</artifactId>
<version>6.3.0</version>
</dependency>

Examples

Basic example

Examples are located in src/examples.

packagecom.recombee.api_client.examples;
importcom.recombee.api_client.RecombeeClient;
importcom.recombee.api_client.util.Region;
importcom.recombee.api_client.api_requests.*;
importcom.recombee.api_client.bindings.RecommendationResponse;
importcom.recombee.api_client.bindings.Recommendation;
importcom.recombee.api_client.exceptions.ApiException;
importjava.util.ArrayList;
importjava.util.Random;
publicclassBasicExample {
publicstaticvoidmain(String[] args) {
RecombeeClientclient = newRecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.US_WEST);
try {
finalintNUM = 100;
// Generate some random purchases of items by usersfinaldoublePROBABILITY_PURCHASED = 0.1;
Randomr = newRandom();
ArrayList<Request> addPurchaseRequests = newArrayList<Request>();
for (inti = 0; i < NUM; i++)
for (intj = 0; j < NUM; j++)
if (r.nextDouble() < PROBABILITY_PURCHASED) {
AddPurchaserequest = newAddPurchase(String.format("user-%s", i),String.format("item-%s", j))
.setCascadeCreate(true); // Use cascadeCreate parameter to create// the yet non-existing users and itemsaddPurchaseRequests.add(request);
}
System.out.println("Send purchases");
client.send(newBatch(addPurchaseRequests)); //Use Batch for faster processing of larger data// Get 5 recommendations for user 'user-25'RecommendationResponserecommendationResponse = client.send(newRecommendItemsToUser("user-25", 5));
System.out.println("Recommended items:");
for(Recommendationrec: recommendationResponse) System.out.println(rec.getId());
// User scrolled down - get next 3 recommended itemsrecommendationResponse = client.send(newRecommendNextItems(recommendationResponse.getRecommId(), 3));
System.out.println("Next recommended items:");
for(Recommendationrec: recommendationResponse) System.out.println(rec.getId());
} catch (ApiExceptione) {
e.printStackTrace();
//use fallback
}
}
}

Using property values

packagecom.recombee.api_client.examples;
importcom.recombee.api_client.RecombeeClient;
importcom.recombee.api_client.util.Region;
importcom.recombee.api_client.api_requests.*;
importcom.recombee.api_client.bindings.RecommendationResponse;
importcom.recombee.api_client.bindings.Recommendation;
importcom.recombee.api_client.bindings.SearchResponse;
importcom.recombee.api_client.exceptions.ApiException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Random;
publicclassItemPropertiesExample {
publicstaticvoidmain(String[] args) {
RecombeeClientclient = newRecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.AP_SE);
try {
client.send(newResetDatabase()); // Clear everything from the database/* We will use computers as items in this example Computers have four properties - price (floating point number) - number of processor cores (integer number) - description (string) - image (url of computer's photo) */client.send(newAddItemProperty("price", "double"));
client.send(newAddItemProperty("num-cores", "int"));
client.send(newAddItemProperty("description", "string"));
client.send(newAddItemProperty("image", "image"));
// Prepare requests for setting a catalog of computersfinalArrayList<Request> requests = newArrayList<Request>();
finalintNUM = 100;
finalRandomrand = newRandom();
for(inti=0; i<NUM; i++)
{
finalStringitemId = String.format("computer-%s",i);
finalSetItemValuesreq = newSetItemValues(
itemId,
//values:newHashMap<String, Object>() {{
put("price", 600.0 + 400*rand.nextDouble());
put("num-cores", 1 + rand.nextInt(7));
put("description", "Great computer");
put("image", String.format("http://examplesite.com/products/%s.jpg", itemId));
}}
).setCascadeCreate(true); // Use cascadeCreate for creating item// with given itemId, if it doesn't exist;requests.add(req);
}
client.send(newBatch(requests)); // Send catalog to the recommender system// Generate some random purchases of items by usersfinaldoublePROBABILITY_PURCHASED = 0.02;
ArrayList<Request> addPurchaseRequests = newArrayList<Request>();
for (inti = 0; i < NUM; i++)
for (intj = 0; j < NUM; j++)
if (rand.nextDouble() < PROBABILITY_PURCHASED) {
AddPurchasereq = newAddPurchase(String.format("user-%s", i),String.format("computer-%s", j))
.setCascadeCreate(true); //use cascadeCreate to create the usersaddPurchaseRequests.add(req);
}
client.send(newBatch(addPurchaseRequests)); // Send purchases to the recommender system// Get 5 recommendations for user-42, who is currently viewing computer-6// Recommend only computers that have at least 3 coresRecommendationResponserecommendationResponse = client.send(
newRecommendItemsToItem("computer-6", "user-42", 5)
.setFilter(" 'num-cores'>=3 "));
System.out.println("Recommended items with at least 3 processor cores:");
for(Recommendationrec: recommendationResponse) System.out.println(rec.getId());
// Recommend only items that are more expensive then currently viewed item (up-sell)recommendationResponse = client.send(newRecommendItemsToItem("computer-6", "user-42", 5)
.setFilter(" 'price' > context_item[\"price\"] "));
System.out.println("Recommended up-sell items:");
for(Recommendationrec: recommendationResponse) System.out.println(rec.getId());
// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)// when scenario is specifiedrecommendationResponse = client.send(
newRecommendItemsToItem("computer-6", "user-42", 5).setScenario("product_detail")
);
// Perform personalized full-text search with a user's search query (e.g. "computers")SearchResponsesearchResponse = client.send(
newSearchItems("user-42", "computers", 5)
);
System.out.println("Search matches:");
for(Recommendationrec: searchResponse) System.out.println(rec.getId());
} catch (ApiExceptione) {
e.printStackTrace();
//Use fallback
}
}
}

Exception handling

Various errors can occur while processing request, for example because of adding an already existing item or submitting interaction of nonexistent user without setCascadeCreate(true). These errors lead to throwing the ResponseException by the send method of the client. Another reason for throwing an exception is a timeout. ApiException is the base class of both ResponseException and ApiTimeoutException.

We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all.

About

Java client for easy use of the Recombee recommendation API

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages