Skip to content

Commit b37ea03

Browse files
committed
feat(vertexai): Add sample retail_v2_search_offset
1 parent 4a7f3da commit b37ea03

3 files changed

Lines changed: 101 additions & 1 deletion

File tree

vertexai/retail/search_offset.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_searchOffset(t *testing.T) {
24+
tc := testutil.SystemTest(t)
25+
26+
query := ""
27+
visitorID := "unique-visitor-id"
28+
offset := int32(10)
29+
30+
if err := searchOffset(tc.ProjectID, query, visitorID, offset); err != nil {
31+
t.Fatalf("unexpected error: %v", err)
32+
}
33+
}

vertexai/retail/search_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
// Handles both text search (using query) and browse search (using page_categories).
3131
//
3232
// projectID: The Google Cloud project ID.
33-
// visitorID: A unique identifier for the user.
3433
// query: The search term for text search.
34+
// visitorID: A unique identifier for the user.
3535
// pageCategories: The categories for browse search.
3636
func searchRequest(projectID, query, visitorID string, pageCategories []string) error {
3737
ctx := context.Background()

0 commit comments

Comments
 (0)