๐Ÿ” Thmanyah Discovery API

Public search and discovery API for finding and exploring media content (podcasts, documentaries, etc.) in the Thmanyah platform.

๐ŸŽฏ Purpose

The Discovery API serves as the public-facing search interface that allows users to:

  • Search for programs using full-text search
  • Filter and sort search results
  • Discover new content through recommendations
  • Access program metadata and details
  • Browse content with pagination support

๐Ÿ—๏ธ Architecture

This service is part of the Thmanyah microservices architecture:

Example :
Discovery API โ†’ Elasticsearch โ†’ Search Results

The Discovery API is read-only and serves content that has been indexed by the Sync Worker from the CMS API.

๐Ÿ› ๏ธ Tech Stack

  • Framework: NestJS + TypeScript
  • Search Engine: Elasticsearch
  • Validation: Class-validator + Class-transformer
  • Documentation: Swagger/OpenAPI
  • Testing: Jest + Supertest

๐Ÿ“‹ Features

Search Capabilities

  • โœ… Full-text search across program content
  • โœ… Advanced filtering (category, duration, language, etc.)
  • โœ… Sorting options (relevance, date, title, etc.)
  • โœ… Pagination and result limiting
  • โœ… Search suggestions and autocomplete
  • โœ… Faceted search results

API Endpoints

Search

  • GET /search - Search programs with filters
  • GET /search/suggest - Get search suggestions
  • GET /search/facets - Get available search facets

Programs

  • GET /programs/:id - Get program details by ID
  • GET /programs - List programs with pagination

Health

  • GET /health - Service health check

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+
  • Elasticsearch instance
  • Environment variables configured

Development

Example :
# Install dependencies (from root)
pnpm install

# Start in development mode
pnpm --filter discovery-api dev

# Or from the app directory
cd apps/discovery-api
pnpm dev

Environment Variables

Required environment variables (see root .env.example):

Example :
# Elasticsearch
ELASTICSEARCH_URL=http://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=changeme
ELASTICSEARCH_INDEX_NAME=programs

# API Configuration
DISCOVERY_API_PORT=3002
NODE_ENV=development

# Optional: CORS
CORS_ORIGINS=http://localhost:3000

๐Ÿ“š API Documentation

Once running, access the Swagger documentation at:

http://localhost:3002/api

๐Ÿงช Testing

Example :
# Unit tests
pnpm --filter discovery-api test

# E2E tests
pnpm --filter discovery-api test:e2e

# Test coverage
pnpm --filter discovery-api test:cov

# Watch mode
pnpm --filter discovery-api test:watch

๐Ÿ”ง Development

Available Scripts

Example :
# Development
pnpm dev                    # Start in development mode
pnpm start                  # Start in production mode
pnpm start:dev              # Start with watch mode
pnpm start:debug            # Start in debug mode
pnpm start:prod             # Start in production mode

# Testing
pnpm test                   # Run unit tests
pnpm test:watch             # Run tests in watch mode
pnpm test:cov               # Run tests with coverage
pnpm test:debug             # Run tests in debug mode
pnpm test:e2e               # Run e2e tests

# Building
pnpm build                  # Build the application
pnpm build:watch            # Build in watch mode

# Linting
pnpm lint                   # Run ESLint
pnpm lint:fix               # Fix ESLint issues

Project Structure

Example :
apps/discovery-api/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app.module.ts           # Main application module
โ”‚   โ”œโ”€โ”€ main.ts                 # Application entry point
โ”‚   โ”œโ”€โ”€ common/                 # Shared utilities
โ”‚   โ”‚   โ””โ”€โ”€ filters/            # Exception filters
โ”‚   โ”œโ”€โ”€ health/                 # Health check endpoints
โ”‚   โ””โ”€โ”€ search/                 # Search functionality
โ”‚       โ”œโ”€โ”€ search.controller.ts
โ”‚       โ”œโ”€โ”€ search.service.ts
โ”‚       โ”œโ”€โ”€ search.module.ts
โ”‚       โ”œโ”€โ”€ dto/                # Data transfer objects
โ”‚       โ”œโ”€โ”€ services/           # Business logic services
โ”‚       โ””โ”€โ”€ utils/              # Utility functions
โ”œโ”€โ”€ test/                       # E2E tests
โ””โ”€โ”€ package.json

๐Ÿ”„ Data Flow

  1. Content Indexing: Sync Worker indexes programs from database to Elasticsearch
  2. Search Requests: Users query Discovery API with search parameters
  3. Elasticsearch Query: API translates requests to Elasticsearch queries
  4. Result Processing: Results are transformed and returned to users
  5. Caching: Frequently accessed data may be cached for performance

๐Ÿ” Search Features

Query Types

  • Full-text search: Search across title, description, and content
  • Exact match: Search for specific terms or phrases
  • Fuzzy search: Handle typos and variations
  • Wildcard search: Use * and ? for pattern matching

Filters

  • Type: Filter by program type
  • Language: Filter by program language
  • Tags: Filter by tags
  • Date range: Filter by creation/update dates

Sorting

  • Relevance: Default sorting by search relevance
  • Date: Sort by creation or update date
  • Title: Sort alphabetically by title

๐Ÿ”’ Security

  • Input validation and sanitization
  • Rate limiting to prevent abuse
  • CORS configuration for frontend access
  • Request/response logging
  • Error handling with proper HTTP status codes

๐Ÿ“Š Monitoring

  • Health check endpoint: GET /health
  • Elasticsearch connection monitoring
  • Search performance metrics
  • Request/response logging via interceptors
  • Error tracking and reporting

๐Ÿš€ Deployment

Docker

Example :
# Build image
docker build --build-arg TARGET_APP=discovery-api -t thmanyah/discovery-api .

# Run container
docker run -p 3002:3002 --env-file .env thmanyah/discovery-api

Production

Example :
# Build for production
pnpm --filter discovery-api build

# Start production server
pnpm --filter discovery-api start:prod

๐Ÿ”ง Configuration

Elasticsearch Index

The service manages an Elasticsearch index with the following mapping:

Example :
{
  "mappings": {
    "properties": {
      "id": { "type": "keyword" },
      "title": {
        "type": "text",
        "analyzer": "standard",
        "fields": { "keyword": { "type": "keyword" } }
      },
      "description": { "type": "text", "analyzer": "standard" },
      "type": { "type": "keyword" },
      "language": { "type": "keyword" },
      "tags": { "type": "keyword" },
      "createdAt": { "type": "date" },
      "updatedAt": { "type": "date" }
    }
  },
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }
}

Performance Tuning

  • Configure Elasticsearch for optimal search performance
  • Implement caching for frequently accessed data
  • Use connection pooling for Elasticsearch
  • Monitor and optimize query performance

๐Ÿค Contributing

  1. Follow the project's coding standards
  2. Add tests for new search features
  3. Update API documentation
  4. Ensure all tests pass before submitting
  5. Consider search performance implications

๐Ÿ“„ License

This project is part of the Thmanyah backend system and is licensed under the MIT License.

results matching ""

    No results matching ""