Skip to content

Commit 0d69c03

Browse files
authored
fix: Updates to Vertex AI Search samples after GA (GoogleCloudPlatform#8662)
* fix: Updates to Vertex AI Search samples after GA - Change `searchEngineId` to `dataStoreId` - Add Endpoint for non-global data stores. * Updates to comments * fix: Line length * Fix syntax error
1 parent 151774e commit 0d69c03

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

discoveryengine/src/main/java/discoveryengine/v1/Search.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.discoveryengine.v1.SearchRequest;
2222
import com.google.cloud.discoveryengine.v1.SearchResponse;
2323
import com.google.cloud.discoveryengine.v1.SearchServiceClient;
24+
import com.google.cloud.discoveryengine.v1.SearchServiceSettings;
2425
import com.google.cloud.discoveryengine.v1.ServingConfigName;
2526
import java.io.IOException;
2627
import java.util.concurrent.ExecutionException;
@@ -30,38 +31,45 @@ public static void main() throws IOException, ExecutionException {
3031
// TODO(developer): Replace these variables before running the sample.
3132
// Project ID or project number of the Cloud project you want to use.
3233
String projectId = "PROJECT_ID";
33-
// Location of the search engine. Options: "global"
34+
// Location of the data store. Options: "global", "us", "eu"
3435
String location = "global";
35-
// Collection containing the datastore.
36+
// Collection containing the data store.
3637
String collectionId = "default_collection";
37-
// Datastore/Search Engine ID.
38-
String searchEngineId = "DATA_STORE_ID";
38+
// Data store ID.
39+
String dataStoreId = "DATA_STORE_ID";
3940
// Serving configuration. Options: "default_search"
4041
String servingConfigId = "default_search";
41-
// Search Query for the search engine.
42+
// Search Query for the data store.
4243
String searchQuery = "Google";
43-
search(projectId, location, collectionId, searchEngineId, servingConfigId, searchQuery);
44+
search(projectId, location, collectionId, dataStoreId, servingConfigId, searchQuery);
4445
}
4546

46-
/** Performs a search on a given datastore/search engine. */
47+
/** Performs a search on a given datastore. */
4748
public static void search(
4849
String projectId,
4950
String location,
5051
String collectionId,
51-
String searchEngineId,
52+
String dataStoreId,
5253
String servingConfigId,
5354
String searchQuery)
5455
throws IOException, ExecutionException {
56+
// For more information, refer to:
57+
// https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
58+
String endpoint = (location.equals("global"))
59+
? String.format("discoveryengine.googleapis.com:443", location)
60+
: String.format("%s-discoveryengine.googleapis.com:443", location);
61+
SearchServiceSettings settings =
62+
SearchServiceSettings.newBuilder().setEndpoint(endpoint).build();
5563
// Initialize client that will be used to send requests. This client only needs to be created
5664
// once, and can be reused for multiple requests. After completing all of your requests, call
5765
// the `searchServiceClient.close()` method on the client to safely
5866
// clean up any remaining background resources.
59-
try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) {
67+
try (SearchServiceClient searchServiceClient = SearchServiceClient.create(settings)) {
6068
SearchRequest request =
6169
SearchRequest.newBuilder()
6270
.setServingConfig(
6371
ServingConfigName.formatProjectLocationCollectionDataStoreServingConfigName(
64-
projectId, location, collectionId, searchEngineId, servingConfigId))
72+
projectId, location, collectionId, dataStoreId, servingConfigId))
6573
.setQuery(searchQuery)
6674
.setPageSize(10)
6775
.build();

discoveryengine/src/test/java/discoveryengine/v1/SearchTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SearchTest {
3333
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3434
private static final String LOCATION = "global";
3535
private static final String COLLECTION_ID = "default_collection";
36-
private static final String SEARCH_ENGINE_ID = "test-search-engine";
36+
private static final String DATA_STORE_ID = "test-search-engine";
3737
private static final String SERVING_CONFIG_ID = "default_search";
3838
private static final String SEARCH_QUERY = "Google";
3939

@@ -64,7 +64,7 @@ public void setUp() {
6464
@Test
6565
public void testSearch() throws Exception {
6666
Search.search(
67-
PROJECT_ID, LOCATION, COLLECTION_ID, SEARCH_ENGINE_ID, SERVING_CONFIG_ID, SEARCH_QUERY);
67+
PROJECT_ID, LOCATION, COLLECTION_ID, DATA_STORE_ID, SERVING_CONFIG_ID, SEARCH_QUERY);
6868
String got = bout.toString();
6969

7070
assertThat(got).contains("Response content:");

0 commit comments

Comments
 (0)