PB-1677: sort extension - Add sortby query param - #663
Merged
asteiner-swisstopo merged 5 commits intoSep 8, 2026
Merged
Conversation
boecklic
approved these changes
Sep 8, 2026
boecklic
left a comment
Contributor
There was a problem hiding this comment.
Nice 🎉
Adding /sortables will be in a separate PR I guess?
| class GetPostCursorPagination(CursorPagination): | ||
| class SortedCursorPagination(CursorPagination): | ||
| '''Pagination class that supports sorting via the sortby parameter''' | ||
| ordering = 'name' |
Contributor
There was a problem hiding this comment.
is this declaration and initialisation correct? below it's defined as an array (which by default is empty), here it's a string..?
Contributor
Author
There was a problem hiding this comment.
Good catch but it should be fine. According to the DRF docs both works:
ordering= This should be a string, or list of strings, indicating the field against which the cursor based pagination will be applied. For example:ordering = 'slug'. Defaults to-created. This value may also be overridden by usingOrderingFilteron the view.
| return sort_fields | ||
|
|
||
|
|
||
| def _resolve_sort_field(field_name, sortable_fields): |
Contributor
There was a problem hiding this comment.
the other helper functions are not _* prefixed... why this one?
Contributor
Author
There was a problem hiding this comment.
Because it is only used within the same module.
According to PEP8:
_single_leading_underscore: weak “internal use” indicator. E.g.from M import *does not import objects whose names start with an underscore.
The other new functions are used elsewhere, so not just used "internally".
I think that is a common way to mark helper functions, so I keep it as it is.
For the moment, sorting is only allowed for the "core" properties: - id - collection - properties.datetime - properties.title - properties.created - properties.updated For the other properties coming from STAC extensions we don't allow sorting for the moment. This will be addressed in PB-2375 when we have a proper `/sortable` endpoint.
This covers both the GET endpoint where `sortby` is a query parameter
and the POST endpoint where `sortby` is a field in the request body like
```json
{
"sortby": [
{
"field": "properties.created",
"direction": "asc"
},
{
"field": "collection",
"direction": "desc"
}
]
}
```
Also changed how the sortby param is handled in
`/collections/{collectionId}/items`: Like /search, it now validates
`sortby` upfront and stores the deserialized fields on
`view.sort_fields` before pagination. Previously
`SortedCursorPagination.get_ordering()` read and parsed `sortby` from
the request only later in the process.
`test_search_endpoint.py` exceeded pylint's `too-many-lines` (C0302), so
the forecast, CF, and sort test classes were moved to
`test_search_endpoint_extensions.py`.
Don't mention the allowed fields as the /sortable endpoint should list that.
This is more in line with how the query extension is implemented. For a "query" to the POST /search endpoint the following field values are allowed: - created - updated - title - cf:standard_name - unit So it's not, e.g., "properties.updated" but only "updated". The "sortby" now works the same. The following fields are allowed for sort: - id - collection - datetime - title - created - updated
asteiner-swisstopo
force-pushed
the
feat-pb-1677-service-stac-implement-the-sort-extension
branch
from
September 8, 2026 14:11
a7bf2aa to
1e6142a
Compare
asteiner-swisstopo
deleted the
feat-pb-1677-service-stac-implement-the-sort-extension
branch
September 8, 2026 14:38
asteiner-swisstopo
added a commit
that referenced
this pull request
Sep 9, 2026
Follow-up of #664: Prepend `properties.` to certain parameters used in the `sortby` query parameter and in the `/sortables` endpoint: - `datetime` -> `properties.datetime` - `title` -> `properties.title` - `created` -> `properties.created` - `updated` -> `properties.updated` - `id` and `collection` stay unchanged (top-level fields) In #663, we chose to omit the `properties.` prefix for the `sortby` query parameter. So we refer to `datetime`, not `properties.datetime`. This was because the "query" parameter of the [POST /search endpoint](https://data.geo.admin.ch/api/stac/static/spec/v1/apitransactional.html#tag/STAC/operation/postSearchSTAC) also omits the `properties.` prefix. Looking into the STAC Browser code, however, I see that the `properties.` prefix is the assumed default. I think then it would be better to follow the STAC Browser. The Query extension will anyway be replaced by the Filter extension (see PB-2392), so the inconsistency is ok.
asteiner-swisstopo
added a commit
that referenced
this pull request
Sep 10, 2026
Follow-up of #664: Prepend `properties.` to certain parameters used in the `sortby` query parameter and in the `/sortables` endpoint: - `datetime` -> `properties.datetime` - `title` -> `properties.title` - `created` -> `properties.created` - `updated` -> `properties.updated` - `id` and `collection` stay unchanged (top-level fields) In #663, we chose to omit the `properties.` prefix for the `sortby` query parameter. So we refer to `datetime`, not `properties.datetime`. This was because the "query" parameter of the [POST /search endpoint](https://data.geo.admin.ch/api/stac/static/spec/v1/apitransactional.html#tag/STAC/operation/postSearchSTAC) also omits the `properties.` prefix. Looking into the STAC Browser code, however, I see that the `properties.` prefix is the assumed default. I think then it would be better to follow the STAC Browser. The Query extension will anyway be replaced by the Filter extension (see PB-2392), so the inconsistency is ok.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This allows sorting items with a
sortbyquery parameter as defined in the sort extension:The following properties are sortable:
As we don't support Collection Search, the only affected endpoints are:
/collections/{collectionId}/items/searchwith simple filtering/searchwith full-featured filteringQueries in practice look like this:
Notes:
createdand notproperties.created. I would findproperties.createdbetter but the "query" parameter in the /search endpoint also refers to the properties without theproperties.prefix. So I favored consistency.test_search_endpoint.pytotest_search_endpoint_extensions.pybecause the file got too long.Follow-up work:
/sortablesendpoint listing the sortable properties/conformanceendpoint/sortablesendpoint that takes into account whichstac_extensionsare declared. For the moment, only core properties are supported.