Predictive search
As a section (recommended)
Section titled “As a section (recommended)”Render a section with predictive_search and swap it in under the search box:
const q = encodeURIComponent(input.value.trim());const html = await (await fetch(`/search/suggest?q=${q}&resources[type]=product,collection,query&resources[limit]=4§ion_id=predictive-search`)).text();{%- if predictive_search.performed -%} <ul role="listbox"> {%- for product in predictive_search.resources.products -%} <li role="option"><a href="{{ product.url }}">{{ product.title | highlight: predictive_search.terms }}</a></li> {%- endfor -%} </ul>{%- endif -%}As JSON
Section titled “As JSON”const data = await (await fetch(`/search/suggest.json?q=${q}&resources[type]=product&resources[limit]=6`)).json();data.resources.results.products; // [{ id, title, handle, url, price, available, vendor, image }]results has products, collections, pages, articles and queries (suggested searches, with text, styled_text and url).
Parameters
Section titled “Parameters”| Parameter | Default | |
|---|---|---|
q |
The search terms. | |
resources[type] |
all | product, collection, page, article, query, comma-separated. |
resources[limit] |
4 | Results per type, at most 10. |
Arabic searches ignore diacritics and tatweel, and treat alef forms (أ إ آ), taa marbuta and alef maqsura alike, so عطر finds عِطر and اسورة finds أسورة. Arabic-Indic digits match Western ones.
Good practice
Section titled “Good practice”- Wait about 250 ms after the last keystroke, and cancel the previous request with an
AbortController. - Use
role="combobox"on the input andaria-activedescendantfor keyboard selection. - Link the “See all results” row to
routes.search_urlwith the sameq.
Was this page helpful?

