diff --git a/python/python/lancedb/rerankers/mrr.py b/python/python/lancedb/rerankers/mrr.py index 38f742bb8..2fdf23f3a 100644 --- a/python/python/lancedb/rerankers/mrr.py +++ b/python/python/lancedb/rerankers/mrr.py @@ -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() diff --git a/python/python/lancedb/rerankers/rrf.py b/python/python/lancedb/rerankers/rrf.py index c9b54a5a7..e683aeeb9 100644 --- a/python/python/lancedb/rerankers/rrf.py +++ b/python/python/lancedb/rerankers/rrf.py @@ -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() ] diff --git a/python/python/tests/test_rerankers.py b/python/python/tests/test_rerankers.py index af55ce98a..88b7f7187 100644 --- a/python/python/tests/test_rerankers.py +++ b/python/python/tests/test_rerankers.py @@ -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