WIP add search API to Django

This commit is contained in:
Daoud Clarke 2023-09-24 08:09:18 +01:00
parent 177324353f
commit 86a6524f0a
3 changed files with 29 additions and 3 deletions

View file

@ -1,7 +1,27 @@
from pathlib import Path
from ninja import NinjaAPI
from app import settings
from mwmbl.indexer.paths import INDEX_NAME
from mwmbl.tinysearchengine import search
from mwmbl.tinysearchengine.completer import Completer
from mwmbl.tinysearchengine.indexer import TinyIndex, Document
from mwmbl.tinysearchengine.rank import HeuristicRanker
api = NinjaAPI(version="1.0.0")
index_path = Path(settings.DATA_PATH) / INDEX_NAME
tiny_index = TinyIndex(item_factory=Document, index_path=index_path)
tiny_index.__enter__()
completer = Completer()
ranker = HeuristicRanker(tiny_index, completer)
search_router = search.create_router(ranker)
api.add_router("/search/", search_router)
@api.get("/hello")
def hello(request):

View file

@ -121,3 +121,9 @@ STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# ===================== Custom Settings =========================
DATA_PATH = "./devdata"

View file

@ -1,6 +1,6 @@
from logging import getLogger
from fastapi import APIRouter
from ninja import Router
from mwmbl.tinysearchengine.rank import HeuristicRanker
@ -10,8 +10,8 @@ logger = getLogger(__name__)
SCORE_THRESHOLD = 0.25
def create_router(ranker: HeuristicRanker) -> APIRouter:
router = APIRouter(prefix="/search", tags=["search"])
def create_router(ranker: HeuristicRanker) -> Router:
router = Router(tags=["search"])
@router.get("")
def search(s: str):