Repository navigation
Fix semantic search threshold to filter by composite score (#787) - #822
Conversation
Previously, the threshold parameter filtered results based on raw cosine similarity, but users saw composite scores (which include breadth boost). This caused confusing behavior where documents with composite score above the threshold could be filtered out. Changes: - Move composite score calculation into SQL using math::log10 - Filter by composite score instead of raw similarity - Sort by composite score in SQL (remove Rust-side re-sorting) - Remove BREADTH_BOOST constant (now inlined in SQL) - Add test demonstrating the fix Example: A document with raw similarity 0.68 and 3 matching chunks has composite score ~0.78. With threshold 0.7, it now IS returned (composite 0.78 > 0.7) instead of being filtered out (raw 0.68 < 0.7). Closes #787 Co-Authored-By: Claude <[email protected]>
Code Review: Fix Semantic Search Threshold to Filter by Composite Score (#787)Review Type: Initial Review SummaryThis PR addresses a user-facing inconsistency in semantic search where the threshold parameter filtered by raw cosine similarity, but users observed composite scores (which include breadth boost). The fix correctly moves the composite score calculation into SQL, ensuring threshold filtering matches user expectations. Verdict: APPROVE The implementation is clean, well-documented, and correctly addresses the issue. The code follows project standards and improves both maintainability and correctness. Requirements Validation (Issue #787)
All acceptance criteria satisfied. FindingsCritical IssuesNone. Suggested Improvements1. [Improvement] Redundant Composite Score Calculation in WHERE ClauseFile: The composite score is calculated twice: once in the SELECT (line 4166) and again in the WHERE clause (line 4181): -- Line 4166: SELECT clause
max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) AS composite_score
-- Line 4181: WHERE clause (duplicate calculation)
WHERE max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) > $thresholdPrinciple: DRY (Don't Repeat Yourself) Recommendation: This is a known SurrealDB limitation (no HAVING clause, cannot reference aliases in WHERE). The current implementation is the correct workaround. Consider adding a brief comment explaining this is intentional due to SurrealDB limitations: // Note: composite_score calculated twice because SurrealDB doesn't support
// referencing SELECT aliases in WHERE clause (no HAVING support)
WHERE max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) > $thresholdSeverity: Nit - the existing comment at line 4143-4144 already explains this. This is optional polish. NitpicksNit 1: Magic Number DocumentationFile: The comment says Consider adding a note about keeping these synchronized: // BREADTH_BOOST = 0.3 inlined in SQL (appears at lines: SELECT composite_score, WHERE clause)
// If changing this value, update both occurrencesThis is purely optional and the current implementation is acceptable. Code Quality AssessmentStrengths
ArchitectureThe design decision to move composite score calculation to SQL is sound:
Security AssessmentNo security concerns. The query uses parameterized bindings ( Performance AssessmentThe change is performance-neutral or slightly positive:
The Test CoverageNew test added:
Existing tests: The PR confirms all 691 Rust tests pass, indicating no regressions. Final RecommendationAPPROVE This is a high-quality fix that:
The implementation is a net positive improvement to code health. The minor suggestions above are optional polish and do not block merging. Reviewed using the Pragmatic Quality Framework |
malibio
left a comment
There was a problem hiding this comment.
Code review completed. Recommendation: APPROVE. Clean implementation that correctly addresses Issue #787. All acceptance criteria met, comprehensive test coverage, and well-documented changes. See detailed review comment above.
Address code review suggestion by restructuring the nested subquery to calculate composite_score once and filter on it in an outer query. **Previous approach (duplicated expression):** - Calculate composite_score in SELECT clause - Repeat same calculation in WHERE clause (SurrealDB limitation) **New approach (no duplication):** - Add 4th nesting level to separate calculation from filtering - Level 1: KNN search for candidate chunks - Level 2: GROUP BY node with aggregates (max_similarity, matching_chunks) - Level 3: Calculate composite_score from aggregates (defined once) - Level 4: Filter by composite_score > threshold (references alias) This works because SurrealDB allows referencing aliases from subqueries in outer WHERE clauses (per GitHub Discussion #1434). All embedding tests pass including test_knn_search_respects_threshold. Co-Authored-By: Claude <[email protected]>
Review Feedback Addressed🟢 SQL Duplication EliminatedCommit: 230649c Restructured the semantic search SQL query to eliminate the duplicate composite score calculation. Before (3 levels, duplicated expression): SELECT * FROM (
SELECT node, max_similarity, matching_chunks,
max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) AS composite_score -- ①
FROM (...)
WHERE max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) > $threshold -- ② DUPLICATE
)After (4 levels, single definition): SELECT * FROM (
SELECT * FROM (
SELECT node, max_similarity, matching_chunks,
max_similarity * (1.0 + 0.3 * math::log10(matching_chunks)) AS composite_score -- defined once
FROM (...)
)
WHERE composite_score > $threshold -- references alias from inner query
)Why this works: SurrealDB allows referencing aliases from subqueries in outer WHERE clauses (per GitHub Discussion #1434). Tests: All embedding tests pass including |
* Fix semantic search threshold to filter by composite score (Issue #787) Previously, the threshold parameter filtered results based on raw cosine similarity, but users saw composite scores (which include breadth boost). This caused confusing behavior where documents with composite score above the threshold could be filtered out. Changes: - Move composite score calculation into SQL using math::log10 - Filter by composite score instead of raw similarity - Sort by composite score in SQL (remove Rust-side re-sorting) - Remove BREADTH_BOOST constant (now inlined in SQL) - Add test demonstrating the fix Example: A document with raw similarity 0.68 and 3 matching chunks has composite score ~0.78. With threshold 0.7, it now IS returned (composite 0.78 > 0.7) instead of being filtered out (raw 0.68 < 0.7). Closes #787 Co-Authored-By: Claude <[email protected]> * Refactor semantic search SQL to eliminate composite score duplication Address code review suggestion by restructuring the nested subquery to calculate composite_score once and filter on it in an outer query. **Previous approach (duplicated expression):** - Calculate composite_score in SELECT clause - Repeat same calculation in WHERE clause (SurrealDB limitation) **New approach (no duplication):** - Add 4th nesting level to separate calculation from filtering - Level 1: KNN search for candidate chunks - Level 2: GROUP BY node with aggregates (max_similarity, matching_chunks) - Level 3: Calculate composite_score from aggregates (defined once) - Level 4: Filter by composite_score > threshold (references alias) This works because SurrealDB allows referencing aliases from subqueries in outer WHERE clauses (per GitHub Discussion #1434). All embedding tests pass including test_knn_search_respects_threshold. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* Fix semantic search threshold to filter by composite score (Issue #787) Previously, the threshold parameter filtered results based on raw cosine similarity, but users saw composite scores (which include breadth boost). This caused confusing behavior where documents with composite score above the threshold could be filtered out. Changes: - Move composite score calculation into SQL using math::log10 - Filter by composite score instead of raw similarity - Sort by composite score in SQL (remove Rust-side re-sorting) - Remove BREADTH_BOOST constant (now inlined in SQL) - Add test demonstrating the fix Example: A document with raw similarity 0.68 and 3 matching chunks has composite score ~0.78. With threshold 0.7, it now IS returned (composite 0.78 > 0.7) instead of being filtered out (raw 0.68 < 0.7). Closes #787 Co-Authored-By: Claude <[email protected]> * Refactor semantic search SQL to eliminate composite score duplication Address code review suggestion by restructuring the nested subquery to calculate composite_score once and filter on it in an outer query. **Previous approach (duplicated expression):** - Calculate composite_score in SELECT clause - Repeat same calculation in WHERE clause (SurrealDB limitation) **New approach (no duplication):** - Add 4th nesting level to separate calculation from filtering - Level 1: KNN search for candidate chunks - Level 2: GROUP BY node with aggregates (max_similarity, matching_chunks) - Level 3: Calculate composite_score from aggregates (defined once) - Level 4: Filter by composite_score > threshold (references alias) This works because SurrealDB allows referencing aliases from subqueries in outer WHERE clauses (per GitHub Discussion #1434). All embedding tests pass including test_knn_search_respects_threshold. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* Fix semantic search threshold to filter by composite score (Issue #787) Previously, the threshold parameter filtered results based on raw cosine similarity, but users saw composite scores (which include breadth boost). This caused confusing behavior where documents with composite score above the threshold could be filtered out. Changes: - Move composite score calculation into SQL using math::log10 - Filter by composite score instead of raw similarity - Sort by composite score in SQL (remove Rust-side re-sorting) - Remove BREADTH_BOOST constant (now inlined in SQL) - Add test demonstrating the fix Example: A document with raw similarity 0.68 and 3 matching chunks has composite score ~0.78. With threshold 0.7, it now IS returned (composite 0.78 > 0.7) instead of being filtered out (raw 0.68 < 0.7). Closes #787 Co-Authored-By: Claude <[email protected]> * Refactor semantic search SQL to eliminate composite score duplication Address code review suggestion by restructuring the nested subquery to calculate composite_score once and filter on it in an outer query. **Previous approach (duplicated expression):** - Calculate composite_score in SELECT clause - Repeat same calculation in WHERE clause (SurrealDB limitation) **New approach (no duplication):** - Add 4th nesting level to separate calculation from filtering - Level 1: KNN search for candidate chunks - Level 2: GROUP BY node with aggregates (max_similarity, matching_chunks) - Level 3: Calculate composite_score from aggregates (defined once) - Level 4: Filter by composite_score > threshold (references alias) This works because SurrealDB allows referencing aliases from subqueries in outer WHERE clauses (per GitHub Discussion #1434). All embedding tests pass including test_knn_search_respects_threshold. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Summary
math::log10Problem Solved
The semantic search threshold parameter was filtering based on raw cosine similarity, but users see a composite score that includes breadth boost for multi-chunk matching. This caused confusing behavior where documents with composite score above the threshold could be filtered out.
Example scenario (before fix):
After fix:
Changes
packages/core/src/db/surreal_store.rsmath::log10WHERE similarity > $thresholdto filter by composite scoremax_similaritytocomposite_scorecomposite_scorefield toRawSearchResultstructBREADTH_BOOSTconstant (now inlined in SQL as 0.3)packages/core/tests/embedding_service_test.rstest_threshold_filters_by_composite_score_not_raw_similaritytestTest Plan
Test Results
Baseline: Rust: 691 passed, Frontend: 3468 passed
After changes: Rust: 691 passed, Frontend: 3468 passed (no regressions)
Closes #787
🤖 Generated with Claude Code