Production-style e-commerce faceted search solution with:
- NestJS REST API + Elasticsearch 8.x
- Next.js (App Router) React frontend with Chakra UI
- Backend: NestJS 10 + TypeScript strict mode
- Search engine: Elasticsearch 8.x (
@elastic/elasticsearch8.x) - Frontend: Next.js 16 (App Router) + React 19 + Chakra UI
- Docker Compose for local development
This implementation follows the requested structure:
src/productsfor controllers, service, DTOs, and query/aggregation helperssrc/elasticsearchfor client module + index mapping/analyzerssrc/scripts/seed-data.tsfor generating and indexing 100,000+ documentstest/products.e2e-spec.tsfor endpoint smoke testpostman/collection.jsonfor ready-to-use API callsfrontend/for React app (Next.js App Router + Chakra UI)
- Start Elasticsearch, API, and frontend with Docker Compose:
docker-compose up -dNote: if your network blocks docker.elastic.co (common on restricted corporate/VPN networks), this project uses the Docker Hub fallback image elasticsearch:8.15.3 in docker-compose.yml.
Services:
- API:
http://localhost:3000 - Frontend:
http://localhost:3001 - Elasticsearch:
http://localhost:9200
- Or run backend and frontend directly on host:
# backend
npm install
npm run start:dev
# frontend (new terminal)
cd frontend
npm install
npm run dev- Seed data (backend):
npm run seedOptional:
SEED_COUNT=200000 npm run seedThe frontend app is located in frontend/ and uses:
- Next.js App Router (
src/app) - Chakra UI provider wired in
src/components/ui/provider.tsx
Useful commands:
cd frontend
npm run dev
npm run build
npm run startProduction Docker image:
- Multi-stage Dockerfile is available at
frontend/Dockerfile - It runs
next buildin a builder stage andnext startin a runtime stage
Swagger UI is available at:
http://localhost:3000/docs
POST /products/searchGET /products/suggest?q=POST /products/bulk-indexGET /healthGET /docs(Swagger UI)
attributesis mapped asnestedto preserve(name, value)tuple semantics.brand,category.id,tagsusekeywordfor exact filtering/aggregation.title/descriptionuse custom analyzer (lowercase,asciifolding,synonym_graph) for typo/synonym-friendly full-text matching.suggestusescompletionwith edge-ngram style analyzer behavior for autocomplete.
The API avoids the classic facet collapse issue by splitting concerns:
querycontains only full-text relevance logic.post_filterapplies all user-selected filters to hits, so returned products respect all selected facets.- facet aggregations are isolated per facet using
filteraggs:- brands aggregation excludes brand filter
- categories aggregation excludes category filter
- price aggregation excludes price range filter
- ratings aggregation excludes rating filter
This keeps each facet useful even while selected.
For dynamic attributes:
- Main nested aggregation (
attributes_scope) computes available attribute names/values under non-attribute filters. - Additional selected-attribute-scoped aggs exclude only the current attribute name filter to keep value counts meaningful when one value is selected.
Implemented optimizations:
- Filter clauses are built in filter context (non-scoring, cache-friendly).
request_cache: trueis enabled for search requests.- Minimal
_sourcefor search hit payload. - Bulk indexing in batches (
2000) to improve throughput.
To capture benchmark screenshots:
- Seed 100k+ docs (
npm run seed) - Run repeated search requests with representative filters
- Capture
meta.took_msfrom response and ES profiling/monitoring screenshots
curl -X POST http://localhost:3000/products/search \
-H "Content-Type: application/json" \
-d '{
"query":"laptop gaming",
"filters":{
"brands":["Dell","HP"],
"attributes":[{"name":"RAM","values":["16GB"]}],
"inStock":true
},
"pagination":{"page":1,"size":20}
}'curl "http://localhost:3000/products/suggest?q=dell"curl http://localhost:3000/healthnpm run test:e2e- Kept Elasticsearch integration in dedicated module/service for separation of concerns.
- Encapsulated complex DSL building in helpers (
query-builder,aggregation-builder) to keep controller/service maintainable. - Used strict DTO validation at API boundary for predictable request shape and safer filtering logic.