|
| 1 | +/** |
| 2 | + * Copyright 2023 Google LLC |
| 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 | + * http://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 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +async function main( |
| 19 | + projectId, |
| 20 | + location, |
| 21 | + searchEngineId, |
| 22 | + servingConfigId, |
| 23 | + searchQuery |
| 24 | +) { |
| 25 | + // [START genappbuilder_search] |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment these variables before running the sample. |
| 28 | + */ |
| 29 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 30 | + // const location = 'YOUR_LOCATION'; // Options: 'global' |
| 31 | + // const searchEngineId = 'YOUR_SEARCH_ENGINE_ID' // Create in Cloud Console |
| 32 | + // const servingConfigId = 'default_config'; // Options: 'default_config' |
| 33 | + // const searchQuery = 'Google'; |
| 34 | + |
| 35 | + const {SearchServiceClient} = require('@google-cloud/discoveryengine').v1beta; |
| 36 | + |
| 37 | + // Instantiates a client |
| 38 | + const client = new SearchServiceClient(); |
| 39 | + |
| 40 | + async function search() { |
| 41 | + // The full resource name of the search engine serving configuration. |
| 42 | + // Example: projects/{projectId}/locations/{location}/dataStores/{searchEngineId}/servingConfigs/{servingConfigId} |
| 43 | + // You must create a search engine in the Cloud Console first. |
| 44 | + const name = client.projectLocationDataStoreServingConfigPath( |
| 45 | + projectId, |
| 46 | + location, |
| 47 | + searchEngineId, |
| 48 | + servingConfigId |
| 49 | + ); |
| 50 | + |
| 51 | + const request = { |
| 52 | + name, |
| 53 | + query: searchQuery, |
| 54 | + }; |
| 55 | + |
| 56 | + // Perform search request |
| 57 | + const response = await client.search(request); |
| 58 | + |
| 59 | + for (const result of response) { |
| 60 | + console.log(result); |
| 61 | + } |
| 62 | + } |
| 63 | + // [END genappbuilder_search] |
| 64 | + await search(); |
| 65 | +} |
| 66 | + |
| 67 | +main(...process.argv.slice(2)).catch(err => { |
| 68 | + console.error(err); |
| 69 | + process.exitCode = 1; |
| 70 | +}); |
0 commit comments