Skip to content

Commit 58725ea

Browse files
committed
add sort to item collection route
1 parent 8974c38 commit 58725ea

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

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

1111
### Added
1212

13+
- Sortby functionality to the item collection route.
14+
1315
### Changed
1416

1517
- Changed assets serialization to prevent mapping explosion while allowing asset inforamtion to be indexed. [#341](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/341)
1618

1719
### Fixed
1820

21+
- Fixed issue where sortby was not accepting the default sort where a + or - was not specified before the field value ie. localhost:8081/collections/{collection_id}/items?sortby=id.
22+
1923
## [v6.2.1] - 2025-09-02
2024

2125
### Added

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import datetime as datetime_type
66
from datetime import timezone
77
from enum import Enum
8+
from types import SimpleNamespace
89
from typing import List, Optional, Set, Type, Union
910
from urllib.parse import unquote_plus, urljoin
1011

@@ -287,6 +288,7 @@ async def item_collection(
287288
bbox: Optional[BBox] = None,
288289
datetime: Optional[str] = None,
289290
limit: Optional[int] = None,
291+
sortby: Optional[str] = None,
290292
token: Optional[str] = None,
291293
**kwargs,
292294
) -> stac_types.ItemCollection:
@@ -296,7 +298,7 @@ async def item_collection(
296298
collection_id (str): The identifier of the collection to read items from.
297299
bbox (Optional[BBox]): The bounding box to filter items by.
298300
datetime (Optional[str]): The datetime range to filter items by.
299-
limit (int): The maximum number of items to return.
301+
sortby (Optional[str]]): Sort spec like "-datetime". Bare fields imply ascending.
300302
token (str): A token used for pagination.
301303
request (Request): The incoming request.
302304
@@ -313,6 +315,16 @@ async def item_collection(
313315

314316
base_url = str(request.base_url)
315317

318+
es_sort = None
319+
if sortby:
320+
specs = []
321+
for s in sortby:
322+
field = s[1:]
323+
direction = "desc" if s[0] == "-" else "asc"
324+
specs.append(SimpleNamespace(field=field, direction=direction))
325+
if specs:
326+
es_sort = self.database.populate_sort(specs)
327+
316328
collection = await self.get_collection(
317329
collection_id=collection_id, request=request
318330
)
@@ -346,7 +358,7 @@ async def item_collection(
346358
items, maybe_count, next_token = await self.database.execute_search(
347359
search=search,
348360
limit=limit,
349-
sort=None,
361+
sort=es_sort,
350362
token=token,
351363
collection_ids=[collection_id],
352364
datetime_search=datetime_search,

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
from fastapi import FastAPI
88

99
from stac_fastapi.api.app import StacApi
10-
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
10+
from stac_fastapi.api.models import (
11+
ItemCollectionUri,
12+
create_get_request_model,
13+
create_post_request_model,
14+
create_request_model,
15+
)
1116
from stac_fastapi.core.core import (
1217
BulkTransactionsClient,
1318
CoreClient,
@@ -39,6 +44,7 @@
3944
TransactionExtension,
4045
)
4146
from stac_fastapi.extensions.core.filter import FilterConformanceClasses
47+
from stac_fastapi.extensions.core.sort import SortConformanceClasses
4248
from stac_fastapi.extensions.third_party import BulkTransactionExtension
4349
from stac_fastapi.sfeos_helpers.aggregation import EsAsyncBaseAggregationClient
4450
from stac_fastapi.sfeos_helpers.filter import EsAsyncBaseFiltersClient
@@ -54,6 +60,7 @@
5460

5561
database_logic = DatabaseLogic()
5662

63+
5764
filter_extension = FilterExtension(
5865
client=EsAsyncBaseFiltersClient(database=database_logic)
5966
)
@@ -114,6 +121,17 @@
114121

115122
post_request_model = create_post_request_model(search_extensions)
116123

124+
items_get_request_model = create_request_model(
125+
model_name="ItemCollectionUri",
126+
base_model=ItemCollectionUri,
127+
extensions=[
128+
SortExtension(
129+
conformance_classes=[SortConformanceClasses.ITEMS],
130+
)
131+
],
132+
request_type="GET",
133+
)
134+
117135
app_config = {
118136
"title": os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi-elasticsearch"),
119137
"description": os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi-elasticsearch"),
@@ -128,6 +146,7 @@
128146
),
129147
"search_get_request_model": create_get_request_model(search_extensions),
130148
"search_post_request_model": post_request_model,
149+
"items_get_request_model": items_get_request_model,
131150
"route_dependencies": get_route_dependencies(),
132151
}
133152

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def populate_sort_shared(sortby: List) -> Optional[Dict[str, Dict[str, str]]]:
8282
directly used in search requests.
8383
Always includes 'id' as secondary sort to ensure unique pagination tokens.
8484
"""
85+
print("sortbx: ", sortby)
8586
if sortby:
8687
sort_config = {s.field: {"order": s.direction} for s in sortby}
8788
sort_config.setdefault("id", {"order": "asc"})

0 commit comments

Comments
 (0)