Faceted Search APIs: Dynamic, Category-Aware, and Scalable (on Elasticsearch)
This is Part 5 of the "Building a Scalable, Faceted Online Marketplace" series. Read the Introduction here.
Why Faceted Search APIs?
Faceted search is the backbone of modern e-commerce discovery. Users expect to filter by brand, price, color, rating, and more—dynamically, and with blazing speed. To deliver this at scale, we need robust, category-aware APIs powered by Elasticsearch.
What You'll Learn
- How to design dynamic, category-driven facet APIs
- Querying Elasticsearch for both products and facet counts
- Handling dynamic attributes and nested fields
- Real-world performance tips for large datasets
Key Features
- Dynamic Facets: APIs adapt to category and available data
- Scalable Aggregations: Uses Elasticsearch's aggregation framework
- Flexible Filtering: Supports multi-select, range, and nested filters
- Consistent UX: Backend and frontend always in sync
Example API Endpoints
/api/products?category=Shoes&brand=Nike&color=Red
/api/facets?category=Shoes
Sample Elasticsearch Query
{
"query": { ... },
"aggs": {
"brand": { "terms": { "field": "brand.keyword" } },
"color": { "terms": { "field": "attributes.color.keyword" } },
"price": { "range": { "field": "price", "ranges": [ ... ] } }
}
}
Real-World Tips
- Use keyword fields for facet values
- Cache facet queries for popular categories
- Limit aggregation size for performance
- Sync facet definitions between backend and frontend
Next up: Zero Downtime Reindexing & Unblocking in Elasticsearch
In the next article, we'll cover how to safely reindex and unblock Elasticsearch indices in production, with zero downtime!