Skip to content

Fix sort_by on embedded model field paths - #871

Open
VanditGupta wants to merge 1 commit into
redis:mainfrom
VanditGupta:prbot/issue-431-a10
Open

VanditGupta wants to merge 1 commit into
redis:mainfrom
VanditGupta:prbot/issue-431-a10

Conversation

@VanditGupta

@VanditGupta VanditGupta commented Oct 20, 2026 •

Copy link
Copy Markdown

Fixes #431

Summary

sort_by() rejected embedded model fields: validate_sort_fields only looked at the model's top-level fields, so sort_by("metrics__score") and sort_by("metrics.score") both raised QueryNotSupportedError ("that field does not exist on the model"), even though find() on the same field works.

This adds a small _resolve_sort_field helper that accepts parent__child or parent.child, walks the same ExpressionProxy chain find() uses, and returns the field plus its RediSearch name (<parent>_<child>, e.g. metrics_score). Validation uses it for the existing sortable/indexed checks, and resolve_redisearch_sort_fields uses it for the SORTBY name. Top-level fields and the KNN score field behave as before. A path to a field that doesn't exist, or one that is neither sortable nor indexed, still raises QueryNotSupportedError.

Added test_sorting_on_embedded_field in tests/test_json_model.py: ascending and descending order with both separators, the generated SORTBY arguments, and both error cases. Only the async code and tests are edited; the sync versions are generated by make sync.

Testing

Run in a Linux Docker container with redis/redis-stack-server, on this branch:

  • The new test passes in tests/ and in the generated tests_sync/. On the unmodified code it fails with the error from the issue.
  • tests/ and tests_sync/: everything passes except test_cli_migrate.py (2 tests in each directory). Those fail the same way without this change, because the CLI subprocess in my setup cannot reach Redis.
  • ruff check, ruff format --check and mypy (the make lint commands) pass.

Not run: make test itself (I ran pytest directly against one Redis Stack instance, without -n auto or coverage), make test_oss against OSS Redis, and nested paths deeper than one embedded level.

AI assistance

This change was drafted with an AI coding assistant (Claude) and then reviewed by me.

  • I read this diff and collaborated with the Claude agent on it
  • I ran the tests myself and attached proof in this PR where required
  • I checked the project's current contribution and AI policy

Note

Low Risk
Localized query-layer change with tests; behavior for top-level sort fields and KNN is preserved.

Overview
sort_by() now accepts embedded JsonModel field paths (metrics__score or metrics.score), matching what find() already supports on nested fields.

FindQuery gains _resolve_sort_field, which walks the same ExpressionProxy chain as filter expressions and maps paths to RediSearch index names (e.g. metrics_score). validate_sort_fields and resolve_redisearch_sort_fields use it for existence/sortable checks and SORTBY args; top-level fields and KNN score sorting are unchanged. Invalid or non-sortable embedded paths still raise QueryNotSupportedError.

Adds test_sorting_on_embedded_field for asc/desc order, SORTBY resolution, and error cases.

Reviewed by Cursor Bugbot for commit b2e0697. Bugbot is set up for automated code reviews on this repo. Configure here.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EmbeddedJson fields are not able to be sorted on, even with the sortable=True flag

1 participant