Prompt
How do I set up faceted search for products in my application?
Latest observation
Faceted search lets users filter products by multiple attributes like brand, price, color, size, rating, etc. A good setup usually has 4 parts:
1) Define your searchable product data
Make sure each product has structured fields you can filter on.
Example:
{
"id": 123,
"name": "Running Shoe",
"category": "Shoes",
"brand": "Nike",
"color": ["Black", "White"],
"size": [9, 10, 11],
"price": 89.99,
"rating": 4.5,
"inStock": true
}
2) Pick a search/filter backend
Common options:
- Elasticsearch / OpenSearch: best for large catalogs and rich faceting
- Algolia: very easy to implement, hosted solution
- Database + custom filtering: fine for smaller apps
- Meilisearch: simpler full-text + filtering search engine
If you need fast faceted search at scale, Elasticsearch/OpenSearch is a common choice.
3) Index fields properly
Facets work best when fields are indexed as filterable aggregations.
Typical rules:
- Use keyword/exact fields for things like brand, category, color
- Use numeric fields for price and ratings
- Use boolean fields for availability
- Use multi-valued fields for arrays like colors or sizes
Example in Elasticsearch-like terms:
brand.keywordcategory.keywordpriceratinginStock
4) Build the search query with filters + facet counts
Faceted search usually requires:
- a search term
- selected filters
- facet counts for the remaining available options
Example user flow:
- Search:
"running" - Filters: brand = Nike, color = Black
- Show counts for other facets like sizes available in the filtered result set
Example query idea
You generally:
- Apply selected filters to narrow results
- Ask the search engine to compute facet counts on the filtered dataset
In Elasticsearch, this is often done with:
boolquery for filtersaggregationsfor facets
5) Render filters in the UI
Common facet types:
- Checkbox lists: brand, color, category
- Range sliders: price
- Star filters: rating
- Toggle switches: in stock only
Good UI practices:
- Show counts next to each facet option
- Disable or hide unavailable options
- Allow multiple selections where it makes sense
- Keep applied filters visible and removable
6) Support URL-driven filters
Store selected filters in the query string so users can share/bookmark results.
Example:
/products?query=running&brand=Nike&color=Black&minPrice=50&maxPrice=150
7) Keep facets fast and relevant
For performance:
- Precompute or index facet-friendly fields
- Cache common queries
- Limit the number of facet values returned
- Use pagination for result lists, not facets
- Consider “top N” facet values plus “show more”
8) Common implementation pattern
A typical backend response looks like this:
{
"results": [
{ "id": 123, "name": "Running Shoe", "brand": "Nike", "price": 89.99 }
],
"facets": {
"brand": [
{ "value": "Nike", "count": 42 },
{ "value": "Adidas", "count": 31 }
],
"color": [
{ "value": "Black", "count": 25 },
{ "value": "White", "count": 18 }
],
"price": {
"min": 20,
"max": 200
}
}
}
9) Watch out for these common pitfalls
- Facets based on unstructured text fields
- Counting facets before applying selected filters incorrectly
- Slow queries on large datasets
- Too many facet values overwhelming the UI
- Not normalizing values like
“blue”,“Blue”,“BLUE”
If you want, I can also show:
- a database-only implementation,
- an Elasticsearch/OpenSearch example, or
- a React + API design for faceted search.