-
Notifications
You must be signed in to change notification settings - Fork 60
Bsb/issue 373 #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bsb/issue 373 #393
Conversation
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>
5d2660e to
122c7ee
Compare
BM25STD scorer was introduced in Redis 7.0.0. These tests will be skipped on Redis 6.2.6-v9 with an informative message.
There was a problem hiding this 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.
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
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
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
No description provided.