Skip to content

Commit 43cbf38

Browse files
committed
feat(vertexai): Add sample retail_v2_search_pagination
1 parent b37ea03 commit 43cbf38

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
"testing"
19+
20+
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
21+
)
22+
23+
func Test_searchPagination(t *testing.T) {
24+
tc := testutil.SystemTest(t)
25+
26+
query := ""
27+
visitorID := "unique-visitor-id"
28+
29+
if err := searchPagination(tc.ProjectID, query, visitorID); err != nil {
30+
t.Fatalf("unexpected error: %v", err)
31+
}
32+
}

vertexai/retail/search_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// [START retail_v2_search_request]
2828
// searchRequest method searches for products using Vertex AI Search for commerce.
2929
// Performs a search request for a specific placement.
30-
// Handles both text search (using query) and browse search (using page_categories).
30+
// Handles both text search (using query) and browse search (using pageCategories).
3131
//
3232
// projectID: The Google Cloud project ID.
3333
// query: The search term for text search.

0 commit comments

Comments
 (0)