Repository navigation
Description
Problem
The semantic search threshold parameter currently filters based on raw cosine similarity in the database query, but the score returned to users is a composite score that includes a breadth boost for multi-chunk matching. This creates confusing behavior:
Example Scenario
- User searches for "performance optimization and indexing"
- A document has a raw max_similarity of 0.68 but matches in 3 chunks
- The composite score would be:
0.68 * (1 + 0.3 * log10(3)) ≈ 0.78 - User sets threshold to 0.7, expecting to see documents with score > 0.7
- Document is not returned because raw similarity (0.68) < threshold (0.7)
- But if threshold is lowered to 0.5, the document appears with score 0.78!
Users expect: "threshold 0.7 means I see results with score > 0.7"
Actual behavior: "threshold 0.7 means I see results with raw_similarity > 0.7"
Current Flow
1. SQL Query (surreal_store.rs:4012-4033)
├── KNN search: vector <|K|> $query_vector
├── Filter: WHERE similarity > $threshold ← User's threshold applied to RAW similarity
├── Group by node: max_similarity, count(matching_chunks)
└── LIMIT $limit
2. Rust Processing (surreal_store.rs:4050-4071)
├── Calculate composite score:
│ score = max_similarity * (1 + BREADTH_BOOST * log10(matching_chunks))
│ where BREADTH_BOOST = 0.3
└── Return results with composite score
Proposed Solution
Calculate composite score in SQL using math::log10, then filter by composite score:
SELECT
node,
math::max(similarity) AS max_similarity,
count() AS matching_chunks,
math::max(similarity) * (1 + 0.3 * math::log10(count())) AS composite_score
FROM (
SELECT node, vector::similarity::cosine(vector, $query_vector) AS similarity
FROM embedding
WHERE stale = false AND vector <|{knn_limit}|> $query_vector
)
GROUP BY node
HAVING composite_score > $threshold -- Filter by composite score in SQL
ORDER BY composite_score DESC
LIMIT $limit
FETCH node;Benefits
- No arbitrary pre-filter threshold needed
- No over-fetching and post-filtering in Rust
- Database does the filtering efficiently
- Simpler Rust code - just convert results
- User's threshold matches the score they see
Files to Modify
packages/core/src/db/surreal_store.rs
-
Update SQL query (around line 4012-4033):
- Add composite score calculation in SQL
- Change
WHERE similarity > $thresholdtoHAVING composite_score > $threshold - Order by
composite_scoreinstead ofmax_similarity
-
Update
RawSearchResultstruct (around line 3990):struct RawSearchResult { node: SurrealNode, max_similarity: f64, matching_chunks: i64, composite_score: f64, // NEW: calculated in SQL }
-
Simplify Rust processing (around line 4050-4071):
- Remove breadth factor calculation (now done in SQL)
- Use
composite_scoredirectly from query result - Remove re-sorting (SQL already sorted by composite_score)
packages/core/src/db/surreal_store.rs - Move constant to SQL
The BREADTH_BOOST constant (0.3) should be inlined in the SQL query or kept as a constant that's interpolated. Since it's a fixed value, inlining in SQL is fine.
Acceptance Criteria
- Composite score calculated in SQL using
math::log10 - Threshold filters by composite score, not raw similarity
- Documents with composite score > threshold are returned
- Remove Rust-side breadth factor calculation (SQL does it)
- Remove Rust-side re-sorting (SQL already sorts by composite_score)
- Existing tests updated to reflect new behavior
- Add test case demonstrating the fix (document with raw 0.68 but composite 0.78 returned with threshold 0.7)
Technical Context
Composite Score Formula
breadth_factor = 1.0 + BREADTH_BOOST * log10(matching_chunks)
composite_score = max_similarity * breadth_factor
Where BREADTH_BOOST = 0.3
Example calculations:
| raw_similarity | chunks | breadth_factor | composite_score |
|---|---|---|---|
| 0.70 | 1 | 1.00 | 0.70 |
| 0.70 | 2 | 1.09 | 0.76 |
| 0.70 | 5 | 1.21 | 0.85 |
| 0.68 | 3 | 1.14 | 0.78 |
SurrealDB Math Functions
SurrealDB supports math::log10() - see Math functions documentation
Related Code
BREADTH_BOOSTconstant:surreal_store.rs:3951(can be removed after this change)- Current composite calculation:
surreal_store.rs:4056-4058(move to SQL) - MCP handler:
mcp/handlers/search.rs:159(default threshold 0.7)
Activity
malibio commentedon Jan 26, 2026
Starting implementation. Test baseline: Rust: 691 passed, Frontend: 3468 passed
Fix semantic search threshold to filter by composite score (Issue #787)
Fix semantic search threshold to filter by composite score (#787) (#822)
Fix semantic search threshold to filter by composite score (#787) (#822)
Fix semantic search threshold to filter by composite score (#787) (#822)