fix: preserve FTS ranking order#93
Conversation
Summary by CodeRabbit
WalkthroughThis change updates search scoring in Estimated code review effortMedium Suggested labelssearch, tests, scoring Suggested reviewersMaintainers familiar with the archive search index and cache logic 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches✨ Simplify code
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/archive/search-index/search-index.ts`:
- Around line 750-752: The FTS ranking values produced by rankToScore are being
merged directly with other search-path scores in createFindResult, so normalize
the rank-derived score into the same bounded range as the lexical scores before
sorting. Update the scoring logic near rankToScore and the result aggregation in
createFindResult so bm25-based hits do not overwhelm other matches, and keep the
comparison behavior consistent for grouped entity/triple results.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1f00590f-8c2c-4eaf-867d-d9b1720657a6
📒 Files selected for processing (3)
src/archive/query/search-cache.tssrc/archive/search-index/search-index.tstest/archive/query/archive-view.test.ts
945997f to
2bca26d
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/archive/search-index/search-index.ts (1)
113-113: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winCorrect fix for the inverted BM25 ordering, but the scale constant likely over-saturates scores.
Dropping
Math.abs(rank)in favor ofMath.max(0, -rank)correctly fixes the inversion (bm25 returns more-negative values for better matches, so-ranknow grows with relevance) — this addresses the PR's core objective.However,
FTS5_RANK_SCORE_SCALE = 1_000_000is very large relative to typical bm25 magnitudes (commonly single digits to low tens). For almost any nonzero rank,relevancewill already be in the thousands+, sorelevance / (1 + relevance)saturates to ~1.0 for both strong and weak matches alike. Ordering is preserved (the function is monotonic), but the resultingscorevalues become nearly indistinguishable in magnitude — which defeats the purpose of a "bounded relevance score" for any downstream logic that compares/merges scores across buckets or applies thresholds (this is the same underlying concern flagged in the prior review about mixing FTS-derived scores with other lexical scores increateFindResult).Consider a much smaller scale (or a log-based/percentile normalization) so the mapped score retains meaningful separation across the realistic bm25 range.
♻️ Example: reduce scale to preserve score spread
-const FTS5_RANK_SCORE_SCALE = 1_000_000; +const FTS5_RANK_SCORE_SCALE = 1;Also applies to: 751-755
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/archive/search-index/search-index.ts` at line 113, The BM25 normalization in search-index.ts is now correctly using the inverted rank, but FTS5_RANK_SCORE_SCALE is too large and causes score saturation. Adjust the scaling logic in the FTS5 score mapping path (around the relevance/score calculation and the FTS5_RANK_SCORE_SCALE constant) so typical bm25 values still produce spread-out bounded scores, rather than near-1.0 outputs for almost all matches; keep the rank inversion fix intact and prefer a smaller scale or a normalization approach that preserves separation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@src/archive/search-index/search-index.ts`:
- Line 113: The BM25 normalization in search-index.ts is now correctly using the
inverted rank, but FTS5_RANK_SCORE_SCALE is too large and causes score
saturation. Adjust the scaling logic in the FTS5 score mapping path (around the
relevance/score calculation and the FTS5_RANK_SCORE_SCALE constant) so typical
bm25 values still produce spread-out bounded scores, rather than near-1.0
outputs for almost all matches; keep the rank inversion fix intact and prefer a
smaller scale or a normalization approach that preserves separation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4c80c3b3-8c9d-4327-8127-cb2be69bde63
📒 Files selected for processing (3)
src/archive/query/search-cache.tssrc/archive/search-index/search-index.tstest/archive/query/archive-view.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- test/archive/query/archive-view.test.ts
- src/archive/query/search-cache.ts
Summary
Tests