Add predictive search to your theme
You can add predictive search to your theme so that suggested results appear immediately as you type into the search field. Predictive search helps customers articulate and refine their search queries, and provides new ways for them to explore an online store. It also lets them quickly browse matches without having to leave their current page to see a separate list of search results.
Predictive search supports suggestions for products, collections, queries, pages, and articles.
How suggestions are generatedAnchor link to section titled "How suggestions are generated"
The predictive search dropdown displays the following information when you enter a query.
|1||Predictive search dropdown|
After you start typing into the search bar, predictive search suggests results that are related to what you’re typing. They match search terms either exactly or with typo tolerance on searchable properties of shop resources.
Matching products or variants are returned as product suggestions that drop down from the search bar. For example, you’re looking for a snowboard and type
very-fast snowbo. Product suggestions appear for products or variants that contain
fast, and a term that begins with
If a word is separated by a hyphen or space, then it will be considered as two terms. Words or phrases that are separated into multiple terms return different results than a single term that includes the same words. For example,
t shirt return the same results, but
tshirt does not.
Product variants are returned only when the query matches terms specific to the variant title. Only the variants with the most matching terms are returned as results. For example, a store has a snowboard with a blue variant and a light blue variant. If you search for
snowbo, then the snowboard product is returned. However, if you search for
light blue snowbo, then only the light blue variant is returned.
Query suggestions are generated by extracting words and phrases from your product catalog, as well as from customer searches, using natural language processing techniques.
Implementing predictive searchAnchor link to section titled "Implementing predictive search"
To support predictive search, you need to implement the following components:
- The main search: A search input for predictive search to apply to.
- The predictive search section: A section to host the general predictive search display. This display is populated with the results of the predictive search using the
resourcesattribute of the
The following examples illustrate a basic predictive search implementation. These examples follow the listbox component pattern suggested by the W3C ARIA authoring practices guide.
The main searchAnchor link to section titled "The main search"
The main search is a search input for the predictive search functionality to apply to.
The predictive search sectionAnchor link to section titled "The predictive search section"
The predictive search section hosts the predictive search results. These results are output by looping through the
resources attribute of the
The following example renders the products from the search results. For a more complex template that accepts all resource types, refer to Dawn's source files.
predictive_search object isn't defined when the predictive search section is initially rendered, so you need to retrieve the populated section content using the section response of the Predictive Search API.
The following example uses the default parameters for the Predictive Search API, but you can customize them to fit your needs.