|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package retail |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + retail "cloud.google.com/go/retail/apiv2" |
| 22 | + |
| 23 | + retailpb "cloud.google.com/go/retail/apiv2/retailpb" |
| 24 | +) |
| 25 | + |
| 26 | +// [START retail_v2_search_pagination] |
| 27 | +// searchPagination method searches for products with pagination using Vertex AI Search for commerce. |
| 28 | +// Performs a search request, then uses the nextToken to get the next page. |
| 29 | +// |
| 30 | +// projectID: The Google Cloud project ID. |
| 31 | +// query: The search term for text search. |
| 32 | +// visitorID: A unique identifier for the user. |
| 33 | +func searchPagination(projectID, query, visitorID string) error { |
| 34 | + ctx := context.Background() |
| 35 | + |
| 36 | + client, err := retail.NewSearchClient(ctx) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("unable to create client: %w", err) |
| 39 | + } |
| 40 | + defer client.Close() |
| 41 | + |
| 42 | + placement := fmt.Sprintf("projects/%s/locations/global/catalogs/default_catalog/placements/default_search", projectID) |
| 43 | + |
| 44 | + // First page request |
| 45 | + firstPageReq := &retailpb.SearchRequest{ |
| 46 | + Placement: placement, |
| 47 | + Query: query, |
| 48 | + VisitorId: visitorID, |
| 49 | + PageSize: 5, |
| 50 | + } |
| 51 | + |
| 52 | + it := client.Search(ctx, firstPageReq) |
| 53 | + |
| 54 | + resp, err := it.Next() |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("failed to fetch first page: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Printf("First result from page 1: %s\n", resp.GetProduct().GetName()) |
| 60 | + |
| 61 | + nextToken := it.PageInfo().Token |
| 62 | + if nextToken == "" { |
| 63 | + fmt.Println("No more pages available") |
| 64 | + return nil |
| 65 | + } |
| 66 | + fmt.Printf("Next page token: %s\n", nextToken) |
| 67 | + |
| 68 | + // Second page request using PageToken |
| 69 | + secondPageReq := &retailpb.SearchRequest{ |
| 70 | + Placement: placement, |
| 71 | + Query: query, |
| 72 | + VisitorId: visitorID, |
| 73 | + PageSize: 5, |
| 74 | + PageToken: nextToken, |
| 75 | + } |
| 76 | + |
| 77 | + it2 := client.Search(ctx, secondPageReq) |
| 78 | + resp2, err := it2.Next() |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to fetch first page: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + fmt.Printf("First result from page 2: %s\n", resp2.GetProduct().GetName()) |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// [END retail_v2_search_pagination] |
0 commit comments