Files
cyberyan_linkedin_search/backend/apps/search/backends/__init__.py
T

37 lines
1.0 KiB
Python
Raw Normal View History

"""Backend factory - resolves ``SEARCH["BACKEND"]`` to a concrete strategy."""
from __future__ import annotations
from functools import cache
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from apps.search.backends.base import SearchBackend
__all__ = ["SearchBackend", "get_search_backend"]
@cache
def get_search_backend(name: str | None = None) -> SearchBackend:
"""
Return the configured backend.
Cached because both implementations hold a connection pool that should be
created once per process, not once per request.
"""
backend = (name or settings.SEARCH["BACKEND"]).lower()
if backend == "elasticsearch":
from apps.search.backends.elastic import ElasticsearchBackend
return ElasticsearchBackend()
if backend == "postgres":
from apps.search.backends.postgres import PostgresBackend
return PostgresBackend()
raise ImproperlyConfigured(
f"Unknown SEARCH['BACKEND'] {backend!r}; expected 'elasticsearch' or 'postgres'."
)