Skip to content

Commit 5f605ba

Browse files
fix(index): sort vector search top-k results when not truncating
`SearchResult::top_k` only sorted results by score on the truncation path (candidates > k). When `k >= candidate count`, it returned rows in the unordered scored-map / insertion order instead of by relevance. Because the row order was previously discarded downstream (row-range scans re-read in file order), this went unnoticed — but consumers that honor the row order (e.g. the DataFusion `vector_search` scan) then get unranked results whenever `limit >= number of matched rows`. This affects `vector_search`, `full_text_search`, and `hybrid_search`. Sort the no-truncation branch by relevance rank too, and update the unit tests that asserted the old insertion-order behavior.
1 parent 27dea79 commit 5f605ba

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

crates/paimon/src/vector_search.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,10 @@ impl SearchResult {
218218
}
219219

220220
if best_by_row_id.len() <= k {
221-
// Keep the original row order when no truncation is needed.
222-
let rows = self
223-
.row_ids
224-
.iter()
225-
.filter_map(|row_id| best_by_row_id.remove(row_id))
226-
.collect();
221+
// Still sort best-first: the scored map is unordered, and consumers rely
222+
// on relevance rank.
223+
let mut rows: Vec<ScoredRow> = best_by_row_id.into_values().collect();
224+
sort_scored_rows_by_rank(&mut rows);
227225
return Self::from_scored_rows(rows);
228226
}
229227

@@ -382,12 +380,14 @@ mod tests {
382380
}
383381

384382
#[test]
385-
fn test_search_result_top_k_preserves_order_without_truncation() {
383+
fn test_search_result_top_k_sorts_best_first_without_truncation() {
384+
// Even when k >= candidate count (no truncation), results must be returned
385+
// best-first by score, not in the input/insertion order.
386386
let result = SearchResult::new(vec![3, 1, 2], vec![0.1, 0.9, 0.5]);
387387

388388
let top = result.top_k(3);
389-
assert_eq!(top.row_ids, result.row_ids);
390-
assert_eq!(top.scores, result.scores);
389+
assert_eq!(top.row_ids, vec![1, 2, 3]);
390+
assert_eq!(top.scores, vec![0.9, 0.5, 0.1]);
391391
}
392392

393393
#[test]
@@ -409,8 +409,9 @@ mod tests {
409409
.without_deleted_row_ranges(Some(&deleted))
410410
.unwrap()
411411
.top_k(10);
412-
assert_eq!(filtered.row_ids, vec![1, 4]);
413-
assert_eq!(filtered.scores, vec![0.1, 0.2]);
412+
// Rows 2..3 are deleted; the remaining rows come back best-first by score.
413+
assert_eq!(filtered.row_ids, vec![4, 1]);
414+
assert_eq!(filtered.scores, vec![0.2, 0.1]);
414415
}
415416

416417
#[test]

0 commit comments

Comments
 (0)