Skip to content

As a user, I want to sort API results ascending or descending per fieldΒ #817

Description

@jordanpadams

🎯 User Story

As a PDS Data User, I want to sort API results in ascending or descending order per field so that I can retrieve data in the order most useful for my query (e.g., newest first, oldest first, lowest version first).

✨ Motivation

The sort query parameter currently accepts one or more field names but unconditionally sorts all fields in ascending order. There is no way for a caller to request descending order for any field. This limits the usefulness of the sort feature β€” for example, a user cannot ask for the most recently harvested products first, or retrieve the highest version of a product at the top of results.

The current behavior is documented in the OpenAPI spec (swagger.yml) with the note: "currently only sorts ascending", confirming this is a known limitation rather than intended design.

πŸ“‹ Proposed Change

Extend the sort parameter syntax to support an optional direction modifier per field. The proposed syntax aligns with common REST API conventions:

sort=<field>:<direction>

Where <direction> is either asc (default, backwards-compatible) or desc.

Examples:

  • sort=ops:Harvest_Info.ops:harvest_date_time:desc β€” most recently harvested first
  • sort=vid:asc β€” lowest version first (same as current default)
  • sort=vid β€” ascending (no direction = default asc, fully backwards-compatible)
  • sort=title:asc&sort=vid:desc β€” multi-field: title ascending, then version descending

Affected components:

  • swagger.yml β€” update the Sort parameter description to document the new syntax
  • RegistrySearchRequestBuilder.sortFromStrings() β€” parse the optional :asc/:desc suffix and pass SortOrder.Asc or SortOrder.Desc accordingly to the OpenSearch FieldSort
  • Unit tests in RegistrySearchRequestBuilderTest β€” add coverage for ascending, descending, and default (no-direction) sort inputs
  • Integration tests β€” add test cases exercising descending sort

🩺 Additional Context

  • The search-after pagination feature requires a matching sort list; the new syntax must not break the existing length-equality validation between sort and search-after.
  • Backwards compatibility must be preserved: a bare field name with no :asc/:desc suffix must continue to sort ascending.
  • The field-name-to-OpenSearch-property conversion in SearchUtil.jsonPropertyToOpenProperty() must occur on the field portion only, not on the direction suffix.

For Internal Dev Team To Complete

βš™οΈ Engineering Details

πŸŽ‰ Integration & Test

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions