| 47 | const client = new SearchServiceClient({apiEndpoint: apiEndpoint}); |
| 48 | |
| 49 | async function search() { |
| 50 | // The full resource name of the search engine serving configuration. |
| 51 | // Example: projects/{projectId}/locations/{location}/collections/{collectionId}/dataStores/{dataStoreId}/servingConfigs/{servingConfigId} |
| 52 | // You must create a search engine in the Cloud Console first. |
| 53 | const name = client.projectLocationCollectionDataStoreServingConfigPath( |
| 54 | projectId, |
| 55 | location, |
| 56 | collectionId, |
| 57 | dataStoreId, |
| 58 | servingConfigId |
| 59 | ); |
| 60 | |
| 61 | const request = { |
| 62 | pageSize: 10, |
| 63 | query: searchQuery, |
| 64 | servingConfig: name, |
| 65 | }; |
| 66 | |
| 67 | const IResponseParams = { |
| 68 | ISearchResult: 0, |
| 69 | ISearchRequest: 1, |
| 70 | ISearchResponse: 2, |
| 71 | }; |
| 72 | |
| 73 | // Perform search request |
| 74 | const response = await client.search(request, { |
| 75 | // Warning: Should always disable autoPaginate to avoid iterate through all pages. |
| 76 | // |
| 77 | // By default NodeJS SDK returns an iterable where you can iterate through all |
| 78 | // search results instead of only the limited number of results requested on |
| 79 | // pageSize, by sending multiple sequential search requests page-by-page while |
| 80 | // iterating, until it exhausts all the search results. This will be unexpected and |
| 81 | // may cause high Search API usage and long wait time, especially when the matched |
| 82 | // document numbers are huge. |
| 83 | autoPaginate: false, |
| 84 | }); |
| 85 | const results = response[IResponseParams.ISearchResponse].results; |
| 86 | |
| 87 | for (const result of results) { |
| 88 | console.log(result); |
| 89 | } |
| 90 | } |
| 91 | // [END genappbuilder_search] |
| 92 | await search(); |
| 93 | } |