Elffuss BraidHub

kikocis/lancedb public

main · 2.1 KiBRenderedRawPermalink
1# Reciprocal Rank Fusion Reranker
2
3This 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
11import numpy
12import lancedb
13from lancedb.embeddings import get_registry
14from lancedb.pydantic import LanceModel, Vector
15from lancedb.rerankers import RRFReranker
16
17embedder = get_registry().get("sentence-transformers").create()
18db = lancedb.connect("~/.lancedb")
19
20class Schema(LanceModel):
21 text: str = embedder.SourceField()
22 vector: Vector(embedder.ndims()) = embedder.VectorField()
23
24data = [
25 {"text": "hello world"},
26 {"text": "goodbye world"}
27 ]
28tbl = db.create_table("test", schema=Schema, mode="overwrite")
29tbl.add(data)
30reranker = RRFReranker()
31
32# Run hybrid search with a reranker
33tbl.create_fts_index("text", replace=True)
34result = tbl.search("hello", query_type="hybrid").rerank(reranker=reranker).to_list()
35
36```
37
38Accepted 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
47You 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`) |