Skip to content

About

Elasticsearch Faceted Search Engine

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Elasticsearch Faceted Search API + Frontend

Production-style e-commerce faceted search solution with:

  • NestJS REST API + Elasticsearch 8.x
  • Next.js (App Router) React frontend with Chakra UI

Stack

  • Backend: NestJS 10 + TypeScript strict mode
  • Search engine: Elasticsearch 8.x (@elastic/elasticsearch 8.x)
  • Frontend: Next.js 16 (App Router) + React 19 + Chakra UI
  • Docker Compose for local development

Project Structure

This implementation follows the requested structure:

  • src/products for controllers, service, DTOs, and query/aggregation helpers
  • src/elasticsearch for client module + index mapping/analyzers
  • src/scripts/seed-data.ts for generating and indexing 100,000+ documents
  • test/products.e2e-spec.ts for endpoint smoke test
  • postman/collection.json for ready-to-use API calls
  • frontend/ for React app (Next.js App Router + Chakra UI)

Run Locally

  1. Start Elasticsearch, API, and frontend with Docker Compose:
docker-compose up -d

Note: 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
  1. 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
  1. Seed data (backend):
npm run seed

Optional:

SEED_COUNT=200000 npm run seed

Frontend

The 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 start

Production Docker image:

  • Multi-stage Dockerfile is available at frontend/Dockerfile
  • It runs next build in a builder stage and next start in a runtime stage

API Documentation

Swagger UI is available at:

  • http://localhost:3000/docs

Endpoints

  • POST /products/search
  • GET /products/suggest?q=
  • POST /products/bulk-index
  • GET /health
  • GET /docs (Swagger UI)

Mapping Design Rationale

  • attributes is mapped as nested to preserve (name, value) tuple semantics.
  • brand, category.id, tags use keyword for exact filtering/aggregation.
  • title/description use custom analyzer (lowercase, asciifolding, synonym_graph) for typo/synonym-friendly full-text matching.
  • suggest uses completion with edge-ngram style analyzer behavior for autocomplete.

Post-Filter / Facet Isolation Solution

The API avoids the classic facet collapse issue by splitting concerns:

  • query contains only full-text relevance logic.
  • post_filter applies all user-selected filters to hits, so returned products respect all selected facets.
  • facet aggregations are isolated per facet using filter aggs:
    • 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.

Search Performance Notes

Implemented optimizations:

  • Filter clauses are built in filter context (non-scoring, cache-friendly).
  • request_cache: true is enabled for search requests.
  • Minimal _source for search hit payload.
  • Bulk indexing in batches (2000) to improve throughput.

Benchmark guidance

To capture benchmark screenshots:

  1. Seed 100k+ docs (npm run seed)
  2. Run repeated search requests with representative filters
  3. Capture meta.took_ms from response and ES profiling/monitoring screenshots

API Examples

Search

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}
  }'

Suggest

curl "http://localhost:3000/products/suggest?q=dell"

Health

curl http://localhost:3000/health

Tests

npm run test:e2e

Architecture Decisions

  • 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.

About

Elasticsearch Faceted Search Engine

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages