feat: LinkedIn profile search engine (Django + React + Elasticsearch + ELK)

Full-stack implementation of the Cyberyan full-stack trial task.

Backend (Django 5 / DRF)
- Normalised PostgreSQL model (Profile, Company, School, Skill, Experience,
  Education) as the source of truth, with GIN + trigram indexes.
- Self-healing CSV reader: the supplied export carries an injected watermark
  column, missing columns, rows split by broken quoting and a duplicated
  header. A signature-scored Needleman-Wunsch alignment reconstructs 284 clean
  records out of 336 physical rows; only 10 rows parse correctly without it.
- Pluggable search layer (Strategy): Elasticsearch by default, PostgreSQL
  full-text search as a fallback selected by a single environment variable.
- Keyword search with per-field boosts, fuzziness and phrase boosting, six
  faceted filters (OR within a filter, AND across filters), an experience
  range, four sort orders, highlighting and facet aggregations.
- JWT auth with rotation + blacklist, three roles, PII masking, scoped rate
  limits, a single error envelope, OpenAPI schema and health probes.

Frontend (React 18 / TypeScript / Vite)
- Debounced search with type-ahead, faceted filter panel, result cards,
  profile drawer, URL-synced search state, automatic token refresh.
- Highlight snippets rendered without dangerouslySetInnerHTML.

Infrastructure
- Hardened multi-stage images (non-root, read-only rootfs, healthchecks).
- docker-compose for the full stack plus dev and production overlays.
- ELK: structured JSON logs shipped asynchronously to Logstash, routed per
  event type, with a composable index template and a Kibana setup script.

Quality
- 61 backend tests (85% coverage), ruff/black/bandit/mypy, ESLint + tsc,
  GitHub Actions running lint, tests and both image builds.
- Persian documentation: README plus architecture, search, API, data,
  security and operations guides.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GnFo6fNaHCTAkJbfcdkqtG
This commit is contained in:
2026-09-03 08:13:10 +00:00
commit a464e550c9
133 changed files with 17019 additions and 0 deletions
+120
View File
@@ -0,0 +1,120 @@
"""Search endpoints."""
from __future__ import annotations
import logging
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.core.permissions import IsAdminRole
from apps.core.throttling import SearchRateThrottle
from apps.search.backends import get_search_backend
from apps.search.serializers import (
SearchRequestSerializer,
SearchResponseSerializer,
SuggestResponseSerializer,
)
audit = logging.getLogger("audit.search")
@extend_schema(
tags=["search"],
parameters=[SearchRequestSerializer],
responses={200: SearchResponseSerializer},
summary="Keyword search with filters and facets",
description=(
"Ranked keyword search across name, job title, skills, employer, education, "
"location and summary, combined with structured filters. Filter values are "
"OR-ed inside one filter and AND-ed across filters."
),
)
class SearchView(APIView):
"""
``GET /api/v1/search/``
Thin by design: validate the query string, hand a normalised request to the
configured backend, shape the response, emit an audit event. All ranking
lives in the backend, all validation lives in the serializer.
"""
permission_classes = [IsAuthenticated]
throttle_classes = [SearchRateThrottle]
serializer_class = SearchRequestSerializer
def get(self, request: Request) -> Response:
serializer = SearchRequestSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
search_request = serializer.to_search_request()
backend = get_search_backend()
result = backend.search(search_request)
audit.info(
"search_executed",
extra={
"event": "search_executed",
"user_id": request.user.id,
"backend": result.backend,
"result_count": result.total,
"took_ms": result.took_ms,
**search_request.as_log_context(),
},
)
return Response(SearchResponseSerializer.from_result(result, search_request))
@extend_schema(
tags=["search"],
parameters=[
OpenApiParameter("q", str, description="Partial name or skill.", required=True),
OpenApiParameter("limit", int, description="Maximum suggestions (1-20)."),
],
responses={200: SuggestResponseSerializer},
summary="Type-ahead suggestions",
)
class SuggestView(APIView):
"""Edge n-gram completions used by the search box."""
permission_classes = [IsAuthenticated]
throttle_classes = [SearchRateThrottle]
def get(self, request: Request) -> Response:
prefix = request.query_params.get("q", "").strip()[:64]
try:
limit = min(max(int(request.query_params.get("limit", 8)), 1), 20)
except ValueError:
limit = 8
suggestions = get_search_backend().suggest(prefix, limit=limit) if prefix else []
return Response({"suggestions": suggestions})
@extend_schema(
tags=["search"],
request=None,
responses={202: dict},
summary="Rebuild the search index (admin only)",
)
class ReindexView(APIView):
"""
Triggers a zero-downtime rebuild of the Elasticsearch index.
Restricted to the admin role: it is an expensive, cluster-wide operation.
"""
permission_classes = [IsAuthenticated, IsAdminRole]
def post(self, request: Request) -> Response:
from apps.search.indexing import rebuild_index
result = rebuild_index()
audit.info(
"index_rebuilt",
extra={"event": "index_rebuilt", "user_id": request.user.id, **result},
)
return Response(result, status=status.HTTP_202_ACCEPTED)