Elffuss BraidHub

kikocis/lancedb public

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