設定使用者介面
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本 Google Cloud Search 教學課程頁面說明如何使用可嵌入的搜尋小工具,設定自訂搜尋應用程式。如要從本教學課程的開頭開始,請參閱 Cloud Search 入門教學課程。
安裝依附元件
如果連接器仍在為存放區建立索引,請開啟新的殼層並繼續操作。
在指令列中,將目錄變更為 cloud-search-samples/end-to-end/search-interface
。
如要下載執行網路伺服器所需的依附元件,請執行下列指令:
npm install
建立搜尋應用程式憑證
連接器需要服務帳戶憑證,才能呼叫 Cloud Search API。如要建立憑證,請按照下列步驟操作:
返回 Google Cloud 控制台。
在左側導覽列中,按一下「憑證」。
在「Create credentials」(建立憑證) 下拉式清單中,選取「OAuth client ID」(OAuth 用戶端 ID)。系統會顯示「建立 OAuth 用戶端 ID」頁面。
(選用) 如果您尚未設定同意畫面,請按一下「設定同意畫面」。系統會顯示「OAuth 同意」畫面。
按一下「內部」,然後按一下「建立」。系統會顯示另一個「OAuth 同意」畫面。
填寫必填欄位。如需進一步的操作說明,請參閱設定 OAuth 2.0 的使用者同意聲明一節。
按一下「應用程式類型」下拉式清單,然後選取「網頁應用程式」。
在「Name」欄位中輸入「tutorial」。
在「已授權的 JavaScript 來源」欄位中,按一下「新增 URI」。畫面上會顯示空白的「URI」欄位。
在「URIs」(URI) 欄位中輸入 http://localhost:8080
。
按一下「建立」。系統會顯示「已建立 OAuth 用戶端」畫面。
記下用戶端 ID。透過 OAuth2 要求使用者授權時,系統會使用這個值來識別應用程式。這項實作作業不需要用戶端密鑰。
按一下「確定」。
建立搜尋應用程式
接著,在管理控制台中建立搜尋應用程式。搜尋應用程式是搜尋介面及其預設設定的虛擬代表。
- 返回 Google 管理控制台。
- 按一下「應用程式」圖示。系統隨即會顯示「應用程式管理」頁面。
- 按一下「Google Workspace」。系統會顯示「應用程式 Google Workspace 管理」頁面。
- 向下捲動並按一下「Cloud Search」。系統會顯示「Google Workspace 設定」頁面。
- 按一下「搜尋應用程式」。畫面上會顯示「搜尋應用程式」頁面。
- 按一下黃色圓圈中的 +,隨即會顯示「建立新的搜尋應用程式」對話方塊。
- 在「顯示名稱」欄位中,輸入「tutorial」。
- 點選「建立」。
- 按一下新建立搜尋應用程式旁邊的鉛筆圖示 (「編輯搜尋應用程式」)。「Search application details」(搜尋應用程式詳細資料) 頁面隨即顯示。
- 記下「應用程式 ID」。
- 按一下「資料來源」右側的鉛筆圖示。
- 按一下「教學課程」旁的「啟用」切換鈕。這個切換鈕會為新建立的搜尋應用程式啟用教學課程資料來源。
- 按一下「教學課程」資料來源右側的「顯示選項」。
- 勾選所有層面。
- 按一下 [儲存]。
- 按一下 [完成]。
建立憑證和搜尋應用程式後,請更新應用程式設定,加入這些值,如下所示:
- 從指令列將目錄變更為 `cloud-search-samples/end-to-end/search-interface/public`。
- 使用文字編輯器開啟
app.js
檔案。
- 在檔案頂端找到
searchConfig
變數。
- 將
[client-id]
替換為先前建立的 OAuth 用戶端 ID。
- 將
[application-id]
換成上一個章節中記下的搜尋應用程式 ID。
- 儲存檔案。
執行應用程式
執行下列指令來啟動應用程式:
npm run start
查詢索引
如要使用搜尋小工具查詢索引,請按照下列步驟操作:
- 開啟瀏覽器並前往
http://localhost:8080
。
- 按一下「登入」,授權應用程式代表您查詢 Cloud Search。
- 在搜尋框中輸入查詢內容,例如「測試」一詞,然後按下 Enter 鍵。頁面應顯示查詢結果,以及用於瀏覽結果的資料類型和分頁控制項。
檢查程式碼
其餘章節將探討如何建構使用者介面。
小工具和相關程式庫會分兩個階段載入。首先,系統會載入 Bootstrap 指令碼:
其次,指令碼準備就緒後,系統會呼叫 onLoad
回呼。然後載入 Google API 用戶端、Google 登入和 Cloud Search 小工具程式庫。
載入所有必要程式庫後,應用程式的其餘初始化作業會由 initializeApp
處理。
處理授權
使用者必須授權應用程式代為查詢。雖然小工具可以提示使用者授權,但自行處理授權程序可提供更優質的使用者體驗。
應用程式會根據使用者的登入狀態,在搜尋介面中顯示兩種不同的檢視畫面。
初始化期間會啟用正確的檢視區塊,並設定登入和登出事件的處理常式:
建立搜尋介面
搜尋小工具需要少量的 HTML 標記,才能提供搜尋輸入內容並保留搜尋結果:
小工具會在初始化期間完成初始化,並繫結至輸入和容器元素:
恭喜,您已順利完成本教學課程!請繼續閱讀清除說明。
返回
繼續
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Set up the user interface\n\nThis page of the Google Cloud Search tutorial shows how to set up a custom\nsearch application using the embeddable search widget.\nTo start from the beginning of this tutorial, refer to\n[Cloud Search getting started tutorial](/workspace/cloud-search/docs/tutorials/end-to-end).\n\nInstall dependencies\n--------------------\n\n1. If the connector is still indexing the repository, open a new shell and\n continue there.\n\n2. From the command line, change directory to\n `cloud-search-samples/end-to-end/search-interface`.\n\n3. To download the required dependencies for\n running the web server, run the following command:\n\n npm install\n\nCreate the search application credentials\n-----------------------------------------\n\nThe connector requires service account credentials to call the Cloud Search\nAPIs. To create the credentials:\n\n1. Return to the [Google Cloud console](https://console.cloud.google.com/apis/dashboard).\n\n2. In the left navigation, click **Credentials**.\n\n3. From the **Create credentials** drop-down list, select\n **OAuth client ID**. The \"Create OAuth client ID\" page appears.\n\n4. (Optional). If you haven't configured the consent screen, click\n **CONFIGURE CONSENT SCREEN**. The \"OAuth consent\" screen appears.\n\n 1. Click **Internal** and click **CREATE**. Another \"OAuth consent\" screen\n appears.\n\n 2. Fill out required fields. For further instructions, refer to the user\n consent section of\n [Setting up OAuth 2.0](https://support.google.com/cloud/answer/6158849).\n\n5. Click the **Application type** drop-down list and select **Web application**.\n\n6. In the **Name** field, enter \"tutorial\".\n\n7. In **Authorized JavaScript origins** field, click **ADD URI**. An empty\n \"URIs\" field appears.\n\n8. In the **URIs** field, enter `http://localhost:8080`.\n\n9. Click **CREATE**. The \"OAuth client created\" screen appears.\n\n10. Note the client ID. This value is used to identify the application when\n requesting user authorization with OAuth2. The client secret is not required\n for this implementation.\n\n11. Click **OK**.\n\nCreate the search application\n-----------------------------\n\nNext, create a search application in the admin console. The search application\nis a virtual representation of the search interface and its default\nconfiguration.\n| **Note:** These steps must be performed by a domain administrator.\n\n1. Return to the [Google Admin console](https://admin.google.com).\n2. Click the Apps icon. The \"Apps administration\" page appears.\n3. Click **Google Workspace**. The \"Apps Google Workspace administration\" page appears.\n4. Scroll down and Click **Cloud Search**. The \"Settings for Google Workspace\" page appears.\n5. Click **Search Applications**. The \"Search Appplications\" page appears.\n6. Click the round yellow **+**. The \"Create a new search application\" dialog appears.\n7. In the **Display name** field, enter \"tutorial\".\n8. Click **CREATE**.\n9. Click the pencil icon next to the newly-created search application (\"Edit search application\"). The \"Search application details\" page appears.\n10. Note the **Application ID**.\n11. To the right of **Data sources**, click the pencil icon.\n12. Next to \"tutorial,\" click the **Enable** toggle. This toggle enables the tutorial data source for the newly created search application.\n13. To the right of the \"tutorial\" data source, click **Display options**.\n14. Check all the facets.\n15. Click **SAVE**.\n16. Click **DONE**.\n\nConfigure the web application\n-----------------------------\n\nAfter creating the credentials and search app, update the application\nconfiguration to include these values as follows:\n\n1. From the command line, change directory to \\`cloud-search-samples/end-to-end/search-interface/public.'\n2. Open `app.js` file with a text editor.\n3. Find the `searchConfig` variable at the top of the file.\n4. Replace `[client-id]` with the previously created OAuth client ID.\n5. Replace `[application-id]` with the search application ID noted in the previous section.\n6. Save the file.\n\nRun the application\n-------------------\n\nStart the application by running this command: \n\n npm run start\n\nQuery the index\n---------------\n\nTo query the index using the search widget:\n\n1. Open your browser and navigate to `http://localhost:8080`.\n2. Click **sign in** to authorize the app to query Cloud Search on your behalf.\n3. In the search box, enter a query, such as the word \"test,\" and press **enter**. The page should display the query results along with facets and pagination controls to navigate the results.\n\nReviewing the code\n------------------\n\nThe remaining sections examine how the user interface is built.\n\n### Loading the widget\n\nThe widget and related libraries are loaded in two phases. First, the bootstrap\nscript is loaded: \nindex.html \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/index.html) \n\n```html\n\u003cscript src=\"https://apis.google.com/js/api.js?mods=enable_cloud_search_widget&onload=onLoad\" async defer\u003e\u003c/script\u003e\n```\n\nSecond, the `onLoad` callback is called once the script is ready. It then loads\nthe Google API client, Google Sign-in, and the Cloud Search widget libraries. \napp.js \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/app.js) \n\n```javascript\n/**\n * Load the cloud search widget & auth libraries. Runs after\n * the initial gapi bootstrap library is ready.\n */\nfunction onLoad() {\n gapi.load('client:auth2:cloudsearch-widget', initializeApp)\n}\n```\n\nThe remaining initialization of the app is handled by `initializeApp`\nonce all the required libraries are loaded.\n\n### Handling authorization\n\nUsers must authorize the app to query on their behalf. While the widget\ncan prompt users to authorize, you can achieve a better user experience\nby handling authorization yourself.\n\nFor the search interface, the app presents two different views depending\non the sign-in state of the user. \nindex.html \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/index.html) \n\n```html\n\u003cdiv class=\"content\"\u003e\n \u003cdiv id=\"app\" hidden\u003e\n \u003cdiv id=\"header\"\u003e\n \u003cbutton id=\"sign-out\"\u003eSign-out\u003c/button\u003e\n \u003c/div\u003e\n \u003c!-- Markup for widget...--\u003e\n \u003c/div\u003e\n \u003cdiv id=\"welcome\" hidden\u003e\n \u003ch1\u003eCloud Search Tutorial\u003c/h1\u003e\n \u003cp\u003eSign in with your Google account to search.\u003c/p\u003e\n \u003cbutton id=\"sign-in\"\u003eSign-in\u003c/button\u003e\n \u003c/div\u003e\n\u003c/div\u003e\n```\n\nDuring initialization the correct view is enabled and the handlers for\nsign-in and sign-out events are configured: \napp.js \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/app.js) \n\n```javascript\n/**\n * Initialize the app after loading the Google API client &\n * Cloud Search widget.\n */\nasync function initializeApp() {\n await gapi.auth2.init({\n 'clientId': searchConfig.clientId,\n 'scope': 'https://www.googleapis.com/auth/cloud_search.query'\n });\n\n let auth = gapi.auth2.getAuthInstance();\n\n // Watch for sign in status changes to update the UI appropriately\n let onSignInChanged = (isSignedIn) =\u003e {\n document.getElementById(\"app\").hidden = !isSignedIn;\n document.getElementById(\"welcome\").hidden = isSignedIn;\n if (resultsContainer) {\n resultsContainer.clear();\n }\n }\n auth.isSignedIn.listen(onSignInChanged);\n onSignInChanged(auth.isSignedIn.get()); // Trigger with current status\n\n // Connect sign-in/sign-out buttons\n document.getElementById(\"sign-in\").onclick = (e) =\u003e auth.signIn();\n document.getElementById(\"sign-out\").onclick = (e) =\u003e auth.signOut();\n\n // ...\n\n}\n```\n\n### Creating the search interface\n\nThe search widget requires a small amount of HTML markup for the search\ninput and to hold the search results: \nindex.html \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/index.html) \n\n```html\n\u003cdiv id=\"search_bar\"\u003e\n \u003cdiv\u003e\n \u003cdiv id=\"suggestions_anchor\"\u003e\n \u003cinput type=\"text\" id=\"search_input\" placeholder=\"Search for...\"\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv id=\"facet_results\" \u003e\u003c/div\u003e\n\u003cdiv id=\"search_results\" \u003e\u003c/div\u003e\n```\n\nThe widget is initialized and bound to the input and container elements\nduring initialization: \napp.js \n[View on GitHub](https://github.com/googleworkspace/cloud-search-samples/blob/master/end-to-end/search-interface/public/app.js) \n\n```javascript\ngapi.config.update('cloudsearch.config/apiVersion', 'v1');\nresultsContainer = new gapi.cloudsearch.widget.resultscontainer.Builder()\n .setSearchApplicationId(searchConfig.searchAppId)\n .setSearchResultsContainerElement(document.getElementById('search_results'))\n .setFacetResultsContainerElement(document.getElementById('facet_results'))\n .build();\n\nconst searchBox = new gapi.cloudsearch.widget.searchbox.Builder()\n .setSearchApplicationId(searchConfig.searchAppId)\n .setInput(document.getElementById('search_input'))\n .setAnchor(document.getElementById('suggestions_anchor'))\n .setResultsContainer(resultsContainer)\n .build();\n```\n\nCongratulations, you've successfully completed the tutorial! Continue on for\ncleanup instructions.\n\n[Previous](/workspace/cloud-search/docs/tutorials/end-to-end/setup-connector)\n[Next](/workspace/cloud-search/docs/tutorials/end-to-end/finish)"]]