Updated docs for .search_sql() method

Also improved indentation of generated SQL queries. Refs #197
This commit is contained in:
Simon Willison 2020-11-08 09:04:33 -08:00
commit ce2b07c358
2 changed files with 12 additions and 10 deletions

View file

@ -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 <https://github.com/simonw/sqlite-fts4>`__. You can register that custom function against a ``Database`` connection using this method:

View file

@ -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()