Skip to content

Commit

Permalink
fix: use base64 encoded JSON string of sort keys as pagination token (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
StijnCaerts committed Dec 10, 2024
1 parent 106fabb commit 3bad19e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Database logic."""

import asyncio
import json
import logging
import os
from base64 import urlsafe_b64decode, urlsafe_b64encode
Expand Down Expand Up @@ -660,7 +661,7 @@ async def execute_search(
search_after = None

if token:
search_after = urlsafe_b64decode(token.encode()).decode().split(",")
search_after = json.loads(urlsafe_b64decode(token).decode())

query = search.query.to_dict() if search.query else None

Expand Down Expand Up @@ -700,9 +701,7 @@ async def execute_search(
next_token = None
if len(hits) > limit and limit < max_result_window:
if hits and (sort_array := hits[limit - 1].get("sort")):
next_token = urlsafe_b64encode(
",".join([str(x) for x in sort_array]).encode()
).decode()
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()

matched = (
es_response["hits"]["total"]["value"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Database logic."""

import asyncio
import json
import logging
import os
from base64 import urlsafe_b64decode, urlsafe_b64encode
Expand Down Expand Up @@ -692,7 +693,7 @@ async def execute_search(
search_after = None

if token:
search_after = urlsafe_b64decode(token.encode()).decode().split(",")
search_after = json.loads(urlsafe_b64decode(token).decode())
if search_after:
search_body["search_after"] = search_after

Expand Down Expand Up @@ -732,9 +733,7 @@ async def execute_search(
next_token = None
if len(hits) > limit and limit < max_result_window:
if hits and (sort_array := hits[limit - 1].get("sort")):
next_token = urlsafe_b64encode(
",".join([str(x) for x in sort_array]).encode()
).decode()
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()

matched = (
es_response["hits"]["total"]["value"]
Expand Down

0 comments on commit 3bad19e

Please sign in to comment.