This page of the Google Cloud Search tutorial shows how to set up a custom search application using the embeddable search widget. To start from the beginning of this tutorial, refer to Cloud Search getting started tutorial.
Install dependencies
If the connector is still indexing the repository, open a new shell and continue there.
From the command line, change directory to
cloud-search-samples/end-to-end/search-interface
.To download the required dependencies for running the web server, run the following command:
npm install
Create the search application credentials
The connector requires service account credentials to call the Cloud Search APIs. To create the credentials:
Return to the Google Cloud console.
In the left navigation, click Credentials.
From the Create credentials drop-down list, select OAuth client ID. The "Create OAuth client ID" page appears.
(Optional). If you haven't configured the consent screen, click CONFIGURE CONSENT SCREEN. The "OAuth consent" screen appears.
Click Internal and click CREATE. Another "OAuth consent" screen appears.
Fill out required fields. For further instructions, refer to the user consent section of Setting up OAuth 2.0.
Click the Application type drop-down list and select Web application.
In the Name field, enter "tutorial".
In Authorized JavaScript origins field, click ADD URI. An empty "URIs" field appears.
In the URIs field, enter
http://localhost:8080
.Click CREATE. The "OAuth client created" screen appears.
Note the client ID. This value is used to identify the application when requesting user authorization with OAuth2. The client secret is not required for this implementation.
Click OK.
Create the search application
Next, create a search application in the admin console. The search application is a virtual representation of the search interface and its default configuration.
- Return to the Google Admin console.
- Click the Apps icon. The "Apps administration" page appears.
- Click Google Workspace. The "Apps Google Workspace administration" page appears.
- Scroll down and Click Cloud Search. The "Settings for Google Workspace" page appears.
- Click Search Applications. The "Search Appplications" page appears.
- Click the round yellow +. The "Create a new search application" dialog appears.
- In the Display name field, enter "tutorial".
- Click CREATE.
- Click the pencil icon next to the newly-created search application ("Edit search application"). The "Search application details" page appears.
- Note the Application ID.
- To the right of Data sources, click the pencil icon.
- Next to "tutorial," click the Enable toggle. This toggle enables the tutorial data source for the newly created search application.
- To the right of the "tutorial" data source, click Display options.
- Check all the facets.
- Click SAVE.
- Click DONE.
Configure the web application
After creating the credentials and search app, update the application configuration to include these values as follows:
- From the command line, change directory to `cloud-search-samples/end-to-end/search-interface/public.'
- Open
app.js
file with a text editor. - Find the
searchConfig
variable at the top of the file. - Replace
[client-id]
with the previously created OAuth client ID. - Replace
[application-id]
with the search application ID noted in the previous section. - Save the file.
Run the application
Start the application by running this command:
npm run start
Query the index
To query the index using the search widget:
- Open your browser and navigate to
http://localhost:8080
. - Click sign in to authorize the app to query Cloud Search on your behalf.
- 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.
Reviewing the code
The remaining sections examine how the user interface is built.
Loading the widget
The widget and related libraries are loaded in two phases. First, the bootstrap script is loaded:
Second, the onLoad
callback is called once the script is ready. It then loads
the Google API client, Google Sign-in, and the Cloud Search widget libraries.
The remaining initialization of the app is handled by initializeApp
once all the required libraries are loaded.
Handling authorization
Users must authorize the app to query on their behalf. While the widget can prompt users to authorize, you can achieve a better user experience by handling authorization yourself.
For the search interface, the app presents two different views depending on the sign-in state of the user.
During initialization the correct view is enabled and the handlers for sign-in and sign-out events are configured:
Creating the search interface
The search widget requires a small amount of HTML markup for the search input and to hold the search results:
The widget is initialized and bound to the input and container elements during initialization:
Congratulations, you've successfully completed the tutorial! Continue on for cleanup instructions.