|
| 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 | + "google.golang.org/api/iterator" |
| 25 | +) |
| 26 | + |
| 27 | +// [START retail_v2_search_offset] |
| 28 | +// searchOffset method searches for products with an offset using Vertex AI Search for commerce. |
| 29 | +// |
| 30 | +// Performs a search request starting from a specified position. |
| 31 | +// |
| 32 | +// projectID: The Google Cloud project ID. |
| 33 | +// query: The search term. |
| 34 | +// visitorID: A unique identifier for the user. |
| 35 | +// offset: The number of results to skip.for products using Vertex AI Search for commerce. |
| 36 | +func searchOffset(projectID, query, visitorID string, offset int32) error { |
| 37 | + ctx := context.Background() |
| 38 | + |
| 39 | + client, err := retail.NewSearchClient(ctx) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("unable to create client: %w", err) |
| 42 | + } |
| 43 | + defer client.Close() |
| 44 | + |
| 45 | + req := &retailpb.SearchRequest{ |
| 46 | + Placement: fmt.Sprintf("projects/%s/locations/global/catalogs/default_catalog/placements/default_search", projectID), |
| 47 | + Query: query, |
| 48 | + VisitorId: visitorID, |
| 49 | + PageSize: 10, |
| 50 | + Offset: offset, |
| 51 | + } |
| 52 | + |
| 53 | + it := client.Search(ctx, req) |
| 54 | + for { |
| 55 | + resp, err := it.Next() |
| 56 | + if err == iterator.Done { |
| 57 | + break |
| 58 | + } |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("it.Next: %w", err) |
| 61 | + } |
| 62 | + fmt.Printf("Search item: %v\n", resp) |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// [END retail_v2_search_offset] |
0 commit comments