fix(vector/search): rescore multi-segment hits on a shared basis (#927) - #928
Merged
Conversation
…r-segment quantized scores are not comparable (#927)
This was referenced Aug 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the cross-segment score comparability bug: per-segment similarities come from quantized kernels running against per-segment affine params, with the query clamped into each segment's value range, so the fan-out's global sort compared meaningless numbers. Any segment whose value range excluded the query reported its boundary doc as an exact match (
similarity = 1.0) — a single-doc segment (one upsert commit, routine under segment-per-commit) topped every out-of-range query. Repro:[7, 20, 10, 0, 1]where brute force says[0, 1, 2, 4, 5], with the top three atsim = 1.0.Changes (
segment/fanout.rs+ one stamp inhnsw/searcher.rs)distance/similarityis recomputed asdistance(prepared_raw_query, dequantized_vector)(hit-carried vector, elseget_vector). Candidate generation stays on the quantized kernels — clamping shifts a segment's distances by a per-segment constant, so segment-internal ordering was always correct; only the absolute scores needed a shared basis.min_similarityre-applies on final scores (the local filter can only be lenient, never over-strict, so no hit is lost).exp(-d)similarity underflows to 0.0 at long range (and DotProduct similarity saturates viaclamp(0,1)), collapsing distant hits into ties whose unstable-sort order leaked segment order into results. The merged sort now uses ascendingdistance(lower-is-more-similar for every metric, precise at long range) with a doc-id tiebreak.distance(raw_query, true_f32_vector)— already cross-segment-comparable and more precise than the dequantized basis; the first version of the rescore overwrote them, silently discarding the sidecar's benefit. The searcher now stampsquery_metadata["score_basis"] = "f32-rerank"when the rerank arm actually ran and the fan-out skips its rescore for such segments (per-segment decision, so mixed sidecar/non-sidecar indexes still get clamp-corrected scores where needed).Flat/IVF segmented indexes share the fan-out and get all three for free. No public API changes.
Tests
segment_score_comparability_test.rs: the discovery repro as a regression test (4 disjoint-range segments incl. the degenerate single-doc one, cross-segment stale duplicates, a deletion; queries inside/outside/between ranges must match brute force exactly; a second test pinsmin_similarityon the shared basis). Query positions are tie-free by construction — an exact tie's rescored order would depend on per-segment reconstruction error, which is not what the test pins.[7, 20, 10, ...]corruption.vector_rerank_engine_test.rs(it caught defect 3 during development).Verification
cargo fmt --checkandcargo clippy --all-targets -- -D warningsclean on stable and 1.97.0cargo test -p laurus --lib: 1258 passed;--tests: all 63 binaries okcargo check -p laurus-wasm: clean;cargo doc --no-deps -p laurus: 73 warnings (baseline)get_vectorfallback, bounded by2·top_k·N_segmentshits/query; perf(vector/search): parallelize SegmentFanoutSearcher's per-segment fan-out #926's parallel fan-out (staged, rebases on this) offsets it in wall-clock, and populatinghit.vectorfrom the searcher's resident pool is a further optimization if the budget ever matters.Closes #927. Found while implementing #926; blocks #926's correctness test.