Skip to content

Commit f3f7a3b

Browse files
committed
changelog, pagination fix
1 parent d34012e commit f3f7a3b

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- GET `/collections` collection search sort extension ex. `/collections?sortby=+id`. [#456](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/456)
14+
1115
### Changed
1216

1317
- updated `numReturned` & `numMatched` fields in itemCollection return to `numberReturned` & `numberMatched`. [#446](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/446)

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,30 @@ async def get_all_collections(
187187
Returns:
188188
A tuple of (collections, next pagination token if any).
189189
"""
190-
search_after = token
191-
192-
formatted_sort = None
190+
# Format the sort parameter
191+
formatted_sort = []
193192
if sort:
194-
formatted_sort = {}
195193
for item in sort:
196194
field = item.get("field")
197195
direction = item.get("direction", "asc")
198196
if field:
199-
formatted_sort[field] = {"order": direction}
197+
formatted_sort.append({field: {"order": direction}})
200198
# Always include id as a secondary sort to ensure consistent pagination
201-
formatted_sort.setdefault("id", {"order": "asc"})
202-
203-
# Use a collections-specific default sort that doesn't rely on properties.datetime
204-
collections_default_sort = {"id": {"order": "asc"}}
199+
if not any("id" in item for item in formatted_sort):
200+
formatted_sort.append({"id": {"order": "asc"}})
201+
else:
202+
# Use a collections-specific default sort that doesn't rely on properties.datetime
203+
formatted_sort = [{"id": {"order": "asc"}}]
205204

206205
# Build the search body step by step to avoid type errors
207206
body = {
208-
"sort": formatted_sort or collections_default_sort,
207+
"sort": formatted_sort,
209208
"size": limit,
210209
}
211210

212211
# Only add search_after if we have a token
213-
if search_after is not None:
214-
body["search_after"] = search_after # type: ignore
212+
if token:
213+
body["search_after"] = [token] # search_after must be a list
215214

216215
response = await self.client.search(
217216
index=COLLECTIONS_INDEX,
@@ -228,7 +227,10 @@ async def get_all_collections(
228227

229228
next_token = None
230229
if len(hits) == limit:
231-
next_token = hits[-1]["sort"][0]
230+
# Ensure we have a valid sort value for next_token
231+
next_token_values = hits[-1].get("sort")
232+
if next_token_values:
233+
next_token = next_token_values[0]
232234

233235
return collections, next_token
234236

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ async def get_all_collections(
159159
request: Request,
160160
sort: Optional[List[Dict[str, Any]]] = None,
161161
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
162-
"""
163-
Retrieve a list of collections from Opensearch, supporting pagination.
162+
"""Retrieve a list of collections from Elasticsearch, supporting pagination.
164163
165164
Args:
166165
token (Optional[str]): The pagination token.
@@ -171,7 +170,7 @@ async def get_all_collections(
171170
Returns:
172171
A tuple of (collections, next pagination token if any).
173172
"""
174-
collections_default_sort = [{"id": {"order": "asc"}}]
173+
# Format the sort parameter
175174
formatted_sort = []
176175
if sort:
177176
for item in sort:
@@ -183,21 +182,22 @@ async def get_all_collections(
183182
if not any("id" in item for item in formatted_sort):
184183
formatted_sort.append({"id": {"order": "asc"}})
185184
else:
186-
formatted_sort = collections_default_sort
185+
# Use a collections-specific default sort that doesn't rely on properties.datetime
186+
formatted_sort = [{"id": {"order": "asc"}}]
187187

188-
search_body = {
188+
# Build the search body step by step to avoid type errors
189+
body = {
189190
"sort": formatted_sort,
190191
"size": limit,
191192
}
192193

193-
# Only add search_after to the query if token is not None and not empty
194+
# Only add search_after if we have a token
194195
if token:
195-
search_after = [token]
196-
search_body["search_after"] = search_after
196+
body["search_after"] = [token] # search_after must be a list
197197

198198
response = await self.client.search(
199199
index=COLLECTIONS_INDEX,
200-
body=search_body,
200+
body=body,
201201
)
202202

203203
hits = response["hits"]["hits"]

0 commit comments

Comments
 (0)