diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 15c78f6..37c44f7 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -941,6 +941,7 @@ def query( "Execute SQL query and return the results as JSON" db = sqlite_utils.Database(path) _load_extensions(db, load_extension) + db.register_fts4_bm25() with db.conn: cursor = db.execute(sql, dict(param)) if cursor.description is None: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index f1a1e74..9d067d3 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1360,7 +1360,7 @@ class Table(Queryable): rank_implementation = "[{}].rank".format(fts_table) else: self.db.register_fts4_bm25() - rank_implementation = "-rank_bm25(matchinfo([{}], 'pcnalx'))".format( + rank_implementation = "rank_bm25(matchinfo([{}], 'pcnalx'))".format( fts_table ) return sql.format( diff --git a/tests/test_cli.py b/tests/test_cli.py index f9b2509..69aef64 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1677,3 +1677,35 @@ def test_insert_encoding(tmpdir): "longitude": None, }, ] + + +@pytest.mark.parametrize("fts", ["FTS4", "FTS5"]) +@pytest.mark.parametrize( + "extra_arg,expected", + [ + ( + None, + '[{"rowid": 2, "id": 2, "title": "Title the second", "rank": -0.5108256237659907}]\n', + ), + ("--csv", "rowid,id,title,rank\n2,2,Title the second,-0.5108256237659907\n"), + ], +) +def test_search(tmpdir, fts, extra_arg, expected): + db_path = str(tmpdir / "test.db") + db = Database(db_path) + db["articles"].insert_all( + [ + {"id": 1, "title": "Title the first"}, + {"id": 2, "title": "Title the second"}, + {"id": 3, "title": "Title the third"}, + ], + pk="id", + ) + db["articles"].enable_fts(["title"], fts_version=fts) + result = CliRunner().invoke( + cli.cli, + ["search", db_path, "articles", "second"] + ([extra_arg] if extra_arg else []), + catch_exceptions=False, + ) + assert result.exit_code == 0 + assert result.output == expected diff --git a/tests/test_fts.py b/tests/test_fts.py index 0b0c106..6f469a2 100644 --- a/tests/test_fts.py +++ b/tests/test_fts.py @@ -116,7 +116,7 @@ def test_populate_fts(fresh_db): "text": "racoons are biting trash pandas", "country": "USA", "not_searchable": "bar", - "rank": 0.5108256237659907, + "rank": -0.5108256237659907, } ] == rows @@ -137,7 +137,7 @@ def test_populate_fts_escape_table_names(fresh_db): "text": "racoons are biting trash pandas", "country": "USA", "not_searchable": "bar", - "rank": 0.5108256237659907, + "rank": -0.5108256237659907, } ] == list(table.search("usa"))