diff --git a/python/python/lancedb/query.py b/python/python/lancedb/query.py index ad456f635..3a66c2ed7 100644 --- a/python/python/lancedb/query.py +++ b/python/python/lancedb/query.py @@ -3545,8 +3545,12 @@ class LanceHybridQueryBuilder(LanceQueryBuilder): sort_indices = np.argsort(scores) if not ascending: sort_indices = sort_indices[::-1] - ranks = np.empty_like(sort_indices) + ranks = np.empty(len(scores)) ranks[sort_indices] = np.arange(len(scores)) + 1 + # Equal scores share the average of their ranks, so the order argsort + # happens to put them in does not decide which one counts as better. + _, groups, counts = np.unique(scores, return_inverse=True, return_counts=True) + ranks = (np.bincount(groups, weights=ranks) / counts)[groups] # replace the _score column with the ranks _score_idx = results.column_names.index(column) results = results.set_column( diff --git a/python/python/tests/test_hybrid_query.py b/python/python/tests/test_hybrid_query.py index 7ed2e3292..d173b2e1e 100644 --- a/python/python/tests/test_hybrid_query.py +++ b/python/python/tests/test_hybrid_query.py @@ -684,6 +684,43 @@ def test_hybrid_single_fts_match_keeps_fts_weight(tmp_path, normalize): ] +@pytest.mark.parametrize("normalize", ["score", "rank"]) +def test_hybrid_tied_fts_matches_share_fts_weight(tmp_path, normalize): + from lancedb.rerankers import LinearCombinationReranker + + db = lancedb.connect(tmp_path) + table = db.create_table( + "test", + [ + {"text": "zebra apple", "vector": [5.0, 5.0]}, + {"text": "zebra grape", "vector": [5.0, 5.1]}, + {"text": "cherry plum", "vector": [0.0, 0.0]}, + ], + ) + table.create_fts_index("text") + + results = ( + table.search(query_type="hybrid") + .vector([0.0, 0.0]) + .text("zebra") + .rerank(LinearCombinationReranker(weight=0.3), normalize=normalize) + .limit(3) + .to_arrow() + ) + + # Both FTS matches have the same BM25 score, so both rank above the + # document without the keyword. + assert results["text"].to_pylist()[-1] == "cherry plum" + + +def test_rank_gives_tied_scores_the_same_rank(): + table = pa.table({"_score": pa.array([0.5, 0.9, 0.5, 0.1], pa.float32())}) + ranked = LanceHybridQueryBuilder._rank(table, "_score") + assert ranked["_score"].to_pylist() == [2.5, 4.0, 2.5, 1.0] + ranked = LanceHybridQueryBuilder._rank(table, "_score", ascending=False) + assert ranked["_score"].to_pylist() == [2.5, 1.0, 2.5, 4.0] + + def test_hybrid_query_rejects_query_with_vector(sync_table: Table): # Pins the full message: the space between "hybrid search." and "But" is # load-bearing (see the _validate_query literal concatenation).