Custom query suggestions
Custom query suggestions
This is for:
DeveloperThere are several ways to add query suggestions to your search box.
The simplest is to use pre-built Coveo Atomic components such as atomic-search-box-recent-queries
and atomic-search-box-query-suggestions
.
Another way is to use your own custom suggestions.
This article shows you how to do so.
This could be useful, for example, if you want to retrieve suggestions from an API external to the Coveo Platform.
For custom suggestions to integrate well with existing Atomic components, the Atomic library offers a function called dispatchSearchBoxSuggestionsEvent
.
You can take a look at the usage of this function, along with the relevant reference documentation, in the following sections.
Complete code example
The following code sample shows you how to leverage dispatchSearchBoxSuggestionsEvents
to provide custom query suggestions returned from a third-party API.
You can copy-paste it, as is, to start playing with the concepts discussed in this article.
<!DOCTYPE html>
<html>
<head>
<title>Custom query suggestions</title>
<script type="module" src="https://static.cloud.coveo.com/atomic/v2/atomic.esm.js"></script>
<link rel="stylesheet" href="https://static.cloud.coveo.com/atomic/v2/themes/coveo.css"/>
<script type="module">
import {dispatchSearchBoxSuggestionsEvent} from 'https://static.cloud.coveo.com/atomic/v2/index.esm.js';
function registerCustomSuggestions() {
class CustomSuggestions extends HTMLElement {
constructor() {
super();
}
suggestions = [];
renderSuggestions(bindings) {
return this.suggestions.slice(0, 2).map((suggestion) => {
const content = document.createElement('div');
content.innerText = suggestion;
return {
key: suggestion,
query: suggestion,
onSelect: () => {
bindings.searchBoxController.updateText(suggestion);
bindings.searchBoxController.submit();
},
content,
};
});
}
onInput(bindings) {
const query = bindings.searchBoxController.state.value;
return fetch('https://pokeapi.co/api/v2/pokemon')
.then((res) => res.json())
.then((res) => {
const resultsWithQuery = query ? results.filter((r) => r.name.includes(query[0])) : results;
this.suggestions = resultsWithQuery.slice(0, 10).map((r) => r.name);
});
}
async connectedCallback() {
dispatchSearchBoxSuggestionsEvent((bindings) => {
return {
position: 2,
onInput: () => this.onInput(bindings),
renderItems: () => this.renderSuggestions(bindings),
};
}, this);
}
}
window.customElements.define('custom-suggestions', CustomSuggestions);
}
async function main() {
await customElements.whenDefined('atomic-search-interface');
const searchInterface = document.querySelector('atomic-search-interface');
await searchInterface.initialize({
accessToken: '<ACCESS_TOKEN>',
organizationId: '<ORGANIZATION_ID>',
});
const engine = searchInterface.engine;
searchInterface.executeFirstSearch();
}
main();
registerCustomSuggestions();
</script>
</head>
<body>
<atomic-search-interface id="search">
<atomic-search-layout>
<atomic-layout-section section="search">
<atomic-search-box>
<custom-suggestions></custom-suggestions>
</atomic-search-box>
</atomic-layout-section>
<atomic-layout-section section="main">
<atomic-layout-section section="results">
<atomic-result-list></atomic-result-list>
</atomic-layout-section>
</atomic-layout-section>
</atomic-search-layout>
</atomic-search-interface>
</body>
</html>
Pass bindings as input for the SearchBoxSuggestionsEvent callback. |
|
The callback function needs to return a SearchBoxSuggestions object with certain required properties, as detailed in the reference section. |
|
onInput accesses the current query by using the state of the searchBoxController .
It then makes a call to an API outside of the Coveo Platform and returns a list of matching results if they included the current query.
These results are saved to be provided as suggestions. |
|
The results saved from the previous step are processed and converted into a SearchBoxSuggestionElement object with a unique key , the query to be suggested, and the HTML content to be displayed for the suggestions.
In addition to these, an onSelect callback is defined so that whenever the suggestion is selected by the user, the search box display text is updated and a search query is triggered. |
|
A new custom HTML element is defined based on the CustomSuggestions class. |
|
The newly defined custom element can be placed as a child of the <atomic-search-box> component to provide custom QS. |
Reference
Take a look at the Atomic project repository for more information about the data types and methods detailed here.
dispatchSearchBoxSuggestionsEvent
Dispatches an event which retrieves the SearchBoxSuggestionsBindings
on a configured parent search box.
Parameters
-
event:
SearchBoxSuggestionsEvent
(required)The event sent from the registered query suggestions to the parent search box.
-
element:
HTMLElement
(required)Element on which to dispatch the event, which must be the child of a configured search box.
Returns void
SearchBoxSuggestionsEvent
The event sent from the registered QS to the parent search box.
Parameters
-
bindings:
SearchBoxSuggestionsBindings
The bindings passed from the search box to the suggestions.
Returns SearchBoxSuggestions
SearchBoxSuggestionElement
Element which will be rendered in the list of suggestions.
Property | Description | Type |
---|---|---|
|
Stable identity which enables Stencil to reuse DOM elements for better performance. The best way to pick a key is to use a string that uniquely identifies that list item among its siblings (often your data will already have IDs). |
|
|
Rendered content of the element. |
|
|
Hook called when the selection is selected. |
|
|
The query associated with the suggestion which will replace the query in the search box if the suggestion is selected. |
|
|
For improved accessibility, provide this property with additional information. See Aria Label. |
|
|
Adds a specific shadow part attribute that can be selected with the CSS ::part pseudo-element. |
|
|
Hide the suggestion if it’s the last in the list. |
|
SearchBoxSuggestions
List of suggestions that will be displayed along with other lists (e.g recent queries) when the search box’s input is selected.
This will contain a list of SearchBoxSuggestionElement
objects.
Property | Description | Type |
---|---|---|
|
The search box will sort the position of suggestions using this value. The lowest value being first. Atomic provided suggestions, by default, use the DOM. |
|
|
Whether the suggestions should be listed in the right or left panel. By default, the suggestions are listed in the left panel. |
|
|
Method that returns the list of elements which will be rendered in the list of suggestions. |
|
|
Hook called when the user changes the search box’s input value. This can lead to all the QS being updated. |
|
|
Hook called when the suggested query changes as a user traverses through the list of suggestions. This is used for instant results, which are rendered based on the current suggested query. |
|
SearchBoxSuggestionsBindings
The bindings passed from the search box to the suggestions. This object gives you access to the state of the suggestion system as a whole and has important information and functions that you can invoke.
Property | Description | Type |
---|---|---|
|
The unique ID of the search box. |
|
|
Whether the search box is standalone. |
|
|
|
|
|
The amount of queries displayed when the user interacts with the search box.
This is a property set on the |
|
|
Whether to clear all active query filters when the user submits a new query from the search box. |
|
|
Retrieves the suggested query, meaning the query that would be sent if the search were executed. The suggested query changes as a user traverses through the list of suggestions. |
|
|
Removes the current suggestions. |
|
|
Triggers update & retrieval of all suggestions. |
|
|
Retrieves the current suggestions. |
|
|
Retrieves the currently suggested elements. |
|