fix(python): honor deduplicate in multivector rerankers (#5266)

Closes #5265

## Summary

- Honor `deduplicate=False` in both multivector rank fusers. Each
retained row keeps its original distance and receives the same fused
score as other rows for its ID.
- Preserve the existing default deduplication and rank calculation.

## Validation

- Both new regression cases fail on main and pass with the fix. They
exercise repeated rows, distinct distances, stable fused scores, and
default deduplication.
- Full Python reranker test module: 42 passed, 13 skipped. Ruff and `git
diff --check` passed.
This commit is contained in:
Divyam Talwar
2026-11-30 20:12:44 +08:00
committed by GitHub
parent e435593602
commit 3e9941fc7c
3 changed files with 30 additions and 4 deletions
+5 -2
View File
@@ -121,7 +121,7 @@ class MRRReranker(Reranker):
self,
vector_results: Union[List[pa.Table], List["LanceVectorQueryBuilder"]],
query: str = None,
deduplicate: bool = True, # noqa: F821
deduplicate: bool = True,
):
"""
Reranks the results from multiple vector searches using MRR algorithm.
@@ -129,6 +129,8 @@ class MRRReranker(Reranker):
and MRR calculates the mean of reciprocal ranks across all systems.
This cannot reuse rerank_hybrid because MRR semantics require treating
each vector result as a separate ranking system.
Set `deduplicate=False` to keep all result rows with their fused scores.
"""
if not vector_results:
raise ValueError("vector_results must not be empty")
@@ -174,7 +176,8 @@ class MRRReranker(Reranker):
final_mrr_scores[result_id] = mean_rr
combined = pa.concat_tables(vector_results, **self._concat_tables_args)
combined = self._deduplicate(combined)
if deduplicate:
combined = self._deduplicate(combined)
combined_row_ids = combined["_rowid"].to_pylist()
+5 -2
View File
@@ -78,13 +78,15 @@ class RRFReranker(Reranker):
self,
vector_results: Union[List[pa.Table], List["LanceVectorQueryBuilder"]],
query: str = None,
deduplicate: bool = True, # noqa: F821 # TODO: automatically deduplicates
deduplicate: bool = True,
):
"""
Overridden method to rerank the results from multiple vector searches.
This leverages the RRF hybrid reranking algorithm to combine the
results from multiple vector searches as it doesn't support reranking
vector results individually.
Set `deduplicate=False` to keep all result rows with their fused scores.
"""
if not vector_results:
raise ValueError("vector_results must not be empty")
@@ -119,7 +121,8 @@ class RRFReranker(Reranker):
rrf_score_map[result_id] += 1 / (i + self.K)
combined = pa.concat_tables(vector_results, **self._concat_tables_args)
combined = self._deduplicate(combined)
if deduplicate:
combined = self._deduplicate(combined)
relevance_scores = [
rrf_score_map[row_id] for row_id in combined["_rowid"].to_pylist()
]
+20
View File
@@ -442,6 +442,26 @@ def test_rrf_multivector_ranks_each_result_list():
assert result["_rowid"].to_pylist() == [2, 1, 3]
@pytest.mark.parametrize("reranker_cls", [RRFReranker, MRRReranker])
def test_rank_fusion_multivector_can_keep_duplicate_rows(reranker_cls):
first = pa.table({"_rowid": [10, 20], "_distance": [0.125, 0.25]})
second = pa.table({"_rowid": [20, 30], "_distance": [0.0625, 0.5]})
reranker = reranker_cls(return_score="all")
default = reranker.rerank_multivector([first, second])
assert default["_rowid"].to_pylist() == [20, 10, 30]
assert default["_distance"].to_pylist()[0] == 0.25
duplicates = reranker.rerank_multivector([first, second], deduplicate=False)
assert duplicates["_rowid"].to_pylist() == [20, 20, 10, 30]
assert sorted(duplicates["_distance"].to_pylist()[:2]) == [0.0625, 0.25]
default_scores = default["_relevance_score"].to_pylist()
assert duplicates["_relevance_score"].to_pylist() == [
default_scores[0],
*default_scores,
]
@pytest.mark.parametrize("reranker_cls", [RRFReranker, MRRReranker])
def test_rank_fusion_return_score_all_keeps_both_scores(reranker_cls):
# A row found by both searches must keep its vector `_distance` and its FTS