Skip to content

Commit 7a3b8cf

Browse files
committed
add improved error message
1 parent 151804a commit 7a3b8cf

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
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)
1313
- GET `/collections` collection search sort extension ex. `/collections?sortby=+id`. [#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)
15+
- 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)
1516

1617
### Changed
1718

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ These extensions make it easier to build user interfaces that display and naviga
131131
> - `id` (keyword field)
132132
> - `extent.temporal.interval` (date field)
133133
>
134-
> 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 `BadRequestError` with a message about fielddata being disabled.
134+
> 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.
135135
136136
## Package Structure
137137

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,35 @@ async def get_all_collections(
186186
187187
Returns:
188188
A tuple of (collections, next pagination token if any).
189+
190+
Raises:
191+
HTTPException: If sorting is requested on a field that is not sortable.
189192
"""
193+
# Define sortable fields based on the ES_COLLECTIONS_MAPPINGS
194+
sortable_fields = ["id", "extent.temporal.interval"]
195+
196+
# Format the sort parameter
190197
formatted_sort = []
191198
if sort:
192199
for item in sort:
193200
field = item.get("field")
194201
direction = item.get("direction", "asc")
195202
if field:
203+
# Validate that the field is sortable
204+
if field not in sortable_fields:
205+
raise HTTPException(
206+
status_code=400,
207+
detail=f"Field '{field}' is not sortable. Sortable fields are: {', '.join(sortable_fields)}. "
208+
+ "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.",
211+
)
196212
formatted_sort.append({field: {"order": direction}})
197213
# Always include id as a secondary sort to ensure consistent pagination
198214
if not any("id" in item for item in formatted_sort):
199215
formatted_sort.append({"id": {"order": "asc"}})
200216
else:
217+
# Use a collections-specific default sort that doesn't rely on properties.datetime
201218
formatted_sort = [{"id": {"order": "asc"}}]
202219

203220
body = {

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,35 @@ async def get_all_collections(
170170
171171
Returns:
172172
A tuple of (collections, next pagination token if any).
173+
174+
Raises:
175+
HTTPException: If sorting is requested on a field that is not sortable.
173176
"""
177+
# Define sortable fields based on the ES_COLLECTIONS_MAPPINGS
178+
sortable_fields = ["id", "extent.temporal.interval"]
179+
180+
# Format the sort parameter
174181
formatted_sort = []
175182
if sort:
176183
for item in sort:
177184
field = item.get("field")
178185
direction = item.get("direction", "asc")
179186
if field:
187+
# Validate that the field is sortable
188+
if field not in sortable_fields:
189+
raise HTTPException(
190+
status_code=400,
191+
detail=f"Field '{field}' is not sortable. Sortable fields are: {', '.join(sortable_fields)}. "
192+
+ "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.",
195+
)
180196
formatted_sort.append({field: {"order": direction}})
181197
# Always include id as a secondary sort to ensure consistent pagination
182198
if not any("id" in item for item in formatted_sort):
183199
formatted_sort.append({"id": {"order": "asc"}})
184200
else:
201+
# Use a collections-specific default sort that doesn't rely on properties.datetime
185202
formatted_sort = [{"id": {"order": "asc"}}]
186203

187204
body = {

0 commit comments

Comments
 (0)