| 1 | # Linear Combination Reranker |
| 2 | |
| 3 | This is the default re-ranker used by LanceDB hybrid search. It combines the results of semantic and full-text search using a linear combination of the scores. The weights for the linear combination can be specified. It defaults to 0.7, i.e, 70% weight for semantic search and 30% weight for full-text search. |
| 4 | |
| 5 | !!! note |
| 6 | Supported Query Types: Hybrid |
| 7 | |
| 8 | |
| 9 | ```python |
| 10 | import numpy |
| 11 | import lancedb |
| 12 | from lancedb.embeddings import get_registry |
| 13 | from lancedb.pydantic import LanceModel, Vector |
| 14 | from lancedb.rerankers import LinearCombinationReranker |
| 15 | |
| 16 | embedder = get_registry().get("sentence-transformers").create() |
| 17 | db = lancedb.connect("~/.lancedb") |
| 18 | |
| 19 | class Schema(LanceModel): |
| 20 | text: str = embedder.SourceField() |
| 21 | vector: Vector(embedder.ndims()) = embedder.VectorField() |
| 22 | |
| 23 | data = [ |
| 24 | {"text": "hello world"}, |
| 25 | {"text": "goodbye world"} |
| 26 | ] |
| 27 | tbl = db.create_table("test", schema=Schema, mode="overwrite") |
| 28 | tbl.add(data) |
| 29 | reranker = LinearCombinationReranker() |
| 30 | |
| 31 | # Run hybrid search with a reranker |
| 32 | tbl.create_fts_index("text", replace=True) |
| 33 | result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list() |
| 34 | |
| 35 | ``` |
| 36 | |
| 37 | Accepted Arguments |
| 38 | ---------------- |
| 39 | | Argument | Type | Default | Description | |
| 40 | | --- | --- | --- | --- | |
| 41 | | `weight` | `float` | `0.7` | The weight to use for the semantic search score. The weight for the full-text search score is `1 - weights`. | |
| 42 | | `return_score` | str | `"relevance"` | Options are "relevance" or "all". The type of score to return. If "relevance", will return only the `_relevance_score. If "all", will return all scores from the vector and FTS search along with the relevance score. | |
| 43 | |
| 44 | |
| 45 | ## Supported Scores for each query type |
| 46 | You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type: |
| 47 | |
| 48 | ### Hybrid Search |
| 49 | |`return_score`| Status | Description | |
| 50 | | --- | --- | --- | |
| 51 | | `relevance` | ✅ Supported | Returns only have the `_relevance_score` column | |
| 52 | | `all` | ✅ Supported | Returns have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_distance`) | |