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, pages, articles, and collections.
To add predictive search to your theme, you need to use the Shopify Ajax API. And because predictive search affects the appearance of your online store, it’s important to be familiar with the UI requirements and best practices before you implement it.
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.
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 Snowboard product will be returned. However, if you search for
light blue snowbo, then only the light blue variant is returned.
Depending on the type of search, results can be based on different searchable properties.
|Type of search||Searchable properties||Products||
Predictive search includes typo tolerance, which lets search terms containing typos return the correct matching search results.
Typo tolerance is set to 1, which means that search displays results that differ from the search term by 1 letter, or results that have 2 letters in a different order. The first 4 letters of a search term need to be entered correctly for typo tolerance to take effect.
The following fields support typo tolerance:
Partial word matches
Predictive search supports partial word matches. This means that it suggests results even if the word you've entered is still incomplete. For example, if you enter
sweate, then you might see a suggested search result for
Predictive search has the following limitations when it applies partial word matches:
- If a search query has more than one term, then partial word matches are applied only to the last term in the query.
- Partial word matches are applied only to the end of a search term. For example, if you enter
book, then you won't see a suggested search result for
- Partial word matches are supported only for themes using specific languages. For more details, refer to General limitations and requirements.
Predictive search uses a different search engine than storefront search. Because of this, it doesn't handle partial word matches in the same way. Although predictive search supports partial word matches, storefront search supports them only if the prefix option parameter is set to
General requirements and limitations
Predictive search is supported only for themes that use the following theme languages:
A script tag in the
<head>section indicates whether predictive search is supported for the theme language:
<script id="shopify-features"></script>. This script tag includes a JSON-encoded
predictiveSearchkey with a boolean value. When it's set to
true, the theme language is supported, and predictive search is enabled. Otherwise, it's set to
Products that are hidden from search engines by using the
seo.hiddenmetafield object won't appear in predictive search results. To learn more, see The metafield object. It is not possible to selectively hide products from predictive search without hiding them from all of search.
The API returns no more than 10 product suggestions per request.
The API does not include unit price information about products. Unit pricing outside of predictive search is available to only certain merchants geographically. To learn more, see Displaying unit prices.
UI requirements and best practices
Before you implement predictive search, make sure that you’re familiar with the following UI requirements and best practices. These include guidance for displaying products, styling the search field, and creating a search experience that is both accessible and mobile friendly.
|Number of resources shown||
The predictive search API returns a maximum of 10 results per resource type. For example, for a query that matches on both products and pages, the API returns a maximum of 10 matching product resources and 10 matching page resources. However, if the query returns matches for multiple resource types, then consider tailoring responses to include a mix of resources. Limit the overall number of suggestions to avoid overwhelming the user or taking up too much space on the screen. Also, you can adjust the number of suggestions to display depending on the context where they appear.
An ideal desktop search experience will not include so many results that scrolling is necessary. This is a frequent problem with sites that use this structure; often the content behind the modal is what responds to scrolling.
Include a close button. It should be in the form of an icon or text that leaves the search experience and closes the product suggestion dropdown. This action should not clear the query.
Include a search button. It should be in the form of an icon or text that takes the user to the search results page. Also achieved by clicking enter on a keyboard, or “go”/”search” on a mobile keyboard.
The query should stay in the search field until the customer explicitly clears the query or navigates to a different page. The used query should be repeated on the search result page.
Hide the empty-state drop-down when there are no product suggestions that match the query. The lack of feedback from predictive search should encourage customers to continue their search from the results page.
Focus the search field when tapping on the search icon. Reduce the number of interactions required to use search. If a user taps the search icon, then focus the search field and ensure that they are able to begin searching immediately.
Allow scrolling within the drop-down. When displaying product suggestions, make sure that the user is able to scroll through the results.
|Browser and mobile autocomplete||
Disable browser-based autocomplete and search history, and mobile OS text autocorrect and autocomplete.
|Best practices||Recommended fields|
Expose only the information essential to the customer’s search. To maintain a streamlined and consistent experience, reduce search interface content and resource data to the minimum amount necessary to make selection meaningful. Using the API, you can control which elements are exposed or hidden. Although you can adapt this to the needs of the merchant, you should consider only exposing the recommended fields.
Don’t show a "Sold out" label for suggested product variants in case the user is looking for a variant that isn’t sold out. "Sold out” should be shown only when the product object within the response has its
Use a heading to label product suggestions to help users anticipate the type of results they can expect. For example, use a "Products" heading for product suggestions.
Include a clear button. Clear button or text in the search field to delete any query entered.
Display the search field, or at least the search icon, in the header of all pages. If search is a significant element of the overall experience you want to provide, then make sure that it’s displayed prominently.
For stores with only one product or a small number of products, the search field can be located within a hamburger menu.
|"More results" links||
Display a link in the list of product suggestions to indicate that there might be more results on the search results page. For example, the link could include the text "Search for [your query]" (where [your query] is what the user has entered in the search field) or "View all results."
Example search patterns
The following layouts and patterns can help you implement predictive search effectively in your theme.
Inline search drop down
Desktop: inline search
The search dropdown overlays the page, without taking up too much visual space.
Mobile: inline search
Desktop: in-menu search
The search dropdown overlays the page, without taking up too much visual space.
Predictive search results appear under the search field in the menu.
Alternatively, the search field can be positioned at the top of the menu. In this case, product suggestions would overlay the menu list (see the in-menu mobile example for this layout).
Mobile: in-menu search
Requesting predictive search results from the Ajax API
You can access predictive search results by making requests to the
/search/suggest.json? endpoint in the Shopify Ajax API. The API responds with JSON, and doesn't require custom Liquid. To learn how this endpoint works, see Shopify Ajax API.
Get predictive results for a specified search query. Predictive search supports suggestions for products, pages, articles, and collections.
||String||The search query.|
||Comma-separated values||Specifies the type of results requested. Valid values:
||Integer||Limits the number of results returned. Default:
||String||Specifies whether to display results for unavailable products. The three possible options are
||Comma-separated values||Specifies the list of fields to search on. Valid fields are:
Example call using jQuery:
Example call using Fetch:
Example call using Predictive Search Library
You can find the Predictive Search Library as part of the Shopify/theme-scripts GitHub repository.
Theme settings settings_schema.json
Global settings example:
All errors related to the request parameters are returned with a 422 status code and a relevant error message. The
description field describes the request error.
If your theme isn't using one of the supported languages listed above, then the API returns the following error:
Exceeding the request throttle limit will return a 429 status code with a relevant error message.
In this case, the response will also contain an HTTP header with the Retry-After value in seconds.
All other unexpected errors will return a 5xx status code.