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
…323)

**Related Issue(s):**

- #322 

**Description:**
Use base64 encoded JSON string of sort keys as pagination token instead
of comma separated string.
  • Loading branch information
StijnCaerts authored Dec 13, 2024
1 parent 106fabb commit 7e848b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- 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)

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

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 @@ -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 7e848b4

Please sign in to comment.