Sub topic 9
Faceted navigation and result counts
This chapter assumes you know the difference between search and filtering, and that you can read basic TypeScript and HTML. It covers how facets work, how to compute their counts correctly, and the errors that make counts untrustworthy.
What a facet is
A facet is a filterable attribute of the items in a result set, such as category, brand, color, or price range. Faceted navigation is the interface that lists each facet with its available values and lets users combine them to narrow results.
A plain filter control only lets the user choose a value. A facet also tells the user what the data contains. Next to each value it shows a count: how many results the user would get if they chose it. Counts do three jobs:
- They preview the outcome, so users avoid choices that lead to a dead end.
- They describe the data. A user who sees "Boots (212), Sandals (9)" learns what the catalog is mostly made of.
- They support decisions without extra clicks, because users can compare options before they commit.
Counts only help when they are accurate. A wrong count is worse than no count, because it makes users distrust the whole interface.
How counts are computed
The count beside a value answers this question: how many items would match if the user selected this value, given everything else they have already selected?
The rule that follows has two parts:
- Within one facet, a count ignores that facet's own selection. This lets the user see the alternatives they could switch to or add.
- Across facets, a count respects every other active filter and the search query.
The helper from the spine counts values over a list of items:
type Item = { id: string; category: string; brand: string };
function facetCounts(items: Item[], field: "category" | "brand"): Map<string, number> {
const counts = new Map<string, number>();
for (const item of items) {
const value = item[field];
counts.set(value, (counts.get(value) ?? 0) + 1);
}
return counts;
}The function is simple. The important decision is which items you pass in. For the brand facet, pass the items that match the query and the category filter, but not the brand filter. For the category facet, pass the items that match the query and the brand filter, but not the category filter.
type Selection = { category: string[]; brand: string[] };
function matches(item: Item, sel: Selection, skip?: keyof Selection): boolean {
if (skip !== "category" && sel.category.length > 0 && !sel.category.includes(item.category)) return false;
if (skip !== "brand" && sel.brand.length > 0 && !sel.brand.includes(item.brand)) return false;
return true;
}
function countsFor(all: Item[], sel: Selection, field: keyof Selection) {
const pool = all.filter((item) => matches(item, sel, field));
return facetCounts(pool, field);
}Here all is assumed to be the items that already match the search query. The skip argument removes one facet's own filter from the pool. In a real app the search engine or database computes this, because you should not send the full catalog to the browser. The logic stays the same.
Multi-select within a facet: OR, and across facets: AND
Most facets behave in one of two ways, and the count behavior depends on which.
| Behavior | Typical facets | Combining rule | What counts show |
|---|---|---|---|
| Multi-select | Brand, color, category | OR within the facet, AND across facets | Count of results if this value were added |
| Single-select | Rating "4 stars and up", price range | One value at a time | Count of results if this value replaced the current one |
Suppose a user selects the brands "Acme" and "Nova". Results include items from either brand. If they then select the category "Boots", results must match Boots and either brand. The category counts reflect the brand selection. The brand counts reflect the category selection but not the brand selection, so the unselected brand "Zeta" shows how many results it would add.
With single-select facets, the displayed counts show what each value alone would give. Selecting a value replaces the current one.
Handling zero counts
When a value has a count of zero, choosing it would produce an empty page. You have three options:
- Hide the value. The list stays short and no one hits a dead end. The cost is that options appear and disappear as users filter, which can feel unstable and hides what exists.
- Show it disabled with a count of 0. Users see that the option exists but is unavailable. The list stays stable. Disabled controls need an accessible explanation, covered in the accessibility chapter.
- Leave it enabled. Users can reach an empty state, which is the worst result for a control that promises a count.
For most catalogs, hide zero-count values in long lists and disable them in short lists such as sizes, where position matters. Never hide a value that the user has already selected, even when its count is zero. Keep it visible so they can remove it.
Exact, approximate, and capped counts
Computing counts for every facet on every request costs time on large result sets. Three strategies exist:
- Exact counts. Correct but can be slow on millions of items. Use them when the set is small or the engine is fast.
- Capped counts. Show "99+" or "1,000+" past a threshold. The engine can stop counting early.
- Approximate counts. Show "about 12,000". The values are fast but must be labeled as estimates, and they must never show a value that reads as exact but is wrong.
Whatever you choose, keep the total at the top of the results consistent with the facet counts. If the page header says "24 results" and the facet values for one facet add up to 31, users will notice. A multi-select facet can legitimately sum to more than the total, since one item can have several values, such as multiple tags. A single-valued attribute like category should add up to the total when no filter on it is active.
Ordering and truncating facet values
Long facets, such as brand with 300 values, need decisions about order and length.
- Order by count, descending, when users mostly want the common options. This is the usual default.
- Order alphabetically when users look for a specific known name, as with brands.
- Use a natural order for ordered data: sizes S, M, L, XL, price ranges from low to high, ratings from high to low.
- Show the top 5 to 10 values and add a "Show more" control. Put selected values first, so they never hide behind the control.
- Add a text field inside the facet for lists beyond roughly 15 values, so users can type "Nik" to find "Nike".
When you order by count, the order changes as other filters change. That is correct, but selected values should keep a stable position at the top so the list does not jump under the pointer.
Range and numeric facets
Price, size, and date do not fit a list of discrete values. Two patterns work:
- Buckets, such as "Under $25", "$25 to $50", "$50 to $100". Each bucket has a count, and buckets are easy on small screens. Choose boundaries from the real distribution. Buckets that hold 90% of the items in one bin tell users nothing.
- A slider or min and max fields. A slider can show a histogram of counts above it. Provide typed inputs as well, since sliders are hard to use precisely and for some users with motor or vision needs.
For range facets, validate input: reject a minimum above the maximum, and treat an empty field as "no limit" rather than zero.
Keeping facets in sync with the URL
Facet selections are state, and the URL should hold them so a filtered view can be shared and restored. Repeat the parameter for multiple values:
type Filters = {
query: string;
categories: string[];
minPrice?: number;
};
function toSearchParams(f: Filters): URLSearchParams {
const params = new URLSearchParams();
if (f.query) params.set("q", f.query);
for (const c of f.categories) params.append("category", c);
if (f.minPrice !== undefined) params.set("min_price", String(f.minPrice));
return params;
}
function fromSearchParams(params: URLSearchParams): Filters {
const min = params.get("min_price");
return {
query: params.get("q") ?? "",
categories: params.getAll("category"),
minPrice: min !== null ? Number(min) : undefined,
};
}A URL such as /search?q=boots&category=hiking&category=winter&min_price=50 fully describes the view. The counts are not stored in the URL. They are recomputed from the selection every time, so they cannot go stale.
Updating counts after a change
When the user toggles a value, the results and every facet's counts must update together. If the results change first and the counts lag, the page contradicts itself for a moment. Common approaches:
- Request results and counts in one response, and render both at once.
- Show a loading indicator on the facet panel while counts refresh, and keep the previous counts visible but dimmed.
- Debounce rapid toggling, then fetch once. Debouncing is covered in the latency chapter.
Announce the change for screen reader users with a status message such as "24 results", as in the search landmark example from the accessibility chapter.
Common errors
- Counts that ignore other filters. The count shows the total for the whole catalog, so selecting it yields a smaller number than promised.
- Counts that include the facet's own filter. In a multi-select facet, unselected values then show 0, which prevents users from adding alternatives.
- Counts that ignore the search query. "Boots (212)" appears after a search that matches only 14 boots.
- Case and spelling variants as separate values. "nike", "Nike", and "NIKE" appear three times. Normalize values at indexing time.
- Too many facets. Showing 15 facets overwhelms users. Show the few that matter for the category, and collapse the rest.
- Layout shift. Values that appear and disappear move the page. Reserve space or keep values stable.
Choosing facets for a data set
Pick facets from attributes that users actually use to decide. Ask these questions about each candidate:
- Does every item have a value for it? Facets with many missing values need an "Unknown" option or should be dropped.
- Does it have a manageable number of distinct values, or a natural range?
- Do users make decisions based on it? Use search logs and support questions to check.
- Does it split the results into meaningfully different groups?
Exercise
A user searches for "jacket" and selects the brands "Acme" and "Nova". The category facet shows Rain (8), Winter (5), Leather (2). The brand facet shows Acme (9), Nova (6), Zeta (4). Which items were used to compute the brand counts, and which were used for the category counts?
Show answer
The category counts use items matching "jacket" and the brands Acme or Nova, because category must respect every other facet's filter. The brand counts use items matching "jacket" and any category filter, but not the brand selection, so Zeta shows how many items it would add. Without the skip, Zeta would show 0.
Exercise
A facet value "Waterproof" has a count of 0 after the user applies other filters, and the user had already selected it. Should you hide it? Explain.
Show answer
No. Selected values must stay visible so the user can see and remove every active filter. Keep it in the list, marked as selected with its count of 0, and pair the facet with an applied-filters summary that offers removal.