Skip to content

Conversation

@bsbodden
Copy link
Collaborator

No description provided.

bsbodden and others added 2 commits September 30, 2025 08:59
Enhanced sort_by parameter to accept multiple formats:
- Single field: sort_by="price"
- Field with direction: sort_by=("price", "DESC")
- Multiple fields: sort_by=["price", ("rating", "DESC")]

Note: Redis Search only supports single-field sorting, so only the first
field is used for actual sorting. A warning is logged when multiple fields
are specified. This provides a flexible API for future enhancements while
working within Redis's current limitations.

Changes:
- Added SortSpec type alias for flexible sort specifications
- Implemented _parse_sort_spec() static method to normalize sort inputs
- Overrode sort_by() method in BaseQuery with validation and warnings
- Updated type hints for FilterQuery, VectorQuery, VectorRangeQuery, TextQuery
- Added comprehensive unit tests for multiple field sorting

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added integration tests using TestContainers to verify multiple field
sorting behavior with real Redis Search instances. Tests cover:

- FilterQuery: single/multiple field sorting with various formats
- VectorQuery: custom sorting vs default vector distance sorting
- TextQuery: sorting text search results by custom fields
- Edge cases: invalid inputs, empty lists, None values
- Backward compatibility: existing sort_by usage patterns
- Warning verification: logs when multiple fields specified

All 17 integration tests validate that:
1. Single field sorting works in ASC/DESC modes
2. Multiple field specifications are accepted but only first field used
3. Warnings are logged when multiple fields provided
4. Filter expressions work correctly with sorting
5. Old-style API calls remain functional

Test data: 8 product records with varied price, rating, and stock values.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
BM25STD scorer was introduced in Redis 7.0.0. These tests will be skipped
on Redis 6.2.6-v9 with an informative message.
@bsbodden bsbodden requested a review from Copilot September 30, 2025 17:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements multi-field sorting functionality for Redis vector library queries to address issue #373. It introduces enhanced sort_by parameters that support single fields, tuples with direction, and lists of multiple fields, while maintaining backward compatibility with existing API usage.

Key changes:

  • Enhanced sort_by parameter handling to support multiple formats (string, tuple, list)
  • Added comprehensive unit and integration tests for the new sorting functionality
  • Updated query classes to use the new SortSpec type alias

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
tests/unit/test_multi_field_sorting.py Unit tests covering all supported sort_by formats and edge cases
tests/integration/test_multi_field_sorting_integration.py Integration tests with actual Redis operations and warning verification
redisvl/query/query.py Core implementation of multi-field sorting with SortSpec type and enhanced sort_by method

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Update comment to use consistent terminology: 'Redis Search only supports
single-field sorting' instead of 'Only the first field is actually used'
for clarity and consistency with runtime warnings.
Add assert_warning_logged helper function to reduce code duplication
in warning assertion patterns across multiple test methods.
@bsbodden bsbodden self-assigned this Sep 30, 2025
@bsbodden bsbodden merged commit 7a573f2 into main Sep 30, 2025
37 checks passed
@bsbodden bsbodden deleted the bsb/issue-373 branch September 30, 2025 18:04
bsbodden added a commit to redis/redis-vl-java that referenced this pull request Oct 22, 2025
Add flexible sort specification API to support multiple sorting formats:
- Single field string: sortBy("price")
- Field with direction: sortBy("price", "DESC")
- SortField object: sortBy(SortField.desc("price"))
- Multiple fields: sortBy(List.of(SortField.desc("price"), SortField.asc("rating")))

Implementation details:
- Created SortField class to represent field name and sort direction
- Created SortSpec utility with parseSortSpec() methods for normalization
- Updated FilterQuery, VectorQuery, VectorRangeQuery, and TextQuery builders
- Added warning when multiple fields specified (Redis limitation - only first used)
- Validates sort direction must be "ASC" or "DESC" (case-insensitive)

Test coverage:
- 11 unit tests in SortSpecTest for parsing and validation
- 21 unit tests in MultiFieldSortingTest for query builder integration
- All existing tests continue to pass

Maintains backward compatibility with existing sortBy(String) and
sortAscending/sortDescending methods.

Refs: redis/redis-vl-python#393
Fixes: #373
bsbodden added a commit to redis/redis-vl-java that referenced this pull request Oct 22, 2025
Add flexible sort specification API to support multiple sorting formats:
- Single field string: sortBy("price")
- Field with direction: sortBy("price", "DESC")
- SortField object: sortBy(SortField.desc("price"))
- Multiple fields: sortBy(List.of(SortField.desc("price"), SortField.asc("rating")))

Implementation details:
- Created SortField class to represent field name and sort direction
- Created SortSpec utility with parseSortSpec() methods for normalization
- Updated FilterQuery, VectorQuery, VectorRangeQuery, and TextQuery builders
- Added warning when multiple fields specified (Redis limitation - only first used)
- Validates sort direction must be "ASC" or "DESC" (case-insensitive)

Test coverage:
- 11 unit tests in SortSpecTest for parsing and validation
- 21 unit tests in MultiFieldSortingTest for query builder integration
- All existing tests continue to pass

Maintains backward compatibility with existing sortBy(String) and
sortAscending/sortDescending methods.

Refs: redis/redis-vl-python#393
bsbodden added a commit to redis/redis-vl-java that referenced this pull request Oct 22, 2025
Add flexible sort specification API to support multiple sorting formats:
- Single field string: sortBy("price")
- Field with direction: sortBy("price", "DESC")
- SortField object: sortBy(SortField.desc("price"))
- Multiple fields: sortBy(List.of(SortField.desc("price"), SortField.asc("rating")))

Implementation details:
- Created SortField class to represent field name and sort direction
- Created SortSpec utility with parseSortSpec() methods for normalization
- Updated FilterQuery, VectorQuery, VectorRangeQuery, and TextQuery builders
- Added warning when multiple fields specified (Redis limitation - only first used)
- Validates sort direction must be "ASC" or "DESC" (case-insensitive)

Test coverage:
- 11 unit tests in SortSpecTest for parsing and validation
- 21 unit tests in MultiFieldSortingTest for query builder integration
- All existing tests continue to pass

Maintains backward compatibility with existing sortBy(String) and
sortAscending/sortDescending methods.

Refs: redis/redis-vl-python#393
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.

2 participants