| 1 | # Reciprocal Rank Fusion Reranker |
| 2 | |
| 3 | This is the default re-ranker used by LanceDB hybrid search. Reciprocal Rank Fusion (RRF) is an algorithm that evaluates the search scores by leveraging the positions/rank of the documents. The implementation follows this [paper](https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf). |
| 4 | |
| 5 | |
| 6 | !!! note |
| 7 | Supported Query Types: Hybrid |
| 8 | |
| 9 | |
| 10 | ```python |
| 11 | import numpy |
| 12 | import lancedb |
| 13 | from lancedb.embeddings import get_registry |
| 14 | from lancedb.pydantic import LanceModel, Vector |
| 15 | from lancedb.rerankers import RRFReranker |
| 16 | |
| 17 | embedder = get_registry().get("sentence-transformers").create() |
| 18 | db = lancedb.connect("~/.lancedb") |
| 19 | |
| 20 | class Schema(LanceModel): |
| 21 | text: str = embedder.SourceField() |
| 22 | vector: Vector(embedder.ndims()) = embedder.VectorField() |
| 23 | |
| 24 | data = [ |
| 25 | {"text": "hello world"}, |
| 26 | {"text": "goodbye world"} |
| 27 | ] |
| 28 | tbl = db.create_table("test", schema=Schema, mode="overwrite") |
| 29 | tbl.add(data) |
| 30 | reranker = RRFReranker() |
| 31 | |
| 32 | # Run hybrid search with a reranker |
| 33 | tbl.create_fts_index("text", replace=True) |
| 34 | result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list() |
| 35 | |
| 36 | ``` |
| 37 | |
| 38 | Accepted Arguments |
| 39 | ---------------- |
| 40 | | Argument | Type | Default | Description | |
| 41 | | --- | --- | --- | --- | |
| 42 | | `K` | `int` | `60` | A constant used in the RRF formula (default is 60). Experiments indicate that k = 60 was near-optimal, but that the choice is not critical | |
| 43 | | `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. | |
| 44 | |
| 45 | |
| 46 | ## Supported Scores for each query type |
| 47 | You can specify the type of scores you want the reranker to return. The following are the supported scores for each query type: |
| 48 | |
| 49 | ### Hybrid Search |
| 50 | |`return_score`| Status | Description | |
| 51 | | --- | --- | --- | |
| 52 | | `relevance` | ✅ Supported | Returned rows only have the `_relevance_score` column | |
| 53 | | `all` | ✅ Supported | Returned rows have vector(`_distance`) and FTS(`score`) along with Hybrid Search score(`_relevance_score`) | |