-
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
Merged
iennae
merged 11 commits into
GoogleCloudPlatform:main
from
alarconesparza:alarconesparza-vertex-ai-search-502616950
Apr 30, 2026
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
88c590a
feat(retail): add Vertex AI Search for commerce snippets
alarconesparza 8453828
Clean up
alarconesparza 7d4ced5
Add search by categories
alarconesparza 9df438c
Remove dependency version
alarconesparza 549df15
Add description to samples
alarconesparza a2babab
Refactor deletion of products to test
alarconesparza 30cb5a3
Refactor method to wait for products to be ready
alarconesparza 838fdc1
Fix lint issue
alarconesparza 5e98e89
Use placementId variable
alarconesparza 4111e69
Fix lint issues
alarconesparza e58cb4e
Add test for pagination with no results
alarconesparza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?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> | ||
| <version>2.93.0</version> | ||
| </dependency> | ||
|
alarconesparza marked this conversation as resolved.
|
||
|
|
||
| <!-- 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> | ||
67 changes: 67 additions & 0 deletions
67
retail/snippets/src/main/java/com/example/search/Search.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public class Search { | ||
|
XrossFox marked this conversation as resolved.
|
||
| public static void main(String[] args) throws IOException { | ||
| String projectId = "my-project-id"; | ||
| String visitorId = "my-visitor-id"; | ||
| String query = "my search query"; | ||
|
|
||
| search(projectId, visitorId, query); | ||
| } | ||
|
|
||
| public static void search(String projectId, String visitorId, String query) 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) | ||
| .build(); | ||
| SearchPagedResponse response = searchServiceClient.search(searchRequest); | ||
|
|
||
| SearchResponse searchResponse = response.getPage().getResponse(); | ||
|
alarconesparza marked this conversation as resolved.
|
||
|
|
||
| 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()); | ||
| } | ||
| } | ||
|
XrossFox marked this conversation as resolved.
|
||
| } | ||
| } | ||
| // [END retail_v2_search_request] | ||
70 changes: 70 additions & 0 deletions
70
retail/snippets/src/main/java/com/example/search/SearchOffset.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
81 changes: 81 additions & 0 deletions
81
retail/snippets/src/main/java/com/example/search/SearchPagination.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.