Skip to content

Commit 7423296

Browse files
liunux4odoosimonw
andauthored
include_rank parameter for Table.search
* Add `include_rank` parameter to `Table.search` * Test for .search(include_rank) * Docs for table.search(include_rank) #628 Refs #480 --------- Co-authored-by: Simon Willison <[email protected]>
1 parent 4223070 commit 7423296

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

docs/python-api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,9 @@ The ``.search()`` method also accepts the following optional parameters:
23112311
``where_args`` dictionary
23122312
Arguments to use for ``:param`` placeholders in the extra WHERE clause
23132313
2314+
``include_rank`` bool
2315+
If set a ``rank`` column will be included with the BM25 ranking score - for FTS5 tables only.
2316+
23142317
``quote`` bool
23152318
Apply :ref:`FTS quoting rules <python_api_quote_fts>` to the search query, disabling advanced query syntax in a way that avoids surprising errors.
23162319

sqlite_utils/db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ def search(
26782678
offset: Optional[int] = None,
26792679
where: Optional[str] = None,
26802680
where_args: Optional[Union[Iterable, dict]] = None,
2681+
include_rank: bool = False,
26812682
quote: bool = False,
26822683
) -> Generator[dict, None, None]:
26832684
"""
@@ -2691,6 +2692,7 @@ def search(
26912692
:param offset: Optional integer SQL offset.
26922693
:param where: Extra SQL fragment for the WHERE clause
26932694
:param where_args: Arguments to use for :param placeholders in the extra WHERE clause
2695+
:param include_rank: Select the search rank column in the final query
26942696
:param quote: Apply quoting to disable any special characters in the search query
26952697
26962698
See :ref:`python_api_fts_search`.
@@ -2710,6 +2712,7 @@ def search(
27102712
limit=limit,
27112713
offset=offset,
27122714
where=where,
2715+
include_rank=include_rank,
27132716
),
27142717
args,
27152718
)

tests/test_fts.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from sqlite_utils import Database
33
from sqlite_utils.utils import sqlite3
4+
from unittest.mock import ANY
45

56
search_records = [
67
{
@@ -126,6 +127,32 @@ def test_search_where_args_disallows_query(fresh_db):
126127
)
127128

128129

130+
def test_search_include_rank(fresh_db):
131+
table = fresh_db["t"]
132+
table.insert_all(search_records)
133+
table.enable_fts(["text", "country"], fts_version="FTS5")
134+
results = list(table.search("are", include_rank=True))
135+
assert results == [
136+
{
137+
"rowid": 1,
138+
"text": "tanuki are running tricksters",
139+
"country": "Japan",
140+
"not_searchable": "foo",
141+
"rank": ANY,
142+
},
143+
{
144+
"rowid": 2,
145+
"text": "racoons are biting trash pandas",
146+
"country": "USA",
147+
"not_searchable": "bar",
148+
"rank": ANY,
149+
},
150+
]
151+
assert isinstance(results[0]["rank"], float)
152+
assert isinstance(results[1]["rank"], float)
153+
assert results[0]["rank"] < results[1]["rank"]
154+
155+
129156
def test_enable_fts_table_names_containing_spaces(fresh_db):
130157
table = fresh_db["test"]
131158
table.insert({"column with spaces": "in its name"})

0 commit comments

Comments
 (0)