Skip to content

Commit a8d9053

Browse files
committed
simplify error message, add alias
1 parent 7a3b8cf commit a8d9053

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111

1212
- Added `USE_DATETIME` environment variable to configure datetime search behavior in SFEOS. [#452](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/452)
13-
- GET `/collections` collection search sort extension ex. `/collections?sortby=+id`. [#456](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/456)
13+
- GET `/collections` collection search sort extension ex. `/collections?sortby=+id`. Added field alias for `temporal` to enable sorting by temporal extent. [#456](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/456)
1414
- GET `/collections` collection search fields extension ex. `/collections?fields=id,title`. [#465](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/465)
1515
- Improved error messages for sorting on unsortable fields in collection search, including guidance on how to make fields sortable. [#465](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/465)
1616

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ SFEOS implements extended capabilities for the `/collections` endpoint, allowing
120120
- **Sorting**: Sort collections by sortable fields using the `sortby` parameter
121121
- Example: `/collections?sortby=+id` (ascending sort by ID)
122122
- Example: `/collections?sortby=-id` (descending sort by ID)
123+
- Example: `/collections?sortby=-temporal` (descending sort by temporal extent)
123124

124125
- **Field Selection**: Request only specific fields to be returned using the `fields` parameter
125126
- Example: `/collections?fields=id,title,description`
@@ -130,8 +131,11 @@ These extensions make it easier to build user interfaces that display and naviga
130131
> **Note**: Sorting is only available on fields that are indexed for sorting in Elasticsearch/OpenSearch. With the default mappings, you can sort on:
131132
> - `id` (keyword field)
132133
> - `extent.temporal.interval` (date field)
134+
> - `temporal` (alias to extent.temporal.interval)
133135
>
134136
> Text fields like `title` and `description` are not sortable by default as they use text analysis for better search capabilities. Attempting to sort on these fields will result in a user-friendly error message explaining which fields are sortable and how to make additional fields sortable by updating the mappings.
137+
>
138+
> **Important**: Adding keyword fields to make text fields sortable can significantly increase the index size, especially for large text fields. Consider the storage implications when deciding which fields to make sortable.
135139
136140
## Package Structure
137141

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ async def get_all_collections(
191191
HTTPException: If sorting is requested on a field that is not sortable.
192192
"""
193193
# Define sortable fields based on the ES_COLLECTIONS_MAPPINGS
194-
sortable_fields = ["id", "extent.temporal.interval"]
194+
sortable_fields = ["id", "extent.temporal.interval", "temporal"]
195195

196196
# Format the sort parameter
197197
formatted_sort = []
@@ -206,15 +206,13 @@ async def get_all_collections(
206206
status_code=400,
207207
detail=f"Field '{field}' is not sortable. Sortable fields are: {', '.join(sortable_fields)}. "
208208
+ "Text fields are not sortable by default in Elasticsearch. "
209-
+ "To make a field sortable, update the mapping to use 'keyword' type or add a '.keyword' subfield. "
210-
+ "See the ES_COLLECTIONS_MAPPINGS in mappings.py for details.",
209+
+ "To make a field sortable, update the mapping to use 'keyword' type or add a '.keyword' subfield. ",
211210
)
212211
formatted_sort.append({field: {"order": direction}})
213212
# Always include id as a secondary sort to ensure consistent pagination
214213
if not any("id" in item for item in formatted_sort):
215214
formatted_sort.append({"id": {"order": "asc"}})
216215
else:
217-
# Use a collections-specific default sort that doesn't rely on properties.datetime
218216
formatted_sort = [{"id": {"order": "asc"}}]
219217

220218
body = {

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def get_all_collections(
175175
HTTPException: If sorting is requested on a field that is not sortable.
176176
"""
177177
# Define sortable fields based on the ES_COLLECTIONS_MAPPINGS
178-
sortable_fields = ["id", "extent.temporal.interval"]
178+
sortable_fields = ["id", "extent.temporal.interval", "temporal"]
179179

180180
# Format the sort parameter
181181
formatted_sort = []
@@ -190,15 +190,13 @@ async def get_all_collections(
190190
status_code=400,
191191
detail=f"Field '{field}' is not sortable. Sortable fields are: {', '.join(sortable_fields)}. "
192192
+ "Text fields are not sortable by default in OpenSearch. "
193-
+ "To make a field sortable, update the mapping to use 'keyword' type or add a '.keyword' subfield. "
194-
+ "See the ES_COLLECTIONS_MAPPINGS in mappings.py for details.",
193+
+ "To make a field sortable, update the mapping to use 'keyword' type or add a '.keyword' subfield. ",
195194
)
196195
formatted_sort.append({field: {"order": direction}})
197196
# Always include id as a secondary sort to ensure consistent pagination
198197
if not any("id" in item for item in formatted_sort):
199198
formatted_sort.append({"id": {"order": "asc"}})
200199
else:
201-
# Use a collections-specific default sort that doesn't rely on properties.datetime
202200
formatted_sort = [{"id": {"order": "asc"}}]
203201

204202
body = {

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/mappings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class Geometry(Protocol): # noqa
165165
"providers": {"type": "object", "enabled": False},
166166
"links": {"type": "object", "enabled": False},
167167
"item_assets": {"type": "object", "enabled": get_bool_env("STAC_INDEX_ASSETS")},
168+
# Field alias to allow sorting on 'temporal' (points to extent.temporal.interval)
169+
"temporal": {"type": "alias", "path": "extent.temporal.interval"},
168170
},
169171
}
170172

0 commit comments

Comments
 (0)