# Predictive search

As-you-type search suggestions, the search switches, and search inside listings and the blog: endpoints, ctx fields and examples.

Themes get as-you-type search suggestions from one JSON endpoint. It reads the same channel-scoped index as the search page — every word in the query must match, the last one matches as a prefix, and the store's synonyms apply — so the dropdown and the results page never disagree.

## The endpoint

```http
GET {{ routes.search_suggest_url }}?q=<query>

GET /search/suggest?q=mug          (the default address)
GET /en-in/search/suggest?q=mug    (an en-in page)
```

Read the URL from `routes.search_suggest_url` (add `routes` to the section's `ctx_needs`) rather than writing /search/suggest. It is the endpoint under the language and country address the page was served at, so on an /en-in page it is /en-in/search/suggest: suggestions are searched in that language and priced in that market, and every url they return keeps the address, so a click never drops the shopper back to the default language or market. Put it where your script reads it, for example `data-suggest-url="{{ routes.search_suggest_url }}"` on the dropdown, and append `?q=` to it.

Same-origin, no token, GET only. Suggestions start at the first character: a one- or two-character query returns products, collections, categories, vendors and brands whose title starts with it; from three characters every enabled type is searched in full, with synonyms and a prefix match on the last word. An empty query returns empty groups. While the merchant has storefront search or predictive search off the endpoint answers 404; read the switches below instead of finding out from a 404. Each visitor may send a short burst of requests and about two per second after that; past it the endpoint answers 429 with a Retry-After header in seconds, so debounce input and treat a 429 like an empty response. Search engine, merchant and ads crawlers are never limited.

## When search or suggestions are off

The merchant can turn storefront search and predictive search off (Online Store › Search). Two booleans on `shop` (always in ctx) say which are on: `shop.search_enabled` is false while storefront search is off, and then `routes.search_url` and `routes.search_suggest_url` both answer 404, so hide the search box and every search link. `shop.predictive_search_enabled` is true only while both switches are on; while it is false, keep the box but let it submit to `routes.search_url` without fetching suggestions. Changing either switch refreshes cached pages, so the flags are always current.

```html
{% if shop.search_enabled %}
  <form action="{{ routes.search_url }}" role="search"
        {% if shop.predictive_search_enabled %}data-suggest-url="{{ routes.search_suggest_url }}"{% endif %}>
    <input type="search" name="q" autocomplete="off">
  </form>
{% endif %}

<script>
  // Suggestions only where the form carries the language-aware URL.
  const form = document.querySelector("form[data-suggest-url]");
  if (form) {
    const url = form.dataset.suggestUrl; // e.g. /de/search/suggest on a /de page
    // fetch(url + "?q=" + encodeURIComponent(query)) on debounced input
  }
</script>
```

## Response

```json
{
  "query": "vanta",
  "groups": [
    {
      "type": "product",
      "has_more": true,
      "items": [
        {
          "title": "Vantage Aero 11 Tablet",
          "url": "/products/vantage-aero-11-tablet",
          "image": "https://…/….w160.webp",
          "price": "$1,199.00",
          "compare_at_price": "$1,499.00",
          "vendor": { "name": "Vantage", "url": "/vendors/vantage" },
          "rating": { "average": 4.6, "count": 38 }
        }
      ]
    }
  ],
  "search_url": "/search?q=vanta"
}
```

Groups arrive in the order of the merchant's result types (Online Store › Search; by default product, collection, category, vendor, brand, page, blog_post, blog_category, policy, metaobject, where metaobject is a custom content entry's web page). Each group's titles and urls are in the page's language: the translated title and the translated page address. The product group holds up to 10 items and every other group up to 5, the most relevant first; has_more is true when the group has more results than it shows — link to search_url for them. Groups carry no total: the search page counts results, the dropdown does not. Every url is permalink-correct for the storefront and carries the request's language and country address — render it as-is, never rebuild routes client-side. image is present only when the resource has one: a small cached CDN variant sized for a dropdown row. Product items also carry price, formatted for the visitor's currency and language exactly as product cards show it; compare_at_price when the product is on sale; price_from: true when variants have different prices (show it as "From …"); vendor, the product's vendor name and its page url, when it has one; and rating, the average (one decimal) and count of approved reviews, when it has reviews and reviews are on. search_url is the full results page for the query — use it for a "see all results" row.

## Building the dropdown

Debounce input (200–300ms) and drop out-of-order responses — keep a request sequence number and ignore any response older than the latest. Render titles as text nodes, not injected HTML. Close on Escape and on clicks outside the panel. On small screens let the field take the full header row while open.

> Suggest requests are not recorded in the merchant's search analytics — submitted searches are. Always submit the form (or link to search_url) for a full search rather than swallowing Enter inside the dropdown.

## Search inside a listing

Shop, collection, category, vendor and brand pages take `?q=`: it searches only that listing's products, in the page's language, with the store's synonyms and other forms of each word, and filters, sort (relevance first while q is set) and paging apply on top; page links keep q. `products.search` carries it: `enabled` is true while storefront search is on and products are one of the merchant's result types, `query` is the search applied (empty while `enabled` is false: then `?q=` is ignored and the grid is the whole listing), and `url` is the listing's own address, the action of the form. Hide the listing's box while `enabled` is false. A searched listing is noindex,follow and canonical to the plain listing.

```html
{% if products.search.enabled %}
  <form action="{{ products.search.url }}" method="get" role="search">
    <input type="search" name="q" value="{{ products.search.query }}">
  </form>
{% endif %}
```

## Blog search

The blog list and its category, tag, author and month pages take `?q=` too: it searches only that list's posts, in the page's language, with the store's synonyms; page links keep q. Blog search follows the storefront search switch: while `blog.search.enabled` (the same as `shop.search_enabled`) is false, `?q=` is ignored, the list shows every post and `blog.search.query` is empty, so hide the blog's box. `blog.search.max_length` is the longest query accepted. A GET form without an action submits to the page's own address, so a search on a category page stays in that category. A searched list is noindex,follow and canonical to the plain list.

```html
{% if blog.search.enabled %}
  <form method="get" role="search">
    <input type="search" name="q" value="{{ blog.search.query }}" maxlength="{{ blog.search.max_length }}">
  </form>
{% endif %}
```

