Skip to content

Conversation

@bsbodden
Copy link
Collaborator

Adds the ability to specify weights for text fields in RedisVL queries, enabling users to prioritize certain fields over others in search results.

  • Support dictionary of field:weight mappings in TextQuery constructor
  • Maintain backward compatibility with single string field names
  • Add set_field_weights() method for dynamic weight updates
  • Generate proper Redis query syntax with weight modifiers
  • Comprehensive validation for positive numeric weights

Example usage:

# Single field with weight
query = TextQuery(text="search", text_field_name={"title": 5.0})

# Multiple fields with weights
query = TextQuery(
    text="search",
    text_field_name={"title": 3.0, "content": 1.5, "tags": 1.0}
)

@bsbodden bsbodden requested a review from rbs333 September 24, 2025 04:00
@bsbodden bsbodden removed the request for review from rbs333 September 25, 2025 21:58
Adds the ability to specify weights for text fields in RedisVL queries,
enabling users to prioritize certain fields over others in search results.

- Support dictionary of field:weight mappings in TextQuery constructor
- Maintain backward compatibility with single string field names
- Add set_field_weights() method for dynamic weight updates
- Generate proper Redis query syntax with weight modifiers
- Comprehensive validation for positive numeric weights

Example usage:
```python
query = TextQuery(text="search", text_field_name={"title": 5.0})

query = TextQuery(
    text="search",
    text_field_name={"title": 3.0, "content": 1.5, "tags": 1.0}
)
```

- Add has_redisearch_module and has_redisearch_module_async helpers to conftest.py
- Add skip_if_no_redisearch and skip_if_no_redisearch_async functions
- Update test_no_proactive_module_checks.py to use shared helpers
- Update test_semantic_router.py to check RediSearch availability in fixtures and tests
- Update test_llmcache.py to check RediSearch availability in all cache fixtures
- Update test_message_history.py to check RediSearch availability for semantic history
- Ensure all tests that require RediSearch are properly skipped on Redis 6.2.6-v9
- BM25STD scorer is not available in Redis versions prior to 7.2.0. Add version check to skip these tests on older Redis versions.
@abrookins
Copy link
Collaborator

Nice one.

@bsbodden bsbodden self-assigned this Sep 26, 2025
@bsbodden bsbodden merged commit 7607554 into main Sep 26, 2025
37 checks passed
@bsbodden bsbodden deleted the bsb/issue-360 branch September 26, 2025 16:39
bsbodden added a commit to redis/redis-vl-java that referenced this pull request Oct 22, 2025
Implements text field weights functionality allowing users to prioritize
certain fields over others in full-text search queries.

Python Reference: PR #384 (redis/redis-vl-python#384)
Test Reference: tests/unit/test_text_query_weights.py (123 lines)

Implementation:
- TextQuery now accepts Map<String, Double> for textFieldWeights
- Builder pattern with textField() for single field (backward compatible)
- Builder method textFieldWeights() for multiple fields with custom weights
- Query syntax: @field:(terms) => { $weight: 5.0 } for non-default weights
- Multiple fields joined with OR operator in parentheses
- Dynamic weight updates via setFieldWeights() method

Validation:
- Rejects negative weights (IllegalArgumentException)
- Rejects zero weights (IllegalArgumentException)
- Ensures all weights are positive numbers
- Maintains backward compatibility with single string field API

Breaking Changes:
- TextQuery constructor changed to private (use Builder instead)
- Updated QueryIntegrationTest to use builder pattern

Tests: 7 unit tests added (TextQueryWeightsTest)
- testTextQueryAcceptsWeightsDict
- testTextQueryGeneratesWeightedQueryString
- testTextQueryMultipleFieldsWithWeights
- testTextQueryBackwardCompatibility
- testTextQueryRejectsNegativeWeights
- testTextQueryRejectsZeroWeights
- testSetFieldWeightsMethod

All tests pass: 289 tests, 14 skipped

Files Modified:
- core/src/main/java/com/redis/vl/query/TextQuery.java (complete rewrite)
- core/src/test/java/com/redis/vl/query/QueryIntegrationTest.java (builder migration)

Files Created:
- core/src/test/java/com/redis/vl/query/TextQueryWeightsTest.java
bsbodden added a commit to redis/redis-vl-java that referenced this pull request Oct 22, 2025
Implements text field weights functionality allowing users to prioritize
certain fields over others in full-text search queries.

Python Reference: PR #384 (redis/redis-vl-python#384)
Test Reference: tests/unit/test_text_query_weights.py (123 lines)

Implementation:
- TextQuery now accepts Map<String, Double> for textFieldWeights
- Builder pattern with textField() for single field (backward compatible)
- Builder method textFieldWeights() for multiple fields with custom weights
- Query syntax: @field:(terms) => { $weight: 5.0 } for non-default weights
- Multiple fields joined with OR operator in parentheses
- Dynamic weight updates via setFieldWeights() method

Validation:
- Rejects negative weights (IllegalArgumentException)
- Rejects zero weights (IllegalArgumentException)
- Ensures all weights are positive numbers
- Maintains backward compatibility with single string field API

Breaking Changes:
- TextQuery constructor changed to private (use Builder instead)
- Updated QueryIntegrationTest to use builder pattern

Tests: 7 unit tests added (TextQueryWeightsTest)
- testTextQueryAcceptsWeightsDict
- testTextQueryGeneratesWeightedQueryString
- testTextQueryMultipleFieldsWithWeights
- testTextQueryBackwardCompatibility
- testTextQueryRejectsNegativeWeights
- testTextQueryRejectsZeroWeights
- testSetFieldWeightsMethod

All tests pass: 289 tests, 14 skipped

Files Modified:
- core/src/main/java/com/redis/vl/query/TextQuery.java (complete rewrite)
- core/src/test/java/com/redis/vl/query/QueryIntegrationTest.java (builder migration)

Files Created:
- core/src/test/java/com/redis/vl/query/TextQueryWeightsTest.java
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.

3 participants