Skip to content

Commit 7e848b4

Browse files
authored
fix: use base64 encoded JSON string of sort keys as pagination token (#323)
**Related Issue(s):** - #322 **Description:** Use base64 encoded JSON string of sort keys as pagination token instead of comma separated string.
1 parent 106fabb commit 7e848b4

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Changed
10+
- Use base64 encoded JSON string of sort keys as pagination token instead of comma-separated string [#323](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/323)
911

1012
## [v3.2.1] - 2024-11-14
1113

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Database logic."""
22

33
import asyncio
4+
import json
45
import logging
56
import os
67
from base64 import urlsafe_b64decode, urlsafe_b64encode
@@ -660,7 +661,7 @@ async def execute_search(
660661
search_after = None
661662

662663
if token:
663-
search_after = urlsafe_b64decode(token.encode()).decode().split(",")
664+
search_after = json.loads(urlsafe_b64decode(token).decode())
664665

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

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

707706
matched = (
708707
es_response["hits"]["total"]["value"]

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Database logic."""
22

33
import asyncio
4+
import json
45
import logging
56
import os
67
from base64 import urlsafe_b64decode, urlsafe_b64encode
@@ -692,7 +693,7 @@ async def execute_search(
692693
search_after = None
693694

694695
if token:
695-
search_after = urlsafe_b64decode(token.encode()).decode().split(",")
696+
search_after = json.loads(urlsafe_b64decode(token).decode())
696697
if search_after:
697698
search_body["search_after"] = search_after
698699

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

739738
matched = (
740739
es_response["hits"]["total"]["value"]

0 commit comments

Comments
 (0)