Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 40 ยท Search and Merchandising

Product Search, Filters and Facets

Learn how shop search works: indexing products, relevance, typo tolerance, synonyms, facets with counts, sorting, zero-result handling and when SQL is enough versus a search engine.

Browsing menus works while a shop has 50 products. With 5,000 products, many visitors skip the menu and type what they want into the search box, and they tend to buy more than people who only browse. Search is also unforgiving: a visitor who types "sneekers" and sees nothing will not wonder about spelling, they will assume you do not sell sneakers. Good search and filtering are therefore among the highest-value features of a shop.

This lesson explains the moving parts: what goes into an index, how results are ranked, what a facet is and how counts are computed, how to handle typos, synonyms and empty results, and how to decide between plain SQL and a dedicated search engine. The product data comes from the catalog described in Product Catalogs and Data Modeling, including variants from Variants and Options.

Search, filter, sort: three different jobs

  • Search finds products that match words the visitor typed and ranks them by relevance.
  • Filtering narrows a list by exact attributes: color is red, price under $100, in stock.
  • Facets are the filter choices shown in the sidebar, each with a count of how many products would remain: "Red (12), Blue (7)".
  • Sorting orders the result: relevance, price, newest, best selling.

They combine: a visitor searches "jacket", filters to size M and price under $150, and sorts by price. The engine must answer all of that quickly, usually in well under a hundred milliseconds, because many shops update the results as soon as a filter is clicked.

What goes into the search index

A search index is a data structure built for fast lookup of words. You do not search your normal tables: you copy a flattened, search-friendly document per product into the index and keep it up to date.

JSON
{
  "id": "prod_88",
  "title": "Trail Running Jacket",
  "description": "Light, water-resistant jacket for running in wind and rain.",
  "brand": "Northpeak",
  "category": ["Clothing", "Jackets"],
  "colors": ["red", "black"],
  "sizes": ["S", "M", "L"],
  "price": 12900,
  "in_stock": true,
  "rating": 4.6,
  "sales_30d": 214
}

Notice the choices. Variant attributes are lifted to the product document as lists so "red" matches the product, even if only some variants are red. Prices are integers. Business signals such as recent sales and rating are included, because ranking uses them. Do not index private data (cost price, supplier), and handle hidden or draft products by never indexing them, or by filtering them out. After a product changes, update its document within seconds; a nightly full rebuild alone makes stock and price go stale.

flowchart LR
    A[("Catalog database")] -->|"on change + nightly rebuild"| B["Indexer"]
    B --> C[("Search index")]
    D["Visitor query + filters"] --> E["Search API"]
    E --> C
    C --> F["Hits + facet counts"]
    F --> G["Results page"]

The database stays the source of truth; the index is a rebuildable copy built for reading.

Relevance: which result comes first

An engine usually scores each matching document, then sorts by score. Typical ingredients:

  • Where the match is. A word in the title counts more than one in the description; a brand or SKU match counts a lot.
  • How well it matches. Exact phrase beats separate words; exact word beats prefix; all query words beat some of them.
  • Word rarity. A rare word such as "gore-tex" says more than "jacket" (this idea is called TF-IDF, and BM25 is its modern relative).
  • Business signals. Popularity, rating, margin, and in-stock status. Out-of-stock products are usually pushed down rather than hidden.
  • Normalization. Lowercasing, removing accents ("cafe" matches "cafรฉ"), and stemming so "running" and "runs" match "run".

Always look at real queries. Teams review the top searches and the searches with no clicks, then adjust field weights and add rules. For ranking by business logic see Merchandising and Recommendations.

Typos, synonyms and zero results

Typo tolerance allows a small edit distance between the query word and an indexed word. A common rule: no typos for words under 4 letters, one typo for 5-8 letters, two for longer ones. Tolerance should rank exact matches above corrected ones, and should be gentler on short words, where one typo can turn "tea" into "sea".

Synonyms map different words to the same meaning: "sofa" and "couch", "hoodie" and "sweatshirt", "tv" and "television". Some are two-way, some one-way ("laptop" should find "notebook computer", but a search for "notebook" should not necessarily return every laptop). Regional vocabulary matters too, such as "sneakers" and "trainers".

Zero results should be rare and never a dead end. Useful fallbacks:

  1. Retry with typo correction or fewer words (drop the least important word).
  2. Show "Did you mean ...?" or the closest category.
  3. Show popular products or bestsellers.
  4. Log the query. A list of failed searches is a free to-do list: they reveal missing products, missing synonyms and wrong data.

Computing facets and counts

The tricky part of facets is what the counts mean. In a typical shop, when the visitor has chosen "Color: red" and looks at the Brand facet, the counts show how many red products each brand has. But for the Color facet itself, counts should be computed with every filter applied except Color, so the visitor can still see "Blue (7)" and add it as a second choice (an OR within the same facet). Filters across different facets combine with AND.

This "disjunctive faceting" is what makes a filter sidebar feel right. It is also the reason facets are expensive to build by hand on a big catalog, and a main reason to use a search engine. The counter below does the logic on a small product list. Search matching is deliberately simple; the goal is to see how the counts are derived.

JS A facet counter with disjunctive counts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the second run. With "color = red" selected, the hits list has only the red jackets, yet the Color facet still shows blue and green with counts, because its counts ignore the color filter. The Brand and Size counts, however, reflect the red-only list. That is exactly what a visitor expects.

Sorting, pagination and performance

Sorting by a number such as price is cheap; sorting by relevance is the default for text queries and "newest" or "best selling" for category pages. Keep sort rules deterministic: add a tie-breaker such as the product id, otherwise products can swap places between page 1 and page 2. Deep pagination (page 400) is expensive in most engines, so cap it and show filters instead. Return only the fields you need and let a CDN cache popular, session-less queries.

Reflect filters and the query in the URL (/search?q=jacket&color=red), so results can be shared and the back button works. For SEO, decide which filtered pages may be indexed and which need a canonical URL; that topic is in SEO for Online Shops.

SQL or a search engine?

SQL (LIKE, full-text index) Search engine (Meilisearch, Elasticsearch, Typesense, Algolia)
Setup None, uses your database Another service to run and sync
Typo tolerance Not built in Built in
Relevance tuning Limited Rich: weights, synonyms, rules
Facet counts Slow on large tables (many GROUP BY queries) Native and fast
Speed on large catalogs Degrades Designed for it
Consistency Always current Slightly delayed copy

A leading wildcard such as LIKE '%jacket%' cannot use a normal index and scans the whole table. For a few hundred products, SQL with a full-text index is perfectly fine. Beyond a few thousand products, or as soon as you need typo tolerance and facets, a search engine pays for itself. Either way, put the search behind one function in your code so you can switch later. Platforms often include search, and large catalogs are the point where Architecture, Scaling and Peak Traffic recommends offloading search from the main database.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a facet in product search?

    Facets are the filter choices in a sidebar, such as Color: Red (12), computed from the current result set.
  2. 2. A visitor selected Color = red. Why should the Color facet still show 'Blue (7)'?

    Disjunctive faceting ignores a facet's own filter when counting, which lets people choose several values within one facet (OR) while other facets still combine with AND.
  3. 3. Why is LIKE '%jacket%' a poor fit for a big catalog?

    Without an anchored prefix the database must examine every row. Full-text indexes or a search engine are built for this.
  4. 4. What is a good first response to a search with zero results?

    Zero-result pages lose sales. Fallbacks keep the visitor moving and the log tells you which products, synonyms or data are missing.
  5. 5. Why add a tie-breaker, such as the product id, to every sort?

    Without a deterministic tie-break, items with the same sort value can appear on two pages or on none while the visitor pages through results.

Summary

  • Search finds and ranks by relevance, filters narrow by exact attributes, facets show filter choices with counts, and sorting orders the result.
  • An index holds flattened product documents, kept in sync with the catalog; the database stays the source of truth.
  • Relevance mixes field weights, match quality, word rarity and business signals; review real queries regularly.
  • Typo tolerance, synonyms and smart zero-result fallbacks turn failed searches into sales.
  • Facet counts are computed per facet while ignoring that facet's own filter (OR inside a facet, AND between facets).
  • Use SQL for small catalogs and a search engine when you need typo tolerance, facets and speed at scale.

Next: Merchandising and Recommendations covers how to decide what to show first and what to suggest next.