From ce2b07c358cd68e6de4c8942d7067591b7f1be96 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 8 Nov 2020 09:04:33 -0800 Subject: [PATCH] Updated docs for .search_sql() method Also improved indentation of generated SQL queries. Refs #197 --- docs/python-api.rst | 18 ++++++++++-------- sqlite_utils/db.py | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index 34dd226..8dead7d 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -1557,19 +1557,20 @@ Outputs: with original as ( select rowid, - [title], [author] + [title], + [author] from [articles] ) select - original.*, - [articles_fts].rank as rank + [original].[title], + [original].[author] from [original] join [articles_fts] on [original].rowid = [articles_fts].rowid where [articles_fts] match :query order by - rank + [articles_fts].rank This method detects if a SQLite table uses FTS4 or FTS5, and outputs the correct SQL for ordering by relevance depending on the search type. @@ -1580,19 +1581,20 @@ The FTS4 output looks something like this: with original as ( select rowid, - [title], [author] + [title], + [author] from [articles] ) select - original.*, - rank_bm25(matchinfo([articles_fts], 'pcnalx')) as rank + [original].[title], + [original].[author] from [original] join [articles_fts] on [original].rowid = [articles_fts].rowid where [articles_fts] match :query order by - rank + rank_bm25(matchinfo([articles_fts], 'pcnalx')) This uses the ``rank_bm25()`` custom SQL function from `sqlite-fts4 `__. You can register that custom function against a ``Database`` connection using this method: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c94216e..c91ef82 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1327,8 +1327,8 @@ class Table(Queryable): columns_sql = "*" columns_with_prefix_sql = "[{}].*".format(original) if columns: - columns_sql = ", ".join("[{}]".format(c) for c in columns) - columns_with_prefix_sql = ", ".join( + columns_sql = ",\n ".join("[{}]".format(c) for c in columns) + columns_with_prefix_sql = ",\n ".join( "[{}].[{}]".format(original, c) for c in columns ) fts_table = self.detect_fts()