From 0b120a13305491d9fb6b0c7c28d4857844a31fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Fri, 22 Sep 2023 16:55:33 +0200 Subject: [PATCH] feat: add real pagination to search API --- app/api.py | 7 ++++--- app/query.py | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/api.py b/app/api.py index 2182cfa1..308c93e9 100644 --- a/app/api.py +++ b/app/api.py @@ -78,9 +78,10 @@ def search( If not provided, `['en']` is used.""" ), ] = None, - num_results: Annotated[ - int, Query(description="The number of results to return") + page_size: Annotated[ + int, Query(description="Number of results to return per page.") ] = 10, + page: Annotated[int, Query(ge=1, description="Page to request, starts at 1.")] = 1, fields: Annotated[ str | None, Query( @@ -102,7 +103,7 @@ def search( langs = set(langs or ["en"]) query = build_search_query( - q=q, langs=langs, num_results=num_results, config=CONFIG, sort_by=sort_by + q=q, langs=langs, size=page_size, page=page, config=CONFIG, sort_by=sort_by ) results = query.execute() diff --git a/app/query.py b/app/query.py index 676b1b1b..2c5b41fb 100644 --- a/app/query.py +++ b/app/query.py @@ -144,7 +144,8 @@ def parse_sort_by_parameter(sort_by: str | None, config: Config) -> str | None: def build_search_query( q: str, langs: set[str], - num_results: int, + size: int, + page: int, config: Config, sort_by: str | None = None, ) -> Query: @@ -165,6 +166,7 @@ def build_search_query( query = query.sort(sort_by) query = query.extra( - size=num_results, + size=size, + from_=size * (page - 1), ) return query