From 7d0299edd40636037a06548736681b67385b499e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 5 Aug 2018 17:44:47 -0700 Subject: [PATCH] fts_table and fts_pk metadata configs, available for both tables and views --- datasette/views/table.py | 8 ++++++-- docs/full_text_search.rst | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/datasette/views/table.py b/datasette/views/table.py index a3a2c3b7..626fe1f6 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -309,7 +309,10 @@ class TableView(RowTableShared): where_clauses, params = filters.build_where_clauses() # _search support: - fts_table = info[name]["tables"].get(table, {}).get("fts_table") + fts_table = table_metadata.get( + "fts_table", info[name]["tables"].get(table, {}).get("fts_table") + ) + fts_pk = table_metadata.get("fts_pk", "rowid") search_args = dict( pair for pair in special_args.items() if pair[0].startswith("_search") ) @@ -320,8 +323,9 @@ class TableView(RowTableShared): # Simple ?_search=xxx search = search_args["_search"] where_clauses.append( - "rowid in (select rowid from {fts_table} where {fts_table} match :search)".format( + "{fts_pk} in (select rowid from {fts_table} where {fts_table} match :search)".format( fts_table=escape_sqlite(fts_table), + fts_pk=escape_sqlite(fts_pk) ) ) search_descriptions.append('search matches "{}"'.format(search)) diff --git a/docs/full_text_search.rst b/docs/full_text_search.rst index 72eabc7d..70900341 100644 --- a/docs/full_text_search.rst +++ b/docs/full_text_search.rst @@ -69,6 +69,30 @@ And then populate it like this: You can use this technique to populate the full-text search index from any combination of tables and joins that makes sense for your project. +Configuring full-text search for a table or view +------------------------------------------------ + +If a table has a corresponding FTS table set up using the ``content=`` argument to ``CREATE VIRTUAL TABLE`` shown above, Datasette will detect it automatically and add a search interface to the table page for that table. + +You can also manually configure which table should be used for full-text search using :ref:`metadata`. You can set the associated FTS table for a specific table and you can also set one for a view - if you do that, the page for that SQL view will offer a search option. + +The ``fts_table`` property can be used to specify an associated FTS table. If the primary key column in your table which was used to populate the FTS table is something other than ``rowid``, you can specify the column to use with the ``fts_pk`` property. + +Here is an example which enables full-text search for a ``display_ads`` view which is defined against the ``ads`` table and hence needs to run FTS against the ``ads_fts`` table, using the ``id`` as the primary key:: + + { + "databases": { + "russian-ads": { + "tables": { + "display_ads": { + "fts_table": "ads_fts", + "fts_pk": "id" + } + } + } + } + } + Setting up full-text search using csvs-to-sqlite ------------------------------------------------