-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(retail): add Vertex AI Search for commerce snippets #10252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Vertex AI Search for commerce Samples | ||
|
|
||
| This directory contains Java samples for [Vertex AI Search for commerce](https://cloud.google.com/retail/docs/search-basic#search). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| To run these samples, you must have: | ||
|
|
||
| 1. **A Google Cloud Project** with the [Vertex AI Search for commerce API](https://console.cloud.google.com/apis/library/retail.googleapis.com) enabled. | ||
| 2. **Vertex AI Search for commerce** set up with a valid catalog and serving configuration (placement). | ||
| 3. **Authentication**: These samples use [Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/provide-credentials-adc). | ||
| - If running locally, you can set up ADC by running: | ||
| ```bash | ||
| gcloud auth application-default login | ||
| ``` | ||
| 4. **IAM Roles**: The service account or user running the samples needs the `roles/retail.viewer` (Retail Viewer) role or higher. | ||
|
|
||
| ## Samples | ||
|
|
||
| - **[Search.java](src/main/java/com/example/search/Search.java)**: Basic search request showing both text search and browse search (using categories). | ||
| - **[SearchPagination.java](src/main/java/com/example/search/SearchPagination.java)**: Shows how to use `next_page_token` to paginate through search results. | ||
| - **[SearchOffset.java](src/main/java/com/example/search/SearchOffset.java)**: Shows how to use `offset` to skip a specified number of results. | ||
|
|
||
| ## Documentation | ||
|
|
||
| For more information, see the [Vertex AI Search for commerce documentation](https://docs.cloud.google.com/retail/docs/search-basic#search). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example.retail</groupId> | ||
| <artifactId>retail-samples</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <version>1.2.2</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>21</maven.compiler.source> | ||
| <maven.compiler.target>21</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <groupId>com.google.cloud</groupId> | ||
| <scope>import</scope> | ||
| <type>pom</type> | ||
| <version>26.80.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-retail</artifactId> | ||
| </dependency> | ||
|
Comment on lines
+53
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are using the |
||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <artifactId>truth</artifactId> | ||
| <groupId>com.google.truth</groupId> | ||
| <scope>test</scope> | ||
| <version>1.4.5</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>5.14.3</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||
| /* | ||||||
| * Copyright 2026 Google LLC | ||||||
| * | ||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| * you may not use this file except in compliance with the License. | ||||||
| * You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| * See the License for the specific language governing permissions and | ||||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| package com.example.search; | ||||||
|
|
||||||
| // [START retail_v2_search_request] | ||||||
|
|
||||||
| import com.google.cloud.retail.v2.BranchName; | ||||||
| import com.google.cloud.retail.v2.Product; | ||||||
| import com.google.cloud.retail.v2.SearchRequest; | ||||||
| import com.google.cloud.retail.v2.SearchResponse; | ||||||
| import com.google.cloud.retail.v2.SearchResponse.SearchResult; | ||||||
| import com.google.cloud.retail.v2.SearchServiceClient; | ||||||
| import com.google.cloud.retail.v2.SearchServiceClient.SearchPagedResponse; | ||||||
| import com.google.cloud.retail.v2.ServingConfigName; | ||||||
| import java.io.IOException; | ||||||
| import java.util.List; | ||||||
|
|
||||||
| public class Search { | ||||||
| public static void main(String[] args) throws IOException { | ||||||
| String projectId = "my-project-id"; | ||||||
| String visitorId = "my-visitor-id"; | ||||||
| String query = "my search query"; | ||||||
| List<String> categories = List.of("category"); | ||||||
|
|
||||||
| search(projectId, visitorId, query, categories); | ||||||
| } | ||||||
|
|
||||||
| public static void search( | ||||||
| String projectId, String visitorId, String query, List<String> categories) | ||||||
| throws IOException { | ||||||
| try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { | ||||||
| ServingConfigName servingConfigName = | ||||||
| ServingConfigName.of(projectId, "global", "default_catalog", "default_search"); | ||||||
| BranchName branchName = | ||||||
| BranchName.of(projectId, "global", "default_catalog", "default_branch"); | ||||||
| SearchRequest searchRequest = | ||||||
| SearchRequest.newBuilder() | ||||||
| .setPlacement(servingConfigName.toString()) | ||||||
| .setBranch(branchName.toString()) | ||||||
| .setVisitorId(visitorId) | ||||||
| .setQuery(query) | ||||||
| .addAllPageCategories(categories) | ||||||
| .setPageSize(10) | ||||||
| .build(); | ||||||
| SearchPagedResponse response = searchServiceClient.search(searchRequest); | ||||||
|
|
||||||
| SearchResponse searchResponse = response.getPage().getResponse(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expression
Suggested change
References
|
||||||
|
|
||||||
| System.out.println("Found " + searchResponse.getResultsCount() + " results in current page"); | ||||||
| for (SearchResult searchResult : searchResponse.getResultsList()) { | ||||||
| Product product = searchResult.getProduct(); | ||||||
| System.out.println("---- Search Result ----"); | ||||||
| System.out.println("Product Name: " + product.getName()); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| // [END retail_v2_search_request] | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.search; | ||
|
|
||
| // [START retail_v2_search_offset] | ||
|
|
||
| import com.google.cloud.retail.v2.BranchName; | ||
| import com.google.cloud.retail.v2.Product; | ||
| import com.google.cloud.retail.v2.SearchRequest; | ||
| import com.google.cloud.retail.v2.SearchResponse; | ||
| import com.google.cloud.retail.v2.SearchResponse.SearchResult; | ||
| import com.google.cloud.retail.v2.SearchServiceClient; | ||
| import com.google.cloud.retail.v2.SearchServiceClient.SearchPagedResponse; | ||
| import com.google.cloud.retail.v2.ServingConfigName; | ||
| import java.io.IOException; | ||
|
|
||
| public class SearchOffset { | ||
| public static void main(String[] args) throws IOException { | ||
| String projectId = "my-project-id"; | ||
| String visitorId = "my-visitor-id"; | ||
| String query = "my search query"; | ||
| int offset = 10; | ||
|
|
||
| searchWithOffset(projectId, visitorId, query, offset); | ||
| } | ||
|
|
||
| public static void searchWithOffset(String projectId, String visitorId, String query, int offset) | ||
| throws IOException { | ||
| try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { | ||
| ServingConfigName servingConfigName = | ||
| ServingConfigName.of(projectId, "global", "default_catalog", "default_search"); | ||
| BranchName branchName = | ||
| BranchName.of(projectId, "global", "default_catalog", "default_branch"); | ||
| SearchRequest searchRequest = | ||
| SearchRequest.newBuilder() | ||
| .setPlacement(servingConfigName.toString()) | ||
| .setBranch(branchName.toString()) | ||
| .setVisitorId(visitorId) | ||
| .setQuery(query) | ||
| .setPageSize(10) | ||
| .setOffset(offset) | ||
| .build(); | ||
| SearchPagedResponse response = searchServiceClient.search(searchRequest); | ||
|
|
||
| SearchResponse searchResponse = response.getPage().getResponse(); | ||
|
|
||
| System.out.println("Found " + searchResponse.getResultsCount() + " results in current page"); | ||
| for (SearchResult searchResult : searchResponse.getResultsList()) { | ||
| Product product = searchResult.getProduct(); | ||
| System.out.println("---- Search Result ----"); | ||
| System.out.println("Product Name: " + product.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END retail_v2_search_offset] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.search; | ||
|
|
||
| // [START retail_v2_search_pagination] | ||
|
|
||
| import com.google.cloud.retail.v2.BranchName; | ||
| import com.google.cloud.retail.v2.Product; | ||
| import com.google.cloud.retail.v2.SearchRequest; | ||
| import com.google.cloud.retail.v2.SearchResponse.SearchResult; | ||
| import com.google.cloud.retail.v2.SearchServiceClient; | ||
| import com.google.cloud.retail.v2.SearchServiceClient.SearchPage; | ||
| import com.google.cloud.retail.v2.SearchServiceClient.SearchPagedResponse; | ||
| import com.google.cloud.retail.v2.ServingConfigName; | ||
| import java.io.IOException; | ||
|
|
||
| public class SearchPagination { | ||
| public static void main(String[] args) throws IOException { | ||
| String projectId = "my-project-id"; | ||
| String visitorId = "my-visitor-id"; | ||
| String query = "my search query"; | ||
| int pageSize = 10; | ||
|
|
||
| searchWithPagination(projectId, visitorId, query, pageSize); | ||
| } | ||
|
|
||
| public static void searchWithPagination( | ||
| String projectId, String visitorId, String query, int pageSize) throws IOException { | ||
| try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { | ||
| ServingConfigName servingConfigName = | ||
| ServingConfigName.of(projectId, "global", "default_catalog", "default_search"); | ||
| BranchName branchName = | ||
| BranchName.of(projectId, "global", "default_catalog", "default_branch"); | ||
| SearchRequest request = | ||
| SearchRequest.newBuilder() | ||
| .setPlacement(servingConfigName.toString()) | ||
| .setBranch(branchName.toString()) | ||
| .setVisitorId(visitorId) | ||
| .setQuery(query) | ||
| .setPageSize(pageSize) | ||
| .build(); | ||
| int currentPage = 0; | ||
| while (true) { | ||
| SearchPagedResponse response = searchServiceClient.search(request); | ||
|
|
||
| SearchPage page = response.getPage(); | ||
| currentPage++; | ||
| System.out.println("\nResults of page number " + currentPage + ":"); | ||
| System.out.println( | ||
| "Found " + page.getResponse().getResultsCount() + " results in current page"); | ||
| for (SearchResult searchResult : page.getResponse().getResultsList()) { | ||
| Product product = searchResult.getProduct(); | ||
| System.out.println("---- Search Result ----"); | ||
| System.out.println("Product Name: " + product.getName()); | ||
| } | ||
|
|
||
| if (page.hasNextPage()) { | ||
| request = request.toBuilder().setPageToken(page.getNextPageToken()).build(); | ||
| } else { | ||
| System.out.println("\nNo more available pages."); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END retail_v2_search_pagination] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description for
Search.javais inaccurate. It states that the sample shows "both text search and browse search (using categories)", but the implementation inSearch.javaonly demonstrates a basic text search using thequeryparameter. It does not include any category-based filtering or browsing logic.