mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): apply hybrid reranker weights when a search leg is empty (#5245)
## Summary Apply the configured hybrid weight and missing-score penalty when one of the two search legs returns no results. The mixed-leg path already applies those weights to a row matched by only one modality. With `weight=0.25` and `fill=0.4`, a normalized vector distance of 0.2 currently receives a relevance of 0.80 if FTS returns no results and 0.65 if FTS returns an unrelated result. Both cases now return 0.65. The same rule applies when the vector leg is empty. Missing raw scores still return as Arrow nulls. ## Validation Added regression coverage for both empty-leg directions. Each case compares the single-leg score with the score for the same row when an unrelated result appears on the other leg and checks the raw score and row ID. - With the release extension built from the base commit (0.42.0b37), both new cases fail at the base and pass with this change. The focused reranker and related hybrid controls report 17 passed and one skipped. - A local LanceDB hybrid search verifies that score and rank normalization both preserve the relevance of unchanged vector hits, along with row IDs, raw distances, and null raw FTS scores. - Ruff 0.9.9 lint and format checks pass for both changed files.
This commit is contained in:
@@ -19,6 +19,7 @@ class LinearCombinationReranker(Reranker):
|
||||
fill : float, default 1.0
|
||||
The score to give to results that are only in one of the two result sets.
|
||||
This is treated as penalty, so a higher value means a lower score.
|
||||
The same weighting and penalty apply when one result set is empty.
|
||||
TODO: We should just hardcode this--
|
||||
its pretty confusing as we invert scores to calculate final score
|
||||
return_score : str, default "relevance"
|
||||
@@ -53,12 +54,22 @@ class LinearCombinationReranker(Reranker):
|
||||
def merge_results(
|
||||
self, vector_results: pa.Table, fts_results: pa.Table, fill: float
|
||||
):
|
||||
# If one is empty then return the other and add _relevance_score
|
||||
# column equal the existing vector or fts score
|
||||
# Apply the same missing-score penalty whether one row or an entire
|
||||
# result set is missing its vector distance or FTS score.
|
||||
missing_fts_score = 1 - fill
|
||||
if len(vector_results) == 0:
|
||||
missing_vector_score = self._invert_score(fill)
|
||||
results = fts_results.append_column(
|
||||
"_relevance_score",
|
||||
pa.array(fts_results["_score"], type=pa.float32()),
|
||||
pa.array(
|
||||
[
|
||||
None
|
||||
if score is None
|
||||
else self._combine_score(missing_vector_score, score)
|
||||
for score in fts_results["_score"].to_pylist()
|
||||
],
|
||||
type=pa.float32(),
|
||||
),
|
||||
)
|
||||
if self.score == "relevance":
|
||||
results = self._keep_relevance_score(results)
|
||||
@@ -75,7 +86,9 @@ class LinearCombinationReranker(Reranker):
|
||||
"_relevance_score",
|
||||
pa.array(
|
||||
[
|
||||
self._invert_score(distance)
|
||||
self._combine_score(
|
||||
self._invert_score(distance), missing_fts_score
|
||||
)
|
||||
for distance in vector_results["_distance"].to_pylist()
|
||||
],
|
||||
type=pa.float32(),
|
||||
@@ -109,7 +122,7 @@ class LinearCombinationReranker(Reranker):
|
||||
# Missing FTS entries are penalised symmetrically: we use
|
||||
# `1 - fill` so that the same `fill` value drives both missing-vector
|
||||
# and missing-FTS penalties in the same direction.
|
||||
fts_score = result.get("_score", 1 - fill)
|
||||
fts_score = result.get("_score", missing_fts_score)
|
||||
result["_relevance_score"] = self._combine_score(vector_score, fts_score)
|
||||
combined_list.append(result)
|
||||
|
||||
|
||||
@@ -1164,6 +1164,66 @@ def test_linear_combination_missing_fts_is_penalised():
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("empty_side", ["vector", "fts"])
|
||||
def test_linear_combination_empty_side_keeps_weighted_scores(empty_side):
|
||||
reranker = LinearCombinationReranker(weight=0.25, fill=0.4, return_score="all")
|
||||
vector_results = pa.table(
|
||||
{
|
||||
"_rowid": pa.array([1], pa.uint64()),
|
||||
"_distance": pa.array([0.2], pa.float32()),
|
||||
}
|
||||
)
|
||||
fts_results = pa.table(
|
||||
{"_rowid": pa.array([2], pa.uint64()), "_score": pa.array([0.9], pa.float32())}
|
||||
)
|
||||
both = reranker.rerank_hybrid("query", vector_results, fts_results)
|
||||
scores = dict(zip(both["_rowid"].to_pylist(), both["_relevance_score"].to_pylist()))
|
||||
if empty_side == "vector":
|
||||
result = reranker.rerank_hybrid(
|
||||
"query", vector_results.slice(0, 0), fts_results
|
||||
)
|
||||
row_id, expected, missing_column = 2, 0.825, "_distance"
|
||||
else:
|
||||
result = reranker.rerank_hybrid(
|
||||
"query", vector_results, fts_results.slice(0, 0)
|
||||
)
|
||||
row_id, expected, missing_column = 1, 0.65, "_score"
|
||||
|
||||
assert result["_rowid"].to_pylist() == [row_id]
|
||||
assert result["_relevance_score"].to_pylist() == pytest.approx([expected])
|
||||
assert result["_relevance_score"].to_pylist() == [scores[row_id]]
|
||||
assert result[missing_column].to_pylist() == [None]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("return_score", ["relevance", "all"])
|
||||
def test_linear_combination_fts_only_preserves_null_scores(return_score):
|
||||
reranker = LinearCombinationReranker(
|
||||
weight=0.25, fill=0.4, return_score=return_score
|
||||
)
|
||||
vector_results = pa.table(
|
||||
{"_rowid": pa.array([], pa.uint64()), "_distance": pa.array([], pa.float32())}
|
||||
)
|
||||
fts_results = pa.table(
|
||||
{
|
||||
"_rowid": pa.array([22, 23], pa.uint64()),
|
||||
"_score": pa.array([0.0, None], pa.float32()),
|
||||
}
|
||||
)
|
||||
|
||||
result = reranker.rerank_hybrid("query", vector_results, fts_results)
|
||||
|
||||
assert result["_rowid"].to_pylist() == [22, 23]
|
||||
relevance = result["_relevance_score"]
|
||||
assert relevance.type == pa.float32()
|
||||
assert relevance[0].as_py() == pytest.approx(0.15)
|
||||
assert relevance[1].as_py() is None
|
||||
if return_score == "all":
|
||||
assert result["_score"].equals(fts_results["_score"])
|
||||
assert result["_distance"].equals(pa.chunked_array([pa.nulls(2, pa.float32())]))
|
||||
else:
|
||||
assert result.column_names == ["_rowid", "_relevance_score"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("empty_side", ["vector", "fts"])
|
||||
def test_linear_combination_empty_side_scores_are_null(empty_side):
|
||||
"""With return_score="all", the missing side's score column is null, not NaN."""
|
||||
|
||||
Reference in New Issue
Block a user