Skip to content

Commit

Permalink
feat: add real pagination to search API
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Sep 22, 2023
1 parent 2f7fa16 commit 0b120a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()

Expand Down
6 changes: 4 additions & 2 deletions app/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 0b120a1

Please sign in to comment.