The search widget provides a customizable search interface for web applications. It requires minimal HTML and JavaScript to implement and supports common features like facets and pagination. You can also customize the interface with CSS and JavaScript.
If you need more flexibility, use the Query API. See Creating a search interface with the Query API.
Build a search interface
Building the search interface requires these steps:
- Configure a search application.
- Generate a client ID for the application.
- Add HTML markup for the search box and results.
- Load the widget on the page.
- Initialize the widget.
Configure a search application
Each search interface requires a search application defined in the Admin console. The application provides query settings, such as data sources, facets, and search quality parameters.
To create a search application, see Create a custom search experience.
Generate a client ID for the application
In addition to the steps in Configure access to the Cloud Search API, generate a client ID for your web application.
When configuring the project:
- Select the Web browser client type.
- Provide the origin URI of your app.
- Note the client ID. The widget doesn't require a client secret.
For more information, see OAuth 2.0 for Client-side Web Application.
Add HTML markup
The widget requires these HTML elements:
- An
inputelement for the search box. - An element to anchor the suggestion dialog.
- An element for search results.
- (Optional) An element for facet controls.
This snippet shows elements identified by their id attributes:
Load the widget
Include the loader using a <script> tag:
Provide an onload callback. When the loader is ready, call
gapi.load()
to load the API client, Google Sign-in, and Cloud Search
modules.
Initialize the widget
Initialize the client library using gapi.client.init() or
gapi.auth2.init() with your client ID and the
https://www.googleapis.com/auth/cloud_search.query scope. Use the builder
classes to configure and bind the widget.
Example initialization:
Configuration variables:
Customize the sign-in experience
The widget prompts users to sign in when they start typing. You can use Google Sign-in for Websites for a tailored experience.
Authorize users directly
Use Sign In With Google to monitor and manage sign-in states.
This example uses GoogleAuth.signIn() on a button click:
Automatically sign in users
Pre-authorize the application for users in your organization to streamline sign-in. This is also useful with Cloud Identity Aware Proxy. See Use Google Sign-In with IT Apps.
Customize the interface
You can change the widget's appearance by:
- Overriding styles with CSS.
- Decorating elements with an adapter.
- Creating custom elements with an adapter.
Override styles with CSS
The widget includes its own CSS. To override it, use ancestor selectors to increase specificity:
#suggestions_anchor .cloudsearch_suggestion_container {
font-size: 14px;
}
See the Supported CSS classes reference.
Decorate elements with an adapter
Create and register an adapter to modify elements before rendering. This example adds a custom CSS class:
Register the adapter during initialization:
Create custom elements with an adapter
Implement createSuggestionElement, createFacetResultElement, or
createSearchResultElement to build custom UI components. This example uses
HTML <template> tags:
Register the adapter:
Custom facet elements must follow these rules:
- Attach
cloudsearch_facet_bucket_clickableto clickable elements. - Wrap each bucket in a
cloudsearch_facet_bucket_container. - Maintain the bucket order from the response.
For example, the following snippet renders facets using links instead of check boxes.
Customize search behavior
Override search application settings by intercepting requests with an adapter.
Implement interceptSearchRequest to modify requests before execution. This
example restricts queries to a selected source:
Register the adapter:
The following HTML is used to display a select box for filtering by sources:
The following code listens for the change, sets the selection, and re-executes the query if necessary.
You can also intercept the search response by implementing
interceptSearchResponse
in the adapter.
Pin versions
- API version: Set
cloudsearch.config/apiVersionbefore initializing. - Widget version: Use
gapi.config.update('cloudsearch.config/clientVersion', 1.1).
Both default to 1.0 if unset.
For example, to pin the widget to version 1.1:
Secure the search interface
Follow security best practices for web applications, especially to prevent clickjacking.
Enable debugging
Use
interceptSearchRequest
to enable debugging:
if (!request.requestOptions) {
request.requestOptions = {};
}
request.requestOptions.debugOptions = {enableDebugging: true};
return request;