Skip to content

Commit 56c0fcf

Browse files
committed
Support SearchItems endpoint
1 parent 3790e42 commit 56c0fcf

File tree

14 files changed

+880
-315
lines changed

14 files changed

+880
-315
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The client is available in the [Maven Central Repository](https://mvnrepository.
1313
<dependency>
1414
<groupId>com.recombee</groupId>
1515
<artifactId>api-client</artifactId>
16-
<version>2.4.2</version>
16+
<version>3.0.0</version>
1717
</dependency>
1818
```
1919

@@ -81,6 +81,7 @@ import com.recombee.api_client.RecombeeClient;
8181
import com.recombee.api_client.api_requests.*;
8282
import com.recombee.api_client.bindings.RecommendationResponse;
8383
import com.recombee.api_client.bindings.Recommendation;
84+
import com.recombee.api_client.bindings.SearchResponse;
8485
import com.recombee.api_client.exceptions.ApiException;
8586

8687
import java.util.ArrayList;
@@ -93,7 +94,7 @@ public class ItemPropertiesExample {
9394
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--");
9495

9596
try {
96-
client.send(new ResetDatabase()); //Clear everything from the database
97+
client.send(new ResetDatabase()); // Clear everything from the database
9798

9899
/*
99100
We will use computers as items in this example
@@ -146,13 +147,9 @@ public class ItemPropertiesExample {
146147

147148

148149
// Get 5 recommendations for user-42, who is currently viewing computer-6
149-
RecommendationResponse recommendationResponse = client.send(new RecommendItemsToItem("computer-6", "user-42", 5));
150-
System.out.println("Recommended items:");
151-
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
152-
153-
154150
// Recommend only computers that have at least 3 cores
155-
recommendationResponse = client.send(new RecommendItemsToItem("computer-6", "user-42", 5)
151+
RecommendationResponse recommendationResponse = client.send(
152+
new RecommendItemsToItem("computer-6", "user-42", 5)
156153
.setFilter(" 'num-cores'>=3 "));
157154
System.out.println("Recommended items with at least 3 processor cores:");
158155
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
@@ -164,6 +161,20 @@ public class ItemPropertiesExample {
164161
System.out.println("Recommended up-sell items:");
165162
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
166163

164+
165+
// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)
166+
// when scenario is specified
167+
recommendationResponse = client.send(
168+
new RecommendItemsToItem("computer-6", "user-42", 5).setScenario("product_detail")
169+
);
170+
171+
// Perform personalized full-text search with a user's search query (e.g. "computers")
172+
SearchResponse searchResponse = client.send(
173+
new SearchItems("user-42", "computers", 5)
174+
);
175+
System.out.println("Search matches:");
176+
for(Recommendation rec: searchResponse) System.out.println(rec.getId());
177+
167178
} catch (ApiException e) {
168179
e.printStackTrace();
169180
//Use fallback

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.recombee</groupId>
88
<artifactId>api-client</artifactId>
9-
<version>2.4.2</version>
9+
<version>3.0.0</version>
1010
<name>Recombee API Client</name>
1111
<description>A client library for easy use of the Recombee recommendation API</description>
1212
<url>http://recombee.com</url>

src/examples/java/com/recombee/api_client/examples/ItemPropertiesExample.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package com.recombee.api_client.examples;
2-
31
import com.recombee.api_client.RecombeeClient;
42
import com.recombee.api_client.api_requests.*;
53
import com.recombee.api_client.bindings.RecommendationResponse;
64
import com.recombee.api_client.bindings.Recommendation;
5+
import com.recombee.api_client.bindings.SearchResponse;
76
import com.recombee.api_client.exceptions.ApiException;
87

98
import java.util.ArrayList;
@@ -13,22 +12,24 @@
1312
public class ItemPropertiesExample {
1413
public static void main(String[] args) {
1514

16-
RecombeeClient client = new RecombeeClient("--my-database-id--", "--my-secret-token--");
15+
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--");
1716

1817
try {
1918
client.send(new ResetDatabase()); //Clear everything from the database
2019

2120
/*
2221
We will use computers as items in this example
23-
Computers have three properties
22+
Computers have four properties
2423
- price (floating point number)
2524
- number of processor cores (integer number)
2625
- description (string)
26+
- image (url of computer's photo)
2727
*/
2828

2929
client.send(new AddItemProperty("price", "double"));
3030
client.send(new AddItemProperty("num-cores", "int"));
3131
client.send(new AddItemProperty("description", "string"));
32+
client.send(new AddItemProperty("image", "image"));
3233

3334
// Prepare requests for setting a catalog of computers
3435
final ArrayList<Request> requests = new ArrayList<Request>();
@@ -37,13 +38,15 @@ public static void main(String[] args) {
3738

3839
for(int i=0; i<NUM; i++)
3940
{
41+
final String itemId = String.format("computer-%s",i);
4042
final SetItemValues req = new SetItemValues(
41-
String.format("computer-%s",i), //itemId
43+
itemId,
4244
//values:
4345
new HashMap<String, Object>() {{
4446
put("price", 600.0 + 400*rand.nextDouble());
4547
put("num-cores", 1 + rand.nextInt(7));
4648
put("description", "Great computer");
49+
put("image", String.format("http://examplesite.com/products/%s.jpg", itemId));
4750
}}
4851
).setCascadeCreate(true); // Use cascadeCreate for creating item
4952
// with given itemId, if it doesn't exist;
@@ -65,14 +68,10 @@ public static void main(String[] args) {
6568

6669

6770
// Get 5 recommendations for user-42, who is currently viewing computer-6
68-
RecommendationResponse recommendationResponse = client.send(new RecommendItemsToItem("computer-6", "user-42", 5));
69-
System.out.println("Recommended items:");
70-
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
71-
72-
7371
// Recommend only computers that have at least 3 cores
74-
recommendationResponse = client.send(new RecommendItemsToItem("computer-6", "user-42", 5)
75-
.setFilter(" 'num-cores'>=3 "));
72+
RecommendationResponse recommendationResponse = client.send(
73+
new RecommendItemsToItem("computer-6", "user-42", 5)
74+
.setFilter(" 'num-cores'>=3 "));
7675
System.out.println("Recommended items with at least 3 processor cores:");
7776
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
7877

@@ -83,9 +82,23 @@ public static void main(String[] args) {
8382
System.out.println("Recommended up-sell items:");
8483
for(Recommendation rec: recommendationResponse) System.out.println(rec.getId());
8584

85+
86+
// Filters, boosters and other settings can be set also in the Admin UI (admin.recombee.com)
87+
// when scenario is specified
88+
recommendationResponse = client.send(
89+
new RecommendItemsToItem("computer-6", "user-42", 5).setScenario("product_detail")
90+
);
91+
92+
// Perform personalized full-text search with a user's search query (e.g. "computers")
93+
SearchResponse searchResponse = client.send(
94+
new SearchItems("user-42", "computers", 5)
95+
);
96+
System.out.println("Search matches:");
97+
for(Recommendation rec: searchResponse) System.out.println(rec.getId());
98+
8699
} catch (ApiException e) {
87100
e.printStackTrace();
88101
//Use fallback
89102
}
90103
}
91-
}
104+
}

src/main/java/com/recombee/api_client/RecombeeClient.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.recombee.api_client.api_requests.RecommendUsersToUser;
6868
import com.recombee.api_client.api_requests.RecommendItemsToItem;
6969
import com.recombee.api_client.api_requests.RecommendUsersToItem;
70+
import com.recombee.api_client.api_requests.SearchItems;
7071
import com.recombee.api_client.api_requests.UserBasedRecommendation;
7172
import com.recombee.api_client.api_requests.ItemBasedRecommendation;
7273

@@ -85,7 +86,7 @@ public class RecombeeClient {
8586

8687
final int BATCH_MAX_SIZE = 10000; //Maximal number of requests within one batch request
8788

88-
final String USER_AGENT = "recombee-java-api-client/2.4.2";
89+
final String USER_AGENT = "recombee-java-api-client/3.0.0";
8990

9091
private final OkHttpClient httpClient = new OkHttpClient();
9192

@@ -104,8 +105,14 @@ public NetworkApplicationProtocol getDefaultProtocol() {
104105
return defaultProtocol;
105106
}
106107

107-
public void setDefaultProtocol(NetworkApplicationProtocol defaultProtocol) {
108+
public RecombeeClient setDefaultProtocol(NetworkApplicationProtocol defaultProtocol) {
108109
this.defaultProtocol = defaultProtocol;
110+
return this;
111+
}
112+
public RecombeeClient setBaseUri(String baseUri)
113+
{
114+
this.baseUri = baseUri;
115+
return this;
109116
}
110117
/* Start of the generated code */
111118
public PropertyInfo send(GetItemPropertyInfo request) throws ApiException {
@@ -348,6 +355,16 @@ public RecommendationResponse send(RecommendUsersToItem request) throws ApiExcep
348355
return null;
349356
}
350357

358+
public SearchResponse send(SearchItems request) throws ApiException {
359+
String responseStr = sendRequest(request);
360+
try {
361+
return this.mapper.readValue(responseStr, SearchResponse.class);
362+
} catch (IOException e) {
363+
e.printStackTrace();
364+
}
365+
return null;
366+
}
367+
351368
/* End of the generated code */
352369

353370
public BatchResponse[] send(Batch batchRequest) throws ApiException {
@@ -439,6 +456,10 @@ else if ((request instanceof RecommendItemsToUser) ||
439456
{
440457
parsedResponse = mapper.convertValue(parsedResponse, RecommendationResponse.class);
441458
}
459+
else if (request instanceof SearchItems)
460+
{
461+
parsedResponse = mapper.convertValue(parsedResponse, SearchResponse.class);
462+
}
442463
/* Start of the generated code */
443464
else if (request instanceof GetItemPropertyInfo)
444465
{

0 commit comments

Comments
 (0)