-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(retail): add searchOffset, searchPagination, and searchRequest samples #4285
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
Open
angelcaamal
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
angelcaamal:feat-retail-search-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+415
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
356aa22
feat(retail): add searchOffset, searchPagination, and searchRequest s…
angelcaamal d844a3f
test(retail): fix Mocha test logic for search samples
angelcaamal 153b4b7
refactor(retail): improve error logging in search samples
angelcaamal d297a64
test: add delay to system tests for search indexing
angelcaamal 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,22 @@ | ||
| { | ||
| "name": "node-snippets", | ||
| "license": "Apache-2.0", | ||
| "author": "Google LLC", | ||
| "engines": { | ||
| "node": ">=18.0.0" | ||
| }, | ||
| "files": [ | ||
| "*.js" | ||
| ], | ||
| "scripts": { | ||
| "test": "c8 mocha test/*.test.js --timeout 180000" | ||
| }, | ||
| "devDependencies": { | ||
| "c8": "^10.1.3", | ||
| "chai": "^4.5.0", | ||
| "mocha": "^10.8.2" | ||
| }, | ||
| "dependencies": { | ||
| "@google-cloud/retail": "^4.3.0" | ||
| } | ||
| } |
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,71 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START retail_v2_search_offset] | ||
| const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
|
||
| const client = new SearchServiceClient(); | ||
|
|
||
| /** | ||
| * Search for products with an offset using Vertex AI Search for commerce. | ||
| * Performs a search request starting from a specified position. | ||
| * | ||
| * @param {string} projectId The Google Cloud project ID. | ||
| * @param {string} placementId The placement name for the search. | ||
| * @param {string} visitorId A unique identifier for the user. | ||
| * @param {string} query The search term. | ||
| * @param {number} offset The number of results to skip. | ||
| */ | ||
| async function searchOffset(projectId, placementId, visitorId, query, offset) { | ||
| const placementPath = client.servingConfigPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| placementId | ||
| ); | ||
|
|
||
| const branchPath = client.branchPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| 'default_branch' | ||
| ); | ||
|
|
||
| const request = { | ||
| placement: placementPath, | ||
| branch: branchPath, | ||
| visitorId: visitorId, | ||
| query: query, | ||
| pageSize: 10, | ||
| offset: offset, | ||
| }; | ||
|
|
||
| try { | ||
| // Set {autoPaginate: false} to manually control the pagination | ||
| const [results] = await client.search(request, {autoPaginate: false}); | ||
| console.log(`--- Results for offset: ${offset} ---`); | ||
| for (const result of results) { | ||
| console.log(`Product ID: ${result.id}`); | ||
| console.log(`Title: ${result.product.title}`); | ||
| console.log(`Scores: ${JSON.stringify(result.modelScores || {})}`); | ||
| } | ||
| } catch (error) { | ||
| console.error('Error searching using offset:', error.message || error); | ||
| } | ||
| } | ||
|
|
||
| // [END retail_v2_search_offset] | ||
| module.exports = {searchOffset}; |
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,101 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START retail_v2_search_pagination] | ||
| const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
|
||
| const client = new SearchServiceClient(); | ||
|
|
||
| /** | ||
| * Search for products with pagination using Vertex AI Search for commerce. | ||
| * Performs a search request, then uses the next_page_token to get the next page. | ||
| * | ||
| * @param {string} projectId - The Google Cloud project ID. | ||
| * @param {string} placementId - The placement name for the search. | ||
| * @param {string} visitorId - A unique identifier for the user. | ||
| * @param {string} query - The search term. | ||
| */ | ||
| async function searchPagination(projectId, placementId, visitorId, query) { | ||
| const placementPath = client.servingConfigPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| placementId | ||
| ); | ||
|
|
||
| const branchPath = client.branchPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| 'default_branch' | ||
| ); | ||
|
|
||
| // First page request | ||
| const firstRequest = { | ||
| placement: placementPath, | ||
| branch: branchPath, | ||
| visitorId: visitorId, | ||
| query: query, | ||
| pageSize: 1, | ||
| }; | ||
|
|
||
| try { | ||
| // Set {autoPaginate: false} to manually control the pagination | ||
| // and extract the raw response which contains the next_page_token. | ||
| const [firstPageResults, , firstRawResponse] = await client.search( | ||
| firstRequest, | ||
| {autoPaginate: false} | ||
| ); | ||
|
|
||
| console.log('--- First Page ---'); | ||
| for (const result of firstPageResults) { | ||
| console.log(`Product ID: ${result.id}`); | ||
| } | ||
|
|
||
| const nextPageToken = firstRawResponse.nextPageToken; | ||
|
|
||
| if (nextPageToken) { | ||
| // Second page request using pageToken | ||
| const secondRequest = { | ||
| placement: placementPath, | ||
| branch: branchPath, | ||
| visitorId: visitorId, | ||
| query: query, | ||
| pageSize: 1, | ||
| pageToken: nextPageToken, | ||
| }; | ||
|
|
||
| const [secondPageResults] = await client.search(secondRequest, { | ||
| autoPaginate: false, | ||
| }); | ||
|
|
||
| console.log('--- Second Page ---'); | ||
| for (const result of secondPageResults) { | ||
| console.log(`Product ID: ${result.id}`); | ||
| } | ||
| } else { | ||
| console.log('No more pages.'); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| 'Failed to complete paginated search:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
| // [END retail_v2_search_pagination] | ||
|
|
||
| module.exports = {searchPagination}; |
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,80 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START retail_v2_search_request] | ||
| const {SearchServiceClient} = require('@google-cloud/retail'); | ||
|
|
||
| const client = new SearchServiceClient(); | ||
|
|
||
| /** | ||
| * Search for products using Vertex AI Search for commerce. | ||
| * | ||
| * Performs a search request for a specific placement. | ||
| * Handles both text search (using query) and browse search (using pageCategories). | ||
| * | ||
| * @param {string} projectId - The Google Cloud project ID. | ||
| * @param {string} placementId - The placement name for the search. | ||
| * @param {string} visitorId - A unique identifier for the user. | ||
| * @param {string} query - The search term for text search. | ||
| * @param {string[]} pageCategories - The categories for browse search. | ||
| */ | ||
| async function searchRequest( | ||
| projectId, | ||
| placementId, | ||
| visitorId, | ||
| query = '', | ||
| pageCategories = [] | ||
| ) { | ||
| const placementPath = client.servingConfigPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| placementId | ||
| ); | ||
|
|
||
| const branchPath = client.branchPath( | ||
| projectId, | ||
| 'global', | ||
| 'default_catalog', | ||
| 'default_branch' | ||
| ); | ||
|
|
||
| const request = { | ||
| placement: placementPath, | ||
| branch: branchPath, | ||
| visitorId: visitorId, | ||
| query: query, | ||
| pageCategories: pageCategories, | ||
| pageSize: 10, | ||
| }; | ||
|
|
||
| try { | ||
| // Set {autoPaginate: false} to manually control the pagination | ||
| const [results] = await client.search(request, {autoPaginate: false}); | ||
| console.log('--- Search Results ---'); | ||
| for (const result of results) { | ||
| console.log(`Product ID: ${result.id}`); | ||
| console.log(`Title: ${result.product.title}`); | ||
| console.log(`Scores: ${JSON.stringify(result.modelScores || {})}`); | ||
| } | ||
| } catch (error) { | ||
| console.error('Error executing search request:', error.message || error); | ||
| } | ||
| } | ||
|
|
||
| // [END retail_v2_search_request] | ||
|
|
||
| module.exports = {searchRequest}; | ||
Oops, something went wrong.
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.