Skip to content

Commit d8f3f96

Browse files
sborisenkoxgcf-owl-bot[bot]tetiana-karasovaNeenu1995
authored
chore(samples): Code samples for Retail Tutorials. Search service samples (part 2) (#290)
* Configure modules settings. * Add search tutorials. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Minor fixes. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Replace PROJECT_NUMBER with PROJECT_ID * Minor fixes. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * kokoro config files are added * Minor fixes. Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: tetiana-karasova <[email protected]> Co-authored-by: Neenu Shaji <[email protected]>
0 parents  commit d8f3f96

10 files changed

+670
-0
lines changed

retail/interactive-tutorials/pom.xml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.google.cloud</groupId>
7+
<artifactId>retail-interactive-tutorials</artifactId>
8+
<packaging>jar</packaging>
9+
<name>Google Cloud Retail Interactive Tutorials</name>
10+
<url>https://github.com/googleapis/java-retail</url>
11+
12+
<!--
13+
The parent pom defines common style checks and testing strategies for our samples.
14+
Removing or replacing it should not affect the execution of the samples in anyway.
15+
-->
16+
<parent>
17+
<groupId>com.google.cloud.samples</groupId>
18+
<artifactId>shared-configuration</artifactId>
19+
<version>1.2.0</version>
20+
</parent>
21+
22+
<properties>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
<maven.compiler.target>1.8</maven.compiler.target>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.google.cloud</groupId>
31+
<artifactId>google-cloud-retail</artifactId>
32+
<version>2.0.6</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.13.2</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-bigquery</artifactId>
43+
<version>2.5.1</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-storage</artifactId>
48+
<version>2.2.2</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.code.gson</groupId>
52+
<artifactId>gson</artifactId>
53+
<version>2.8.9</version>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.codehaus.mojo</groupId>
61+
<artifactId>exec-maven-plugin</artifactId>
62+
<version>1.2.1</version>
63+
<configuration>
64+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_search_for_products_with_filter]
18+
19+
/*
20+
* Call Retail API to search for a products in a catalog,
21+
* filter the results by different product fields.
22+
*/
23+
24+
package search;
25+
26+
import com.google.cloud.retail.v2.SearchRequest;
27+
import com.google.cloud.retail.v2.SearchResponse;
28+
import com.google.cloud.retail.v2.SearchServiceClient;
29+
import java.io.IOException;
30+
import java.util.UUID;
31+
32+
public class SearchWithFiltering {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = System.getenv("PROJECT_ID");
37+
String defaultCatalogName =
38+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
39+
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";
40+
41+
getSearchResponse(defaultSearchPlacementName);
42+
}
43+
44+
public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
45+
throws IOException {
46+
// TRY DIFFERENT FILTER EXPRESSIONS HERE:
47+
String filter = "(colorFamilies: ANY(\"Black\"))";
48+
String queryPhrase = "Tee";
49+
int pageSize = 10;
50+
String visitorId = UUID.randomUUID().toString();
51+
52+
SearchRequest searchRequest =
53+
SearchRequest.newBuilder()
54+
.setPlacement(defaultSearchPlacementName)
55+
.setVisitorId(visitorId)
56+
.setQuery(queryPhrase)
57+
.setPageSize(pageSize)
58+
.setFilter(filter)
59+
.build();
60+
61+
System.out.println("Search request: " + searchRequest);
62+
63+
try (SearchServiceClient client = SearchServiceClient.create()) {
64+
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
65+
System.out.println("Search response: " + searchResponse);
66+
67+
return searchResponse;
68+
}
69+
}
70+
}
71+
72+
// [END retail_search_for_products_with_filter]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_search_for_products_with_ordering]
18+
19+
/*
20+
* Call Retail API to search for a products in a catalog,
21+
* order the results by different product fields.
22+
*/
23+
24+
package search;
25+
26+
import com.google.cloud.retail.v2.SearchRequest;
27+
import com.google.cloud.retail.v2.SearchResponse;
28+
import com.google.cloud.retail.v2.SearchServiceClient;
29+
import java.io.IOException;
30+
import java.util.UUID;
31+
32+
public class SearchWithOrdering {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = System.getenv("PROJECT_ID");
37+
String defaultCatalogName =
38+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
39+
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";
40+
41+
getSearchResponse(defaultSearchPlacementName);
42+
}
43+
44+
public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
45+
throws IOException {
46+
// TRY DIFFERENT ORDER BY EXPRESSION HERE:
47+
String order = "price desc";
48+
String queryPhrase = "Hoodie";
49+
int pageSize = 10;
50+
String visitorId = UUID.randomUUID().toString();
51+
52+
SearchRequest searchRequest =
53+
SearchRequest.newBuilder()
54+
.setPlacement(defaultSearchPlacementName)
55+
.setQuery(queryPhrase)
56+
.setOrderBy(order)
57+
.setVisitorId(visitorId)
58+
.setPageSize(pageSize)
59+
.build();
60+
System.out.println("Search request: " + searchRequest);
61+
62+
try (SearchServiceClient client = SearchServiceClient.create()) {
63+
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
64+
System.out.println("Search response: " + searchResponse);
65+
66+
return searchResponse;
67+
}
68+
}
69+
}
70+
71+
// [END retail_search_for_products_with_ordering]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_search_for_products_with_page_size]
18+
19+
/*
20+
* Call Retail API to search for a products in a catalog,
21+
* limit the number of the products per page and go to the next page
22+
* using "next_page_token" or jump to chosen page using "offset".
23+
*/
24+
25+
package search;
26+
27+
import com.google.cloud.retail.v2.SearchRequest;
28+
import com.google.cloud.retail.v2.SearchResponse;
29+
import com.google.cloud.retail.v2.SearchServiceClient;
30+
import java.io.IOException;
31+
import java.util.UUID;
32+
33+
public class SearchWithPagination {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = System.getenv("PROJECT_ID");
38+
String defaultCatalogName =
39+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
40+
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";
41+
42+
getSearchResponse(defaultSearchPlacementName);
43+
}
44+
45+
public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
46+
throws IOException {
47+
// TRY DIFFERENT PAGINATION PARAMETERS HERE:
48+
int pageSize = 6;
49+
String queryPhrase = "Hoodie";
50+
int offset = 0;
51+
String pageToken = "";
52+
String visitorId = UUID.randomUUID().toString();
53+
54+
SearchRequest searchRequest =
55+
SearchRequest.newBuilder()
56+
.setPlacement(defaultSearchPlacementName)
57+
.setVisitorId(visitorId)
58+
.setQuery(queryPhrase)
59+
.setPageSize(pageSize)
60+
.setOffset(offset)
61+
.setPageToken(pageToken)
62+
.build();
63+
System.out.println("Search request: " + searchRequest);
64+
65+
try (SearchServiceClient client = SearchServiceClient.create()) {
66+
SearchResponse searchResponseFirstPage = client.search(searchRequest).getPage().getResponse();
67+
System.out.println("Search response: " + searchResponseFirstPage);
68+
69+
// PASTE CALL WITH NEXT PAGE TOKEN HERE:
70+
71+
// PASTE CALL WITH OFFSET HERE:
72+
73+
return searchResponseFirstPage;
74+
}
75+
}
76+
}
77+
78+
// [END retail_search_for_products_with_page_size]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START retail_search_for_products_with_query_expansion_specification]
18+
19+
/*
20+
* Call Retail API to search for a products in a catalog,
21+
* enabling the query expansion feature to let the Google Retail Search
22+
* build an automatic query expansion.
23+
*/
24+
25+
package search;
26+
27+
import com.google.cloud.retail.v2.SearchRequest;
28+
import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec;
29+
import com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.Condition;
30+
import com.google.cloud.retail.v2.SearchResponse;
31+
import com.google.cloud.retail.v2.SearchServiceClient;
32+
import java.io.IOException;
33+
import java.util.UUID;
34+
35+
public class SearchWithQueryExpansionSpec {
36+
37+
public static void main(String[] args) throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = System.getenv("PROJECT_ID");
40+
String defaultCatalogName =
41+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
42+
String defaultSearchPlacementName = defaultCatalogName + "/placements/default_search";
43+
44+
getSearchResponse(defaultSearchPlacementName);
45+
}
46+
47+
public static SearchResponse getSearchResponse(String defaultSearchPlacementName)
48+
throws IOException {
49+
// TRY DIFFERENT QUERY EXPANSION CONDITION HERE:
50+
Condition condition = Condition.AUTO;
51+
int pageSize = 10;
52+
String queryPhrase = "Google Youth Hero Tee Grey";
53+
String visitorId = UUID.randomUUID().toString();
54+
55+
QueryExpansionSpec queryExpansionSpec =
56+
QueryExpansionSpec.newBuilder().setCondition(condition).build();
57+
58+
SearchRequest searchRequest =
59+
SearchRequest.newBuilder()
60+
.setPlacement(defaultSearchPlacementName)
61+
.setQuery(queryPhrase)
62+
.setVisitorId(visitorId)
63+
.setQueryExpansionSpec(queryExpansionSpec)
64+
.setPageSize(pageSize)
65+
.build();
66+
System.out.println("Search request: " + searchRequest);
67+
68+
try (SearchServiceClient client = SearchServiceClient.create()) {
69+
SearchResponse searchResponse = client.search(searchRequest).getPage().getResponse();
70+
System.out.println("Search response: " + searchResponse);
71+
72+
return searchResponse;
73+
}
74+
}
75+
}
76+
77+
// [END retail_search_for_products_with_query_expansion_specification]

0 commit comments

Comments
 (0)